-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathcp.js
More file actions
26 lines (21 loc) · 839 Bytes
/
cp.js
File metadata and controls
26 lines (21 loc) · 839 Bytes
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
import { spawn } from 'node:child_process';
import { fileURLToPath } from 'node:url';
export const spawnChildProcess = async (args = []) => {
const scriptPath = fileURLToPath(new URL('./files/script.js', import.meta.url));
const child = spawn(process.execPath, [scriptPath, ...args], {
stdio: ['pipe', 'pipe', 'inherit'],
});
process.stdin.pipe(child.stdin);
child.stdout.pipe(process.stdout);
return new Promise((resolve, reject) => {
child.once('error', reject);
child.once('exit', (code) => {
process.stdin.unpipe(child.stdin);
child.stdout.unpipe(process.stdout);
if (code === 0) resolve();
else reject(new Error(`Child exited with code ${code}`));
});
});
};
// Put your arguments in function call to test this functionality
spawnChildProcess( ["rs", "2025", "node.js"] );