Skip to content
Snippets Groups Projects
Commit df03819e authored by Marcus Schiesser's avatar Marcus Schiesser
Browse files

feat: copy test PDF for TS projects and automatically call npm run generate

parent 072354af
No related branches found
No related tags found
No related merge requests found
...@@ -8,13 +8,13 @@ import type { PackageManager } from "./get-pkg-manager"; ...@@ -8,13 +8,13 @@ import type { PackageManager } from "./get-pkg-manager";
* *
* @returns A Promise that resolves once the installation is finished. * @returns A Promise that resolves once the installation is finished.
*/ */
export async function install( export async function callPackageManager(
/** Indicate which package manager to use. */ /** Indicate which package manager to use. */
packageManager: PackageManager, packageManager: PackageManager,
/** Indicate whether there is an active Internet connection.*/ /** Indicate whether there is an active Internet connection.*/
isOnline: boolean, isOnline: boolean,
args: string[] = ["install"],
): Promise<void> { ): Promise<void> {
let args: string[] = ["install"];
if (!isOnline) { if (!isOnline) {
console.log( console.log(
yellow("You appear to be offline.\nFalling back to the local cache."), yellow("You appear to be offline.\nFalling back to the local cache."),
......
import { copy } from "../helpers/copy"; import { copy } from "../helpers/copy";
import { install } from "../helpers/install"; import { callPackageManager } from "../helpers/install";
import fs from "fs/promises"; import fs from "fs/promises";
import os from "os"; import os from "os";
...@@ -7,7 +7,12 @@ import path from "path"; ...@@ -7,7 +7,12 @@ import path from "path";
import { bold, cyan } from "picocolors"; import { bold, cyan } from "picocolors";
import { version } from "../package.json"; import { version } from "../package.json";
import { InstallTemplateArgs, TemplateFramework } from "./types"; import { PackageManager } from "../helpers/get-pkg-manager";
import {
InstallTemplateArgs,
TemplateEngine,
TemplateFramework,
} from "./types";
const envFileNameMap: Record<TemplateFramework, string> = { const envFileNameMap: Record<TemplateFramework, string> = {
nextjs: ".env.local", nextjs: ".env.local",
...@@ -31,6 +36,48 @@ const createEnvLocalFile = async ( ...@@ -31,6 +36,48 @@ const createEnvLocalFile = async (
} }
}; };
const copyTestData = async (
root: string,
framework: TemplateFramework,
packageManager?: PackageManager,
engine?: TemplateEngine,
) => {
if (engine === "context" || framework === "fastapi") {
const srcPath = path.join(__dirname, "components", "data");
const destPath = path.join(root, "data");
console.log(`\nCopying test data to ${cyan(destPath)}\n`);
await copy("**", destPath, {
parents: true,
cwd: srcPath,
});
}
if (packageManager && engine === "context") {
console.log(
`\nRunning ${cyan("npm run generate")} to generate the context data.\n`,
);
await callPackageManager(packageManager, true, ["run", "generate"]);
console.log();
}
};
const rename = (name: string) => {
switch (name) {
case "gitignore":
case "eslintrc.json": {
return `.${name}`;
}
// README.md is ignored by webpack-asset-relocator-loader used by ncc:
// https://github.com/vercel/webpack-asset-relocator-loader/blob/e9308683d47ff507253e37c9bcbb99474603192b/src/asset-relocator.js#L227
case "README-template.md": {
return "README.md";
}
default: {
return name;
}
}
};
/** /**
* Install a LlamaIndex internal template to a given `root` directory. * Install a LlamaIndex internal template to a given `root` directory.
*/ */
...@@ -45,7 +92,6 @@ const installTSTemplate = async ({ ...@@ -45,7 +92,6 @@ const installTSTemplate = async ({
ui, ui,
eslint, eslint,
customApiPath, customApiPath,
openAIKey,
}: InstallTemplateArgs) => { }: InstallTemplateArgs) => {
console.log(bold(`Using ${packageManager}.`)); console.log(bold(`Using ${packageManager}.`));
...@@ -57,23 +103,6 @@ const installTSTemplate = async ({ ...@@ -57,23 +103,6 @@ const installTSTemplate = async ({
const copySource = ["**"]; const copySource = ["**"];
if (!eslint) copySource.push("!eslintrc.json"); if (!eslint) copySource.push("!eslintrc.json");
const rename = (name: string) => {
switch (name) {
case "gitignore":
case "eslintrc.json": {
return `.${name}`;
}
// README.md is ignored by webpack-asset-relocator-loader used by ncc:
// https://github.com/vercel/webpack-asset-relocator-loader/blob/e9308683d47ff507253e37c9bcbb99474603192b/src/asset-relocator.js#L227
case "README-template.md": {
return "README.md";
}
default: {
return name;
}
}
};
await copy(copySource, root, { await copy(copySource, root, {
parents: true, parents: true,
cwd: templatePath, cwd: templatePath,
...@@ -115,8 +144,6 @@ const installTSTemplate = async ({ ...@@ -115,8 +144,6 @@ const installTSTemplate = async ({
}); });
} }
await createEnvLocalFile(root, framework, openAIKey);
/** /**
* Update the package.json scripts. * Update the package.json scripts.
*/ */
...@@ -205,18 +232,14 @@ const installTSTemplate = async ({ ...@@ -205,18 +232,14 @@ const installTSTemplate = async ({
console.log(); console.log();
await install(packageManager, isOnline); await callPackageManager(packageManager, isOnline);
}; };
const installPythonTemplate = async ({ const installPythonTemplate = async ({
root, root,
template, template,
framework, framework,
openAIKey, }: Pick<InstallTemplateArgs, "root" | "framework" | "template">) => {
}: Pick<
InstallTemplateArgs,
"root" | "framework" | "template" | "openAIKey"
>) => {
console.log("\nInitializing Python project with template:", template, "\n"); console.log("\nInitializing Python project with template:", template, "\n");
const templatePath = path.join(__dirname, "types", template, framework); const templatePath = path.join(__dirname, "types", template, framework);
await copy("**", root, { await copy("**", root, {
...@@ -239,8 +262,6 @@ const installPythonTemplate = async ({ ...@@ -239,8 +262,6 @@ const installPythonTemplate = async ({
}, },
}); });
await createEnvLocalFile(root, framework, openAIKey);
console.log( console.log(
"\nPython project, dependencies won't be installed automatically.\n", "\nPython project, dependencies won't be installed automatically.\n",
); );
...@@ -253,6 +274,17 @@ export const installTemplate = async (props: InstallTemplateArgs) => { ...@@ -253,6 +274,17 @@ export const installTemplate = async (props: InstallTemplateArgs) => {
} else { } else {
await installTSTemplate(props); await installTSTemplate(props);
} }
// Copy the environment file to the target directory.
await createEnvLocalFile(props.root, props.framework, props.openAIKey);
// Copy test pdf file
await copyTestData(
props.root,
props.framework,
props.packageManager,
props.engine,
);
}; };
export * from "./types"; export * from "./types";
File deleted
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment