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.
@@ -1,52 +1,54 @@
1
- // @ts-check
2
1
  const { spawn } = require('child_process');
3
2
 
4
- /** @type {(commands: { command: string; name: string; env: import('../../src').VovkEnv }[]) => Promise<void>} */
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
- /** @type {{ name: string; process: import('child_process').ChildProcess; }[]} */
8
- let processes = [];
9
-
10
- commands.forEach((cmd) => {
11
- const processObj = {
12
- name: cmd.name,
13
- process: runCommand(
14
- cmd.command,
15
- cmd.env,
16
- /** @type {(code: number) => void} */
17
- (code) => handleProcessExit(code, cmd.name)
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
- processes.push(processObj);
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
- proc.on('exit', onExit);
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
- return proc;
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
- /** @type {(code: number, name: string) => void} */
33
- function handleProcessExit(code, name) {
34
- processes = processes.filter((p) => p.name !== name);
38
+ children.push(child);
35
39
 
36
- processes.forEach((p) => {
37
- if (p.name !== name) {
38
- // TS fix
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
- processes = [];
45
- process.stdout.write('\n');
46
- if (code !== 0) {
47
- return reject(new Error(`Process ${name} exited with code ${code}`));
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
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vovk",
3
- "version": "1.1.2",
3
+ "version": "1.1.3-beta.1",
4
4
  "description": "REST for Next",
5
5
  "bin": "./cli/index.cjs",
6
6
  "scripts": {