in-parallel-lit 0.4.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +0 -0
- package/README.md +0 -12
- package/dist/bin.cjs +413 -0
- package/dist/bin.js +411 -0
- package/dist/index.d.ts +16 -0
- package/package.json +27 -17
- package/src/bin.js +0 -42
- package/src/index.js +0 -201
- package/src/lib/get-signal-num.js +0 -32
- package/src/lib/get-stream-kind.js +0 -13
- package/src/lib/in-parallel-error.js +0 -8
- package/src/lib/kill-pids.js +0 -110
- package/src/lib/memory-writable.js +0 -69
- package/src/lib/prefix-transform.js +0 -33
- package/src/lib/remove-from-arr.js +0 -11
- package/src/lib/select-color.js +0 -67
- package/src/lib/spawn.js +0 -20
- package/src/lib/wrap-stream-with-label.js +0 -20
package/LICENSE
CHANGED
|
File without changes
|
package/README.md
CHANGED
|
@@ -2,16 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
A CLI tool to run multiple processes in parallel.
|
|
4
4
|
|
|
5
|
-
## Requirements
|
|
6
|
-
|
|
7
|
-
- [Node v16+][install-node]
|
|
8
|
-
|
|
9
5
|
## Installation
|
|
10
6
|
|
|
11
7
|
```bash
|
|
12
8
|
$ npm i -g in-parallel-lit
|
|
13
|
-
# or
|
|
14
|
-
$ yarn global add in-parallel-lit
|
|
15
9
|
```
|
|
16
10
|
|
|
17
11
|
## Usage
|
|
@@ -155,9 +149,3 @@ $ ./Taskfile.sh validate
|
|
|
155
149
|
|
|
156
150
|
(3) Start developing. See [`./Taskfile.sh`](./Taskfile.sh) for more tasks to
|
|
157
151
|
help you develop.
|
|
158
|
-
|
|
159
|
-
---
|
|
160
|
-
|
|
161
|
-
_This project was set up by @jvdx/core_
|
|
162
|
-
|
|
163
|
-
[install-node]: https://github.com/nvm-sh/nvm
|
package/dist/bin.cjs
ADDED
|
@@ -0,0 +1,413 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
5
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
6
|
+
const node_module = require("node:module");
|
|
7
|
+
const sade = require("sade");
|
|
8
|
+
const node_stream = require("node:stream");
|
|
9
|
+
const node_string_decoder = require("node:string_decoder");
|
|
10
|
+
const crossSpawn = require("cross-spawn");
|
|
11
|
+
const os = require("node:os");
|
|
12
|
+
var _documentCurrentScript = typeof document !== "undefined" ? document.currentScript : null;
|
|
13
|
+
const signals = {
|
|
14
|
+
SIGABRT: 6,
|
|
15
|
+
SIGALRM: 14,
|
|
16
|
+
SIGBUS: 10,
|
|
17
|
+
SIGCHLD: 20,
|
|
18
|
+
SIGCONT: 19,
|
|
19
|
+
SIGFPE: 8,
|
|
20
|
+
SIGHUP: 1,
|
|
21
|
+
SIGILL: 4,
|
|
22
|
+
SIGINT: 2,
|
|
23
|
+
SIGKILL: 9,
|
|
24
|
+
SIGPIPE: 13,
|
|
25
|
+
SIGQUIT: 3,
|
|
26
|
+
SIGSEGV: 11,
|
|
27
|
+
SIGSTOP: 17,
|
|
28
|
+
SIGTERM: 15,
|
|
29
|
+
SIGTRAP: 5,
|
|
30
|
+
SIGTSTP: 18,
|
|
31
|
+
SIGTTIN: 21,
|
|
32
|
+
SIGTTOU: 22,
|
|
33
|
+
SIGUSR1: 30,
|
|
34
|
+
SIGUSR2: 31
|
|
35
|
+
};
|
|
36
|
+
function getSignalNumber(signal) {
|
|
37
|
+
return signals[signal] || 0;
|
|
38
|
+
}
|
|
39
|
+
function getStreamKind(stream, std) {
|
|
40
|
+
if (stream == null) return "ignore";
|
|
41
|
+
if (stream !== std || !std.isTTY) return "pipe";
|
|
42
|
+
return stream;
|
|
43
|
+
}
|
|
44
|
+
class InParallelError extends Error {
|
|
45
|
+
constructor(causeResult, allResults) {
|
|
46
|
+
super(`"${causeResult.name}" exited with ${causeResult.code}.`);
|
|
47
|
+
__publicField(this, "code");
|
|
48
|
+
__publicField(this, "results");
|
|
49
|
+
this.name = causeResult.name;
|
|
50
|
+
this.code = causeResult.code;
|
|
51
|
+
this.results = allResults;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const noop = () => {
|
|
55
|
+
};
|
|
56
|
+
class MemoryWritable extends node_stream.Writable {
|
|
57
|
+
constructor(data = null) {
|
|
58
|
+
super();
|
|
59
|
+
__publicField(this, "queue", []);
|
|
60
|
+
__publicField(this, "data");
|
|
61
|
+
this.data = Array.isArray(data) ? data : [data];
|
|
62
|
+
this.queue = [];
|
|
63
|
+
for (let chunk of this.data) {
|
|
64
|
+
if (chunk == null) continue;
|
|
65
|
+
if (!(chunk instanceof Buffer)) {
|
|
66
|
+
chunk = Buffer.from(chunk);
|
|
67
|
+
}
|
|
68
|
+
this.queue.push(chunk);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
_write(chunk, enc, cb = noop) {
|
|
72
|
+
let decoder = null;
|
|
73
|
+
try {
|
|
74
|
+
decoder = enc && enc !== "buffer" ? new node_string_decoder.StringDecoder(enc) : null;
|
|
75
|
+
} catch (err) {
|
|
76
|
+
return cb(err);
|
|
77
|
+
}
|
|
78
|
+
let decodedChunk = decoder != null ? decoder.write(chunk) : chunk;
|
|
79
|
+
this.queue.push(
|
|
80
|
+
Buffer.isBuffer(decodedChunk) ? decodedChunk : Buffer.from(decodedChunk)
|
|
81
|
+
);
|
|
82
|
+
cb();
|
|
83
|
+
}
|
|
84
|
+
_getQueueSize() {
|
|
85
|
+
let size = 0;
|
|
86
|
+
for (let i = 0; i < this.queue.length; i++) {
|
|
87
|
+
size += this.queue[i].length;
|
|
88
|
+
}
|
|
89
|
+
return size;
|
|
90
|
+
}
|
|
91
|
+
toString() {
|
|
92
|
+
let str = "";
|
|
93
|
+
for (const chunk of this.queue) {
|
|
94
|
+
str += chunk.toString();
|
|
95
|
+
}
|
|
96
|
+
return str;
|
|
97
|
+
}
|
|
98
|
+
toBuffer() {
|
|
99
|
+
const buffer = Buffer.alloc(this._getQueueSize());
|
|
100
|
+
let currentOffset = 0;
|
|
101
|
+
for (const chunk of this.queue) {
|
|
102
|
+
chunk.copy(buffer, currentOffset);
|
|
103
|
+
currentOffset += chunk.length;
|
|
104
|
+
}
|
|
105
|
+
return buffer;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
function removeFromArr(arr, item) {
|
|
109
|
+
const index = arr.indexOf(item);
|
|
110
|
+
if (index !== -1) {
|
|
111
|
+
arr.splice(index, 1);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
function crossSpawnPromise(cmd, args = [], options = {}) {
|
|
115
|
+
return new Promise((resolve, reject) => {
|
|
116
|
+
let stdout = "";
|
|
117
|
+
let stderr = "";
|
|
118
|
+
const ch = crossSpawn.spawn(cmd, args, options);
|
|
119
|
+
if (ch.stdout === null || ch.stderr === null) {
|
|
120
|
+
return reject("stdout/stderr is null");
|
|
121
|
+
}
|
|
122
|
+
ch.stdout.on("data", (d) => {
|
|
123
|
+
stdout += d.toString();
|
|
124
|
+
});
|
|
125
|
+
ch.stderr.on("data", (d) => {
|
|
126
|
+
stderr += d.toString();
|
|
127
|
+
});
|
|
128
|
+
ch.on("error", (err) => reject(err));
|
|
129
|
+
ch.on("close", (code) => {
|
|
130
|
+
if (stderr) return reject(stderr);
|
|
131
|
+
if (code !== 0) return reject(`${cmd} exited with code ${code}`);
|
|
132
|
+
return resolve(stdout);
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
async function killPids(pid, platform = process.platform) {
|
|
137
|
+
if (platform === "win32") {
|
|
138
|
+
crossSpawn.spawn("taskkill", ["/F", "/T", "/PID", String(pid)]);
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
try {
|
|
142
|
+
let stdout = await crossSpawnPromise("ps", ["-A", "-o", "ppid,pid"]);
|
|
143
|
+
let stdoutRows = stdout.split(os.EOL);
|
|
144
|
+
let pidExists = false;
|
|
145
|
+
const pidTree = {};
|
|
146
|
+
for (let i = 1; i < stdoutRows.length; i++) {
|
|
147
|
+
stdoutRows[i] = stdoutRows[i].trim();
|
|
148
|
+
if (!stdoutRows[i]) continue;
|
|
149
|
+
let stdoutTuple = stdoutRows[i].split(/\s+/);
|
|
150
|
+
let stdoutPpid = parseInt(stdoutTuple[0], 10);
|
|
151
|
+
let stdoutPid = parseInt(stdoutTuple[1], 10);
|
|
152
|
+
if (!pidExists && stdoutPid === pid || !pidExists && stdoutPpid === pid) {
|
|
153
|
+
pidExists = true;
|
|
154
|
+
}
|
|
155
|
+
if (pidTree[stdoutPpid]) {
|
|
156
|
+
pidTree[stdoutPpid].push(stdoutPid);
|
|
157
|
+
} else {
|
|
158
|
+
pidTree[stdoutPpid] = [stdoutPid];
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
if (!pidExists) return;
|
|
162
|
+
let idx = 0;
|
|
163
|
+
const pids = [pid];
|
|
164
|
+
while (idx < pids.length) {
|
|
165
|
+
let curpid = pids[idx++];
|
|
166
|
+
if (!pidTree[curpid]) continue;
|
|
167
|
+
for (let j = 0; j < pidTree[curpid].length; j++) {
|
|
168
|
+
pids.push(pidTree[curpid][j]);
|
|
169
|
+
}
|
|
170
|
+
delete pidTree[curpid];
|
|
171
|
+
}
|
|
172
|
+
for (const pid2 of pids) {
|
|
173
|
+
process.kill(pid2);
|
|
174
|
+
}
|
|
175
|
+
} catch (err) {
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
function spawn(command, args, options) {
|
|
179
|
+
const child = crossSpawn.spawn(command, args, options);
|
|
180
|
+
child.kill = function kill() {
|
|
181
|
+
killPids(this.pid);
|
|
182
|
+
return true;
|
|
183
|
+
};
|
|
184
|
+
return child;
|
|
185
|
+
}
|
|
186
|
+
let FORCE_COLOR;
|
|
187
|
+
let NODE_DISABLE_COLORS;
|
|
188
|
+
let NO_COLOR;
|
|
189
|
+
let TERM;
|
|
190
|
+
let isTTY = true;
|
|
191
|
+
let enabled;
|
|
192
|
+
function checkColorSupport() {
|
|
193
|
+
if (enabled != null) return;
|
|
194
|
+
({ FORCE_COLOR, NODE_DISABLE_COLORS, NO_COLOR, TERM } = process.env);
|
|
195
|
+
isTTY = process.stdout && process.stdout.isTTY;
|
|
196
|
+
enabled = !NODE_DISABLE_COLORS && NO_COLOR == null && TERM !== "dumb" && (FORCE_COLOR != null && FORCE_COLOR !== "0" || isTTY);
|
|
197
|
+
}
|
|
198
|
+
function createColor(x, y) {
|
|
199
|
+
const rgx = new RegExp(`\\x1b\\[${y}m`, "g");
|
|
200
|
+
const open = `\x1B[${x}m`;
|
|
201
|
+
const close = `\x1B[${y}m`;
|
|
202
|
+
return function(txt) {
|
|
203
|
+
checkColorSupport();
|
|
204
|
+
if (!enabled) return txt;
|
|
205
|
+
return open + (~("" + txt).indexOf(close) ? txt.replace(rgx, close + open) : txt) + close;
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
const colors = [
|
|
209
|
+
createColor(36, 39),
|
|
210
|
+
// cyan
|
|
211
|
+
createColor(32, 39),
|
|
212
|
+
// green
|
|
213
|
+
createColor(35, 39),
|
|
214
|
+
// magenta
|
|
215
|
+
createColor(33, 39),
|
|
216
|
+
// yellow
|
|
217
|
+
createColor(31, 39)
|
|
218
|
+
// red
|
|
219
|
+
];
|
|
220
|
+
let colorIndex = 0;
|
|
221
|
+
const taskNamesToColors = /* @__PURE__ */ new Map();
|
|
222
|
+
function selectColor(taskName) {
|
|
223
|
+
let color = taskNamesToColors.get(taskName);
|
|
224
|
+
if (color == null) {
|
|
225
|
+
color = colors[colorIndex];
|
|
226
|
+
colorIndex = (colorIndex + 1) % colors.length;
|
|
227
|
+
taskNamesToColors.set(taskName, color);
|
|
228
|
+
}
|
|
229
|
+
return color;
|
|
230
|
+
}
|
|
231
|
+
const ALL_BR = /\n/g;
|
|
232
|
+
class PrefixTransform extends node_stream.Transform {
|
|
233
|
+
constructor(prefix) {
|
|
234
|
+
super();
|
|
235
|
+
__publicField(this, "prefix");
|
|
236
|
+
__publicField(this, "lastPrefix");
|
|
237
|
+
__publicField(this, "lastIsLinebreak");
|
|
238
|
+
this.prefix = prefix;
|
|
239
|
+
this.lastPrefix = null;
|
|
240
|
+
this.lastIsLinebreak = true;
|
|
241
|
+
}
|
|
242
|
+
_transform(chunk, _enc, cb) {
|
|
243
|
+
const firstPrefix = this.lastIsLinebreak ? this.prefix : this.lastPrefix !== this.prefix ? "\n" : "";
|
|
244
|
+
const prefixed = `${firstPrefix}${chunk}`.replace(
|
|
245
|
+
ALL_BR,
|
|
246
|
+
`
|
|
247
|
+
${this.prefix}`
|
|
248
|
+
);
|
|
249
|
+
const index = prefixed.indexOf(
|
|
250
|
+
this.prefix,
|
|
251
|
+
Math.max(0, prefixed.length - this.prefix.length)
|
|
252
|
+
);
|
|
253
|
+
this.lastPrefix = this.prefix;
|
|
254
|
+
this.lastIsLinebreak = index !== -1;
|
|
255
|
+
cb(null, index !== -1 ? prefixed.slice(0, index) : prefixed);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
function wrapStreamWithLabel(source, label) {
|
|
259
|
+
if (source == null) return source;
|
|
260
|
+
const color = source.isTTY ? selectColor(label) : (x) => x;
|
|
261
|
+
const prefix = color(`[${label}] `);
|
|
262
|
+
const target = new PrefixTransform(prefix);
|
|
263
|
+
target.pipe(source);
|
|
264
|
+
return target;
|
|
265
|
+
}
|
|
266
|
+
function prog(opts, proc) {
|
|
267
|
+
const { _: tasks, ...options } = opts;
|
|
268
|
+
const customTaskNames = options.names != null ? options.names.split(",").map((n) => n.trim()) : [];
|
|
269
|
+
return new Promise((resolve, reject) => {
|
|
270
|
+
let results = [];
|
|
271
|
+
let queue = [];
|
|
272
|
+
let promises = [];
|
|
273
|
+
let error = null;
|
|
274
|
+
let aborted = false;
|
|
275
|
+
if (tasks.length === 0) return done();
|
|
276
|
+
for (let i = 0; i < tasks.length; i++) {
|
|
277
|
+
results.push({ name: tasks[i], code: void 0 });
|
|
278
|
+
queue.push({ name: tasks[i], index: i });
|
|
279
|
+
}
|
|
280
|
+
function done() {
|
|
281
|
+
if (error == null) return resolve(results);
|
|
282
|
+
return reject(error);
|
|
283
|
+
}
|
|
284
|
+
function abort() {
|
|
285
|
+
if (aborted) return;
|
|
286
|
+
aborted = true;
|
|
287
|
+
if (promises.length === 0) return done();
|
|
288
|
+
for (const p of promises) {
|
|
289
|
+
p.abort();
|
|
290
|
+
}
|
|
291
|
+
return Promise.all(promises).then(done, reject);
|
|
292
|
+
}
|
|
293
|
+
function next() {
|
|
294
|
+
if (aborted) return;
|
|
295
|
+
if (queue.length === 0) {
|
|
296
|
+
if (promises.length === 0) return done();
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
const task = queue.shift();
|
|
300
|
+
if (task == null) return;
|
|
301
|
+
const originalOutputStream = proc.stdout;
|
|
302
|
+
const optionsClone = {
|
|
303
|
+
stdout: proc.stdout,
|
|
304
|
+
stderr: proc.stderr,
|
|
305
|
+
stdin: proc.stdin,
|
|
306
|
+
customName: customTaskNames[task.index]
|
|
307
|
+
};
|
|
308
|
+
const writer = new MemoryWritable();
|
|
309
|
+
if (options["aggregate-output"]) {
|
|
310
|
+
optionsClone.stdout = writer;
|
|
311
|
+
}
|
|
312
|
+
const promise = runTask(task.name, optionsClone);
|
|
313
|
+
promises.push(promise);
|
|
314
|
+
promise.then(
|
|
315
|
+
(result) => {
|
|
316
|
+
removeFromArr(promises, promise);
|
|
317
|
+
if (aborted) return;
|
|
318
|
+
if (options["aggregate-output"]) {
|
|
319
|
+
originalOutputStream.write(writer.toString());
|
|
320
|
+
}
|
|
321
|
+
if (result.code === null && result.signal !== null) {
|
|
322
|
+
result.code = 128 + getSignalNumber(result.signal);
|
|
323
|
+
}
|
|
324
|
+
results[task.index].code = result.code;
|
|
325
|
+
if (result.code) {
|
|
326
|
+
error = new InParallelError(result, results);
|
|
327
|
+
if (!options["continue-on-error"]) {
|
|
328
|
+
return abort();
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
next();
|
|
332
|
+
},
|
|
333
|
+
(err) => {
|
|
334
|
+
removeFromArr(promises, promise);
|
|
335
|
+
if (!options["continue-on-error"]) {
|
|
336
|
+
error = err;
|
|
337
|
+
return abort();
|
|
338
|
+
}
|
|
339
|
+
next();
|
|
340
|
+
}
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
let end = tasks.length;
|
|
344
|
+
if (typeof options["max-parallel"] === "number" && options["max-parallel"] > 0) {
|
|
345
|
+
end = Math.min(tasks.length, options["max-parallel"]);
|
|
346
|
+
}
|
|
347
|
+
for (let i = 0; i < end; ++i) {
|
|
348
|
+
next();
|
|
349
|
+
}
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
function runTask(name, opts) {
|
|
353
|
+
let proc = null;
|
|
354
|
+
const task = new Promise((resolve, reject) => {
|
|
355
|
+
const stdin = opts.stdin;
|
|
356
|
+
const stdout = wrapStreamWithLabel(opts.stdout, opts.customName || name);
|
|
357
|
+
const stderr = wrapStreamWithLabel(opts.stderr, opts.customName || name);
|
|
358
|
+
const stdinKind = getStreamKind(stdin, process.stdin);
|
|
359
|
+
const stdoutKind = getStreamKind(stdout, process.stdout);
|
|
360
|
+
const stderrKind = getStreamKind(stderr, process.stderr);
|
|
361
|
+
const [spawnName, ...spawnArgs] = name.split(" ");
|
|
362
|
+
proc = spawn(spawnName, spawnArgs, {
|
|
363
|
+
stdio: [stdinKind, stdoutKind, stderrKind]
|
|
364
|
+
});
|
|
365
|
+
if (proc == null) return reject("Failed to spawn process");
|
|
366
|
+
if (stdinKind === "pipe") {
|
|
367
|
+
stdin.pipe(proc.stdin);
|
|
368
|
+
}
|
|
369
|
+
if (stdoutKind === "pipe") {
|
|
370
|
+
proc.stdout.pipe(stdout, { end: false });
|
|
371
|
+
}
|
|
372
|
+
if (stderrKind === "pipe") {
|
|
373
|
+
proc.stderr.pipe(stderr, { end: false });
|
|
374
|
+
}
|
|
375
|
+
proc.on("error", (err) => {
|
|
376
|
+
proc = null;
|
|
377
|
+
return reject(err);
|
|
378
|
+
});
|
|
379
|
+
proc.on("close", (code, signal) => {
|
|
380
|
+
proc = null;
|
|
381
|
+
return resolve({ name, code, signal });
|
|
382
|
+
});
|
|
383
|
+
});
|
|
384
|
+
task.abort = () => {
|
|
385
|
+
if (proc == null) return;
|
|
386
|
+
proc.kill();
|
|
387
|
+
proc = null;
|
|
388
|
+
};
|
|
389
|
+
return task;
|
|
390
|
+
}
|
|
391
|
+
async function run(argv) {
|
|
392
|
+
const require$1 = node_module.createRequire(typeof document === "undefined" ? require("url").pathToFileURL(__filename).href : _documentCurrentScript && _documentCurrentScript.src || new URL("bin.cjs", document.baseURI).href);
|
|
393
|
+
const packageJson = require$1("../package.json");
|
|
394
|
+
process.stdout.setMaxListeners(0);
|
|
395
|
+
process.stderr.setMaxListeners(0);
|
|
396
|
+
process.stdin.setMaxListeners(0);
|
|
397
|
+
sade("in-parallel", true).version(packageJson.version).describe(packageJson.description).option(
|
|
398
|
+
`-n, --names`,
|
|
399
|
+
`List of custom names to be used in prefix template.`
|
|
400
|
+
).example(`-n "first,second" "ping google.com" "ping 172.0.0.1"`).option(
|
|
401
|
+
`-c, --continue-on-error`,
|
|
402
|
+
`Set the flag to continue executing other/subsequent tasks even if a task threw an error. 'in-parallel' itself will exit with non-zero code if one or more tasks threw error(s).`
|
|
403
|
+
).option(
|
|
404
|
+
`--max-parallel`,
|
|
405
|
+
`Set the maximum number of parallelism. Default is unlimited.`,
|
|
406
|
+
0
|
|
407
|
+
).option(
|
|
408
|
+
`--aggregate-output`,
|
|
409
|
+
`Avoid interleaving output by delaying printing of each command's output until it has finished.`,
|
|
410
|
+
false
|
|
411
|
+
).action((opts) => prog(opts, process)).parse(argv);
|
|
412
|
+
}
|
|
413
|
+
run(process.argv);
|