-
-
Notifications
You must be signed in to change notification settings - Fork 669
Expand file tree
/
Copy pathinstall-dependencies.ts
More file actions
62 lines (59 loc) · 2.2 KB
/
install-dependencies.ts
File metadata and controls
62 lines (59 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import {
type ChildProcess,
type SpawnOptionsWithStdioTuple,
type StdioNull,
type StdioPipe,
} from "node:child_process";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { spawn } from "cross-spawn";
import { type NodePlopAPI } from "node-plop";
export default async function installDependencies(plop: NodePlopAPI) {
const __dirname = dirname(fileURLToPath(import.meta.url));
plop.setPlopfilePath(resolve(__dirname, "../plopfile.js"));
plop.setDefaultInclude({ actions: true });
plop.setActionType("install-dependencies", (answers, config) => {
const options: SpawnOptionsWithStdioTuple<
StdioNull,
StdioNull | StdioPipe,
StdioPipe | StdioNull
> = {
cwd: config.path,
stdio: [
"inherit", // Use parent's stdio configuration
process.stdout.isTTY ? "inherit" : "pipe", // Pipe child process' stdout to parent's stdout
process.stderr.isTTY ? "inherit" : "pipe", // Pipe child process' stderr to parent's stderr
],
};
// promise to complete subprocess of installing packages and return a message
const returnPromise = new Promise<string>((resolve, reject) => {
const returnMessage = "Project dependencies installed successfully!";
const { packageManager } = answers;
const packages = config.packages.length === 1 ? [config.packages[0]] : config.packages;
const installOptions: Record<string, string[]> = {
npm: ["install", "--save-dev"],
yarn: ["add", "-D"],
pnpm: ["install", "--save-dev"],
};
const npmInstallPackages: ChildProcess = spawn(
`${packageManager}`,
[...installOptions[packageManager], ...packages],
options,
);
npmInstallPackages.stdout?.on("data", (data) => {
console.log(data.toString());
});
npmInstallPackages.stderr?.on("data", (data) => {
console.warn(data.toString());
});
npmInstallPackages.on("exit", (code) => {
if (code === 0) {
resolve(returnMessage);
} else {
reject(new Error(`Error occurred while installing packages\n Exit code: ${code}`));
}
});
});
return returnPromise;
});
}