Skip to content
Snippets Groups Projects
Commit 82ce5db4 authored by Huu Le (Lee)'s avatar Huu Le (Lee) Committed by GitHub
Browse files

fix: add handle error from template installation (#522)

parent 56f8942e
No related branches found
No related tags found
No related merge requests found
/* eslint-disable import/no-extraneous-dependencies */ /* eslint-disable import/no-extraneous-dependencies */
import path from "path"; import path from "path";
import { green, yellow } from "picocolors"; import { green, red } from "picocolors";
import { tryGitInit } from "./helpers/git"; import { tryGitInit } from "./helpers/git";
import { isFolderEmpty } from "./helpers/is-folder-empty"; import { isFolderEmpty } from "./helpers/is-folder-empty";
import { getOnline } from "./helpers/is-online"; import { getOnline } from "./helpers/is-online";
...@@ -87,28 +87,48 @@ export async function createApp({ ...@@ -87,28 +87,48 @@ export async function createApp({
tools, tools,
}; };
let installationErrors = [];
if (frontend) { if (frontend) {
// install backend // install backend
const backendRoot = path.join(root, "backend"); const backendRoot = path.join(root, "backend");
await makeDir(backendRoot); await makeDir(backendRoot);
await installTemplate({ ...args, root: backendRoot, backend: true }); try {
await installTemplate({
...args,
root: backendRoot,
backend: true,
});
} catch (error) {
installationErrors.push(error);
console.log(red(`${error}`));
}
// install frontend // install frontend
const frontendRoot = path.join(root, "frontend"); const frontendRoot = path.join(root, "frontend");
await makeDir(frontendRoot); await makeDir(frontendRoot);
await installTemplate({ try {
...args, await installTemplate({
root: frontendRoot, ...args,
framework: "nextjs", root: frontendRoot,
customApiPath: `http://localhost:${externalPort ?? 8000}/api/chat`, framework: "nextjs",
backend: false, customApiPath: `http://localhost:${externalPort ?? 8000}/api/chat`,
}); backend: false,
});
} catch (error) {
installationErrors.push(error);
}
// copy readme for fullstack // copy readme for fullstack
await fs.promises.copyFile( await fs.promises.copyFile(
path.join(templatesDir, "README-fullstack.md"), path.join(templatesDir, "README-fullstack.md"),
path.join(root, "README.md"), path.join(root, "README.md"),
); );
} else { } else {
await installTemplate({ ...args, backend: true, forBackend: framework }); try {
await installTemplate({ ...args, backend: true, forBackend: framework });
} catch (error) {
installationErrors.push(error);
}
} }
process.chdir(root); process.chdir(root);
...@@ -119,7 +139,7 @@ export async function createApp({ ...@@ -119,7 +139,7 @@ export async function createApp({
if (toolsRequireConfig(tools)) { if (toolsRequireConfig(tools)) {
console.log( console.log(
yellow( red(
`You have selected tools that require configuration. Please configure them in the ${terminalLink( `You have selected tools that require configuration. Please configure them in the ${terminalLink(
"tools_config.json", "tools_config.json",
`file://${root}/tools_config.json`, `file://${root}/tools_config.json`,
...@@ -127,14 +147,24 @@ export async function createApp({ ...@@ -127,14 +147,24 @@ export async function createApp({
), ),
); );
} }
console.log("");
console.log(`${green("Success!")} Created ${appName} at ${appPath}`);
console.log(
`Now have a look at the ${terminalLink(
"README.md",
`file://${root}/README.md`,
)} and learn how to get started.`,
);
console.log(); console.log();
if (installationErrors.length > 0) {
for (const error of installationErrors) {
console.log(red(`${error}`));
}
console.log(
"\nExiting installation. Please check the generated code or try create app again!",
);
process.exit(1);
} else {
console.log(`${green("Success!")} Created ${appName} at ${appPath}`);
console.log(
`Now have a look at the ${terminalLink(
"README.md",
`file://${root}/README.md`,
)} and learn how to get started.`,
);
console.log();
}
} }
import fs from "fs/promises"; import fs from "fs/promises";
import path from "path"; import path from "path";
import { cyan, red, yellow } from "picocolors"; import { cyan } from "picocolors";
import { parse, stringify } from "smol-toml"; import { parse, stringify } from "smol-toml";
import terminalLink from "terminal-link"; import terminalLink from "terminal-link";
import { copy } from "./copy"; import { copy } from "./copy";
...@@ -103,22 +103,16 @@ export const installPythonDependencies = ( ...@@ -103,22 +103,16 @@ export const installPythonDependencies = (
); );
const installSuccessful = tryPoetryInstall(noRoot); const installSuccessful = tryPoetryInstall(noRoot);
if (!installSuccessful) { if (!installSuccessful) {
console.error( throw new Error(
red("Install failed. Please install dependencies manually."), "Poetry installation failed. Please install dependencies manually.",
); );
process.exit(1);
} }
} else { } else {
console.warn( throw new Error(`Poetry is not available in the current environment. The Python dependencies will not be installed automatically.
yellow(
`Poetry is not available in the current environment. The Python dependencies will not be installed automatically.
Please check ${terminalLink( Please check ${terminalLink(
"Poetry Installation", "Poetry Installation",
`https://python-poetry.org/docs/#installation`, `https://python-poetry.org/docs/#installation`,
)} to install poetry first, then install the dependencies manually.`, )} to install poetry first, then install the dependencies manually.`);
),
);
process.exit(1);
} }
}; };
...@@ -222,4 +216,5 @@ export const installPythonTemplate = async ({ ...@@ -222,4 +216,5 @@ export const installPythonTemplate = async ({
if (postInstallAction !== "none") { if (postInstallAction !== "none") {
installPythonDependencies(); installPythonDependencies();
} }
console.log("\nFastAPI project initialized successfully!\n");
}; };
import fs from "fs/promises"; import fs from "fs/promises";
import os from "os"; import os from "os";
import path from "path"; import path from "path";
import { bold, cyan } from "picocolors"; import { bold, cyan, green } from "picocolors";
import { version } from "../../core/package.json"; import { version } from "../../core/package.json";
import { copy } from "../helpers/copy"; import { copy } from "../helpers/copy";
import { callPackageManager } from "../helpers/install"; import { callPackageManager } from "../helpers/install";
...@@ -231,4 +231,6 @@ export const installTSTemplate = async ({ ...@@ -231,4 +231,6 @@ export const installTSTemplate = async ({
if (postInstallAction !== "none") { if (postInstallAction !== "none") {
await installTSDependencies(packageJson, packageManager, isOnline); await installTSDependencies(packageJson, packageManager, isOnline);
} }
console.log(green(`${framework} project is initialized successfully.`));
}; };
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