vovk 1.1.2 → 1.1.3-beta.1
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/cli/lib/parallel.cjs +41 -39
- package/package.json +1 -1
package/cli/lib/parallel.cjs
CHANGED
|
@@ -1,52 +1,54 @@
|
|
|
1
|
-
// @ts-check
|
|
2
1
|
const { spawn } = require('child_process');
|
|
3
2
|
|
|
4
|
-
/**
|
|
3
|
+
/**
|
|
4
|
+
* Execute multiple commands in parallel and return a promise.
|
|
5
|
+
* @param {Array} commands - An array of command objects { command: string, name: string, env: Object }
|
|
6
|
+
* @returns {Promise} - A promise that resolves when all commands have completed.
|
|
7
|
+
* @type {(commands: { command: string; name: string; env: import('../../src').VovkEnv }[]) => Promise<void>}
|
|
8
|
+
*/
|
|
5
9
|
function parallel(commands) {
|
|
6
10
|
return new Promise((resolve, reject) => {
|
|
7
|
-
|
|
8
|
-
let
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
(
|
|
18
|
-
|
|
11
|
+
let children = [];
|
|
12
|
+
let results = [];
|
|
13
|
+
|
|
14
|
+
// Helper function to handle closure of a child process
|
|
15
|
+
function childClose(index, resolve, reject) {
|
|
16
|
+
return (code) => {
|
|
17
|
+
code = code ? code.code || code : code;
|
|
18
|
+
if (code > 0) {
|
|
19
|
+
reject(new Error('`' + commands[index].name + '` failed with exit code ' + code));
|
|
20
|
+
} else {
|
|
21
|
+
resolve('`' + commands[index].name + '` ended successfully');
|
|
22
|
+
}
|
|
19
23
|
};
|
|
20
|
-
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
/** @type {(command: string, env: import('../../src').VovkEnv, onExit: (code: number) => void) => import('child_process').ChildProcess} */
|
|
24
|
-
function runCommand(command, env, onExit) {
|
|
25
|
-
const proc = spawn(command, { shell: true, detached: true, env: { ...env, ...process.env }, stdio: 'inherit' });
|
|
24
|
+
}
|
|
26
25
|
|
|
27
|
-
|
|
26
|
+
// Start each command as a child process
|
|
27
|
+
commands.forEach((command, index) => {
|
|
28
|
+
let cmd = command.command;
|
|
29
|
+
let sh = process.platform === 'win32' ? 'cmd' : 'sh';
|
|
30
|
+
let shFlag = process.platform === 'win32' ? '/c' : '-c';
|
|
28
31
|
|
|
29
|
-
|
|
30
|
-
|
|
32
|
+
let child = spawn(sh, [shFlag, cmd], {
|
|
33
|
+
cwd: process.cwd(),
|
|
34
|
+
env: { ...process.env, ...command.env },
|
|
35
|
+
stdio: ['pipe', 'inherit', 'inherit'], // Use 'inherit' to keep stdout and stderr
|
|
36
|
+
});
|
|
31
37
|
|
|
32
|
-
|
|
33
|
-
function handleProcessExit(code, name) {
|
|
34
|
-
processes = processes.filter((p) => p.name !== name);
|
|
38
|
+
children.push(child);
|
|
35
39
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
if (p.process.pid) {
|
|
40
|
-
process.kill(-p.process.pid, 'SIGTERM');
|
|
41
|
-
}
|
|
42
|
-
}
|
|
40
|
+
// Create a new promise for each child process
|
|
41
|
+
let childPromise = new Promise((childResolve, childReject) => {
|
|
42
|
+
child.on('close', childClose(index, childResolve, childReject));
|
|
43
43
|
});
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
44
|
+
|
|
45
|
+
results.push(childPromise);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
// Use Promise.all to wait for all child processes to complete
|
|
49
|
+
Promise.all(results)
|
|
50
|
+
.then(() => resolve('All processes have ended successfully'))
|
|
51
|
+
.catch((error) => reject(error));
|
|
50
52
|
});
|
|
51
53
|
}
|
|
52
54
|
|