-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcli.js
executable file
·56 lines (48 loc) · 1.48 KB
/
cli.js
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
const path = require("path");
const fs = require("fs");
const validCommands = [
"build",
"clean",
"install",
"installAndBuild",
"lint",
"lintfix",
"list",
"list-deps",
"offlineInstall",
"syncdeps",
"test",
"doc",
];
function parseCommandLine() {
const [, scriptPath, ...parameters] = process.argv;
if (parameters.includes("--help")) return { scriptPath, command: null };
const startIndex = parameters.indexOf("--start");
if (startIndex > 0 && startIndex + 1 >= parameters.length)
return { scriptPath, command: null };
const start =
startIndex !== -1 ? parameters.splice(startIndex, 2)[1] : null;
if (parameters.length > 1) return { scriptPath, command: null };
const command = parameters.length ? parameters[0] : "installAndBuild";
return {
scriptPath,
command: validCommands.includes(command) ? command : null,
start,
};
}
function showUsage(scriptPath) {
const baseName = path.basename(scriptPath);
const validCommandString = validCommands.join(" | ");
console.error(
`Usage: ${baseName} [ --start PACKAGE_NAME ] [ ${validCommandString} ]`
);
console.error(" Run without arguments to build all packages");
}
function parseManifest(path) {
try {
return JSON.parse(fs.readFileSync(path));
} catch (e) {
throw `Cannot read ${path} or parse the result: ${e}`;
}
}
module.exports = { parseCommandLine, showUsage, parseManifest };