Skip to content

Commit

Permalink
feat: improve package install performance
Browse files Browse the repository at this point in the history
  • Loading branch information
rsaz committed Aug 1, 2024
1 parent 565a069 commit b7ac564
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/new/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`));
});

Expand All @@ -51,6 +56,7 @@ async function packageManagerInstall({
});

installProcess.on("close", (code) => {
clearTimeout(installTimeout);
if (code === 0) {
resolve("Installation Done!");
} else {
Expand All @@ -61,6 +67,11 @@ async function packageManagerInstall({
);
}
});

installTimeout = setTimeout(() => {
installProcess.kill("SIGKILL");
reject(new Error("Installation took too long. Aborted!"));
}, 600000);
});
}

Expand All @@ -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));
}

Expand Down

0 comments on commit b7ac564

Please sign in to comment.