forked from microsoft/typespec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlegacy-helpers.js
51 lines (43 loc) · 1.58 KB
/
legacy-helpers.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
import { spawn, spawnSync } from "child_process";
import { dirname, resolve } from "path";
import { fileURLToPath } from "url";
const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), "../..");
const prettier = resolve(repoRoot, "packages/compiler/node_modules/.bin/prettier");
const tsc = resolve(repoRoot, "packages/compiler/node_modules/.bin/tsc");
// We could use { shell: true } to let Windows find .cmd, but that causes other issues.
// It breaks ENOENT checking for command-not-found and also handles command/args with spaces
// poorly.
const isCmdOnWindows = ["rush", "npm", "code", "code-insiders", "docusaurus", tsc, prettier];
export class CommandFailedError extends Error {
constructor(msg, proc) {
super(msg);
this.proc = proc;
}
}
export function run(command, args, options) {
console.log();
console.log(`> ${command} ${args.join(" ")}`);
options = {
stdio: "inherit",
sync: true,
throwOnNonZeroExit: true,
...options,
};
if (process.platform === "win32" && isCmdOnWindows.includes(command)) {
command += ".cmd";
}
const proc = (options.sync ? spawnSync : spawn)(command, args, options);
if (proc.error) {
if (options.ignoreCommandNotFound && proc.error.code === "ENOENT") {
console.log(`Skipped: Command \`${command}\` not found.`);
} else {
throw proc.error;
}
} else if (options.throwOnNonZeroExit && proc.status !== undefined && proc.status !== 0) {
throw new CommandFailedError(
`Command \`${command} ${args.join(" ")}\` failed with exit code ${proc.status}`,
proc
);
}
return proc;
}