-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathcp.js
More file actions
32 lines (24 loc) · 839 Bytes
/
cp.js
File metadata and controls
32 lines (24 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
27
28
29
30
31
32
import { spawn } from 'node:child_process';
import { stdin, stdout } from 'node:process';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const spawnChildProcess = async (args) => {
return new Promise((resolve, reject) => {
const scriptPath = path.join(__dirname, 'files', 'script.js');
const child = spawn('node', [scriptPath, ...args], {
stdio: ['pipe', 'pipe', 'inherit'],
});
stdin.pipe(child.stdin);
child.stdout.pipe(stdout);
child.on('close', (code) => {
resolve(`Child process exited with code ${code}`);
});
child.on('error', (err) => {
reject(err);
});
});
};
// Пример вызова для теста
spawnChildProcess(['arg1', 'arg2']);