-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.ts
More file actions
76 lines (64 loc) · 1.61 KB
/
Copy pathutil.ts
File metadata and controls
76 lines (64 loc) · 1.61 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import chalk from "chalk";
import spawn from "cross-spawn";
import prompts from "prompts";
import validateProjectName from "validate-npm-package-name";
export function failOnError(
response: ReturnType<typeof spawn.sync>,
message: string
) {
if (response.status !== 0) {
console.error(chalk.red(message));
process.exit(1);
}
}
export async function askProjectName() {
const defaultName = "new-project";
const answer = await prompts({
type: "text",
name: "projectName",
message: "Project name:",
initial: defaultName,
validate: (name) => {
const result = validateProjectName(name);
if (result.validForNewPackages) {
return true;
}
return `Invalid project name: ${name}`;
},
});
if (typeof answer.projectName === "string") {
return answer.projectName.trim();
}
return defaultName;
}
export type PackageManager = "yarn" | "pnpm" | "npm";
export function getPackageManager(): PackageManager {
const packageManager = process.env.npm_config_user_agent;
if (packageManager?.startsWith("yarn")) {
return "yarn";
} else if (packageManager?.startsWith("pnpm")) {
return "pnpm";
} else {
return "npm";
}
}
export function installPackages(
manager: PackageManager,
dependencies: string[]
) {
let executable = "npm";
let command = "install";
if (manager === "yarn") {
executable = "yarn";
command = "add";
}
if (manager == "pnpm") {
executable = "pnpm";
}
failOnError(
spawn.sync(executable, [command, "-D", ...dependencies], {
stdio: "inherit",
}),
"Unable to install dependencies"
);
}