From 565a069aea5934c8b5aacce18c1a12e8bb60987b Mon Sep 17 00:00:00 2001 From: Richard Zampieri Date: Wed, 31 Jul 2024 17:20:44 -0700 Subject: [PATCH 1/2] fix: update nodejs latest version --- package.json | 5 +++-- src/new/cli.ts | 10 +++++----- src/new/form.ts | 1 + src/utils/cli-ui.ts | 14 ++++++++++++++ 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 855ee4c..e8b9a98 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "expressots": "bin/cli.js" }, "engines": { - "node": ">=18.18.0" + "node": ">=18.0.0" }, "funding": { "type": "github", @@ -31,7 +31,7 @@ "Scaffolding" ], "scripts": { - "prepare": "husky install", + "prepare": "husky", "start:build": "npm run build && npm run start", "start": "node ./bin/cli.js", "start:dev": "tsnd ./src/cli.ts", @@ -58,6 +58,7 @@ "inquirer": "8.2.6", "mustache": "4.2.0", "semver": "7.6.2", + "ts-node": "10.9.2", "yargs": "17.7.2" }, "devDependencies": { diff --git a/src/new/cli.ts b/src/new/cli.ts index 7b02655..c808078 100644 --- a/src/new/cli.ts +++ b/src/new/cli.ts @@ -1,6 +1,8 @@ import { Argv, CommandModule } from "yargs"; import { projectForm } from "./form"; import semver from "semver"; +import { printWarning } from "../utils/cli-ui"; +import chalk from "chalk"; type CommandModuleArgs = object; @@ -40,14 +42,12 @@ const commandOptions = (yargs: Argv): Argv => { const checkNodeVersion = (): void => { const minVersion = "18.0.0"; - const maxVersion = "20.7.0"; + const maxVersion = "22.5.1"; const currentVersion = process.version; if (!semver.satisfies(currentVersion, `>=${minVersion} <=${maxVersion}`)) { - console.error( - `Node.js version ${currentVersion} is not supported. Please use a version between ${minVersion} and ${maxVersion}.`, - ); - process.exit(1); + const msg: string = `Node.js version [${chalk.bold(chalk.white(currentVersion))}] is not tested. Please use a version between ${minVersion} and ${maxVersion}.`; + printWarning(msg); } }; diff --git a/src/new/form.ts b/src/new/form.ts index 51ddd62..b90d2e3 100644 --- a/src/new/form.ts +++ b/src/new/form.ts @@ -25,6 +25,7 @@ async function packageManagerInstall({ const installProcess = spawn(command, ["install"], { cwd: directory, + shell: true, }); installProcess.on("error", (error) => { diff --git a/src/utils/cli-ui.ts b/src/utils/cli-ui.ts index e5bb64a..08f7755 100644 --- a/src/utils/cli-ui.ts +++ b/src/utils/cli-ui.ts @@ -1,4 +1,5 @@ import chalk from "chalk"; +import { stdout } from "process"; export function printError(message: string, component: string): void { console.error( @@ -6,6 +7,19 @@ export function printError(message: string, component: string): void { ); } +export function printWarning(message: string, component?: string): void { + if (component === undefined) { + stdout.write(chalk.yellow(`${message} ⚠️\n`)); + return; + } + stdout.write( + chalk.yellow( + `${message}:`, + chalk.bold(chalk.white(`[${component}] ⚠️\n`)), + ), + ); +} + export async function printGenerateError(schematic: string, file: string) { console.error( " ", From b7ac564d4fc8adf54134c87d2f6e566e21790dae Mon Sep 17 00:00:00 2001 From: Richard Zampieri Date: Wed, 31 Jul 2024 18:11:24 -0700 Subject: [PATCH 2/2] feat: improve package install performance --- src/new/form.ts | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/new/form.ts b/src/new/form.ts index b90d2e3..387039f 100644 --- a/src/new/form.ts +++ b/src/new/form.ts @@ -23,12 +23,17 @@ async function packageManagerInstall({ ? `${packageManager}.cmd` : packageManager; - const installProcess = spawn(command, ["install"], { + const installProcess = spawn(command, ["install", "--prefer-offline"], { cwd: directory, shell: true, + timeout: 600000, }); + // eslint-disable-next-line prefer-const + let installTimeout: NodeJS.Timeout; + installProcess.on("error", (error) => { + clearTimeout(installTimeout); reject(new Error(`Failed to start subprocess: ${error.message}`)); }); @@ -51,6 +56,7 @@ async function packageManagerInstall({ }); installProcess.on("close", (code) => { + clearTimeout(installTimeout); if (code === 0) { resolve("Installation Done!"); } else { @@ -61,6 +67,11 @@ async function packageManagerInstall({ ); } }); + + installTimeout = setTimeout(() => { + installProcess.kill("SIGKILL"); + reject(new Error("Installation took too long. Aborted!")); + }, 600000); }); } @@ -81,18 +92,11 @@ function changePackageName({ directory: string; name: string; }): void { - // Get the absolute path of the input directory parameter const absDirPath = path.resolve(directory); - - // Load the package.json file const packageJsonPath = path.join(absDirPath, "package.json"); const fileContents = fs.readFileSync(packageJsonPath, "utf-8"); const packageJson = JSON.parse(fileContents); - - // Change the name packageJson.name = name; - - // Save the package.json file fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2)); }