-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
index.js
184 lines (148 loc) · 4.57 KB
/
index.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
"use strict";
const os = require("os");
const chalk = require("chalk");
const execa = require("execa");
const logTransformer = require("strong-log-transformer");
// bookkeeping for spawned processes
/** @type {Set<import("execa").ExecaChildProcess<string>>} */
const children = new Set();
// when streaming processes are spawned, use this color for prefix
const colorWheel = ["cyan", "magenta", "blue", "yellow", "green", "red"];
const NUM_COLORS = colorWheel.length;
// ever-increasing index ensures colors are always sequential
let currentColor = 0;
/**
* Execute a command asynchronously, piping stdio by default.
* @param {string} command
* @param {string[]} args
* @param {import("execa").Options} [opts]
*/
function exec(command, args, opts) {
const options = Object.assign({ stdio: "pipe" }, opts);
const spawned = spawnProcess(command, args, options);
return wrapError(spawned);
}
/**
* Execute a command synchronously.
* @param {string} command
* @param {string[]} args
* @param {import("execa").SyncOptions} [opts]
*/
function execSync(command, args, opts) {
return execa.sync(command, args, opts).stdout;
}
/**
* Spawn a command asynchronously, _always_ inheriting stdio.
* @param {string} command
* @param {string[]} args
* @param {import("execa").Options} [opts]
*/
function spawn(command, args, opts) {
const options = Object.assign({}, opts, { stdio: "inherit" });
const spawned = spawnProcess(command, args, options);
return wrapError(spawned);
}
/**
* Spawn a command asynchronously, streaming stdio with optional prefix.
* @param {string} command
* @param {string[]} args
* @param {import("execa").Options} [opts]
* @param {string} [prefix]
*/
// istanbul ignore next
function spawnStreaming(command, args, opts, prefix) {
const options = Object.assign({}, opts);
options.stdio = ["ignore", "pipe", "pipe"];
const spawned = spawnProcess(command, args, options);
const stdoutOpts = {};
const stderrOpts = {}; // mergeMultiline causes escaped newlines :P
if (prefix) {
const colorName = colorWheel[currentColor % NUM_COLORS];
const color = chalk[colorName];
currentColor += 1;
stdoutOpts.tag = `${color.bold(prefix)}:`;
stderrOpts.tag = `${color(prefix)}:`;
}
// Avoid "Possible EventEmitter memory leak detected" warning due to piped stdio
if (children.size > process.stdout.listenerCount("close")) {
process.stdout.setMaxListeners(children.size);
process.stderr.setMaxListeners(children.size);
}
spawned.stdout.pipe(logTransformer(stdoutOpts)).pipe(process.stdout);
spawned.stderr.pipe(logTransformer(stderrOpts)).pipe(process.stderr);
return wrapError(spawned);
}
function getChildProcessCount() {
return children.size;
}
/**
* @param {import("execa").ExecaError<string>} result
* @returns {number}
*/
function getExitCode(result) {
if (result.exitCode) {
return result.exitCode;
}
// https://nodejs.org/docs/latest-v6.x/api/child_process.html#child_process_event_close
if (typeof result.code === "number") {
return result.code;
}
// https://nodejs.org/docs/latest-v6.x/api/errors.html#errors_error_code
if (typeof result.code === "string") {
return os.constants.errno[result.code];
}
// we tried
return process.exitCode;
}
/**
* @param {string} command
* @param {string[]} args
* @param {import("execa").Options} opts
*/
function spawnProcess(command, args, opts) {
const child = execa(command, args, opts);
const drain = (exitCode, signal) => {
children.delete(child);
// don't run repeatedly if this is the error event
if (signal === undefined) {
child.removeListener("exit", drain);
}
// propagate exit code, if any
if (exitCode) {
process.exitCode = exitCode;
}
};
child.once("exit", drain);
child.once("error", drain);
if (opts.pkg) {
child.pkg = opts.pkg;
}
children.add(child);
return child;
}
/**
* @param {import("execa").ExecaChildProcess<string> & { pkg?: import("@lerna/package").Package }} spawned
*/
function wrapError(spawned) {
if (spawned.pkg) {
return spawned.catch((err) => {
// ensure exit code is always a number
err.exitCode = getExitCode(err);
// log non-lerna error cleanly
err.pkg = spawned.pkg;
throw err;
});
}
return spawned;
}
exports.exec = exec;
exports.execSync = execSync;
exports.spawn = spawn;
exports.spawnStreaming = spawnStreaming;
exports.getChildProcessCount = getChildProcessCount;
exports.getExitCode = getExitCode;
/**
* @typedef {object} ExecOpts Provided to any execa-based call
* @property {string} cwd
* @property {number} [maxBuffer]
*/