-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathrun.js
More file actions
121 lines (113 loc) · 2.93 KB
/
run.js
File metadata and controls
121 lines (113 loc) · 2.93 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { spawn, spawnSync } from 'node:child_process';
import { fileURLToPath } from 'node:url';
import {
isDebugVerbosity,
debuglog
} from './verbosity.js';
export const IGNORE = '__ignore__';
function runAsyncBase(cmd, args, {
ignoreFailure = true,
spawnArgs,
input,
captureStdout = false,
captureStderr = false
} = {}) {
if (cmd instanceof URL) {
cmd = fileURLToPath(cmd);
}
let stdio = 'inherit';
if (captureStdout || input != null) {
stdio = [
input == null ? 'inherit' : 'pipe',
captureStdout ? 'pipe' : 'inherit',
captureStderr ? 'pipe' : 'inherit'
];
}
return new Promise((resolve, reject) => {
const opt = Object.assign({
cwd: process.cwd(),
stdio
}, spawnArgs);
if (isDebugVerbosity()) {
debuglog('[Spawn]', `${cmd} ${(args || []).join(' ')}`, opt);
}
const child = spawn(cmd, args, opt);
let stdout;
if (captureStdout) {
stdout = '';
child.stdout.setEncoding('utf8');
child.stdout.on('data', (chunk) => { stdout += chunk; });
}
let stderr;
if (captureStderr) {
stderr = '';
child.stderr.setEncoding('utf8');
child.stderr.on('data', (chunk) => { stderr += chunk; });
}
child.on('error', reject);
child.on('close', (code) => {
if (code !== 0) {
if (ignoreFailure) {
return reject(new Error(IGNORE));
}
const err = new Error(`${cmd} ${args} failed: ${code}`);
if (stderr) {
err.stderr = stderr;
}
err.code = code;
err.messageOnly = true;
return reject(err);
}
if (captureStdout === 'lines') {
stdout = stdout.split(/\r?\n/g);
if (stdout[stdout.length - 1] === '') stdout.pop();
}
return resolve(stdout);
});
if (input != null) child.stdin.end(input);
});
}
export function forceRunAsync(cmd, args, options) {
return runAsyncBase(cmd, args, options).catch((error) => {
if (error.message !== IGNORE) {
if (!error.messageOnly) {
console.error(error);
}
throw error;
}
});
};
export function runPromise(promise) {
return promise.catch((error) => {
if (error.message !== IGNORE) {
console.error(error);
}
exit();
});
};
export function runAsync(cmd, args, options) {
return runPromise(runAsyncBase(cmd, args, options));
};
export function runSync(cmd, args, options) {
if (cmd instanceof URL) {
cmd = fileURLToPath(cmd);
}
const opt = Object.assign({
cwd: process.cwd()
}, options);
if (isDebugVerbosity()) {
debuglog('[SpawnSync]', `${cmd} ${(args || []).join(' ')}`, opt);
}
const child = spawnSync(cmd, args, opt);
if (child.error) {
throw child.error;
} else if (child.status !== 0) {
throw new Error(`${cmd} ${args} failed with stderr: ` +
child.stderr.toString());
} else {
return child.stdout.toString();
}
};
export function exit() {
process.exit(1);
};