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

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

parent 74c920a6
Branches
Tags
No related merge requests found
......@@ -8,13 +8,13 @@ import type { PackageManager } from "./get-pkg-manager";
*
* @returns A Promise that resolves once the installation is finished.
*/
export async function install(
export async function callPackageManager(
/** Indicate which package manager to use. */
packageManager: PackageManager,
/** Indicate whether there is an active Internet connection.*/
isOnline: boolean,
args: string[] = ["install"],
): Promise<void> {
let args: string[] = ["install"];
if (!isOnline) {
console.log(
yellow("You appear to be offline.\nFalling back to the local cache."),
......
import { copy } from "../helpers/copy";
import { install } from "../helpers/install";
import { callPackageManager } from "../helpers/install";
import fs from "fs/promises";
import os from "os";
......@@ -7,7 +7,12 @@ import path from "path";
import { bold, cyan } from "picocolors";
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> = {
nextjs: ".env.local",
......@@ -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.
*/
......@@ -45,7 +92,6 @@ const installTSTemplate = async ({
ui,
eslint,
customApiPath,
openAIKey,
}: InstallTemplateArgs) => {
console.log(bold(`Using ${packageManager}.`));
......@@ -57,23 +103,6 @@ const installTSTemplate = async ({
const copySource = ["**"];
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, {
parents: true,
cwd: templatePath,
......@@ -115,8 +144,6 @@ const installTSTemplate = async ({
});
}
await createEnvLocalFile(root, framework, openAIKey);
/**
* Update the package.json scripts.
*/
......@@ -205,18 +232,14 @@ const installTSTemplate = async ({
console.log();
await install(packageManager, isOnline);
await callPackageManager(packageManager, isOnline);
};
const installPythonTemplate = async ({
root,
template,
framework,
openAIKey,
}: Pick<
InstallTemplateArgs,
"root" | "framework" | "template" | "openAIKey"
>) => {
}: Pick<InstallTemplateArgs, "root" | "framework" | "template">) => {
console.log("\nInitializing Python project with template:", template, "\n");
const templatePath = path.join(__dirname, "types", template, framework);
await copy("**", root, {
......@@ -239,8 +262,6 @@ const installPythonTemplate = async ({
},
});
await createEnvLocalFile(root, framework, openAIKey);
console.log(
"\nPython project, dependencies won't be installed automatically.\n",
);
......@@ -253,6 +274,17 @@ export const installTemplate = async (props: InstallTemplateArgs) => {
} else {
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";
File deleted
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment