-
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) · 1.29 KB
/
cp.js
File metadata and controls
32 lines (24 loc) · 1.29 KB
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 'child_process';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// ДЛЯ ЦЕЛЕЙ ТЕСТИРОВАНИЯ РАБОТЫ ПРИЛОЖЕНИЯ СРАЗУ ПОСЛЕ СТАРТА ВВЕСТИ ЛЮБУЮ СТРОКУ
// И НАЖАТЬ ЕНТЕР. ЧТОБЫ ВЫЙТИЁ ИЗ ПРОЦЕССА ВВЕСЬТИ CLOSE именно большими буквами
const spawnChildProcess = async (args) => {
// Путь к script.js в папке files
const scriptPath = join(__dirname, 'files', 'script.js');
const childProcess = spawn('node', [scriptPath, ...args], {
stdio: ['pipe', 'pipe', 'inherit']
});
// Связываем stdin и stdout родительского и дочернего процессов
process.stdin.pipe(childProcess.stdin);
childProcess.stdout.pipe(process.stdout);
// Обработка завершения дочернего процесса
childProcess.on('exit', (code) => {
console.log(`\nChild process exited with code ${code}`);
});
return childProcess;
};
// Тестируем с аргументами
spawnChildProcess(['argument1', 'argument2', 'argument3', 'argument4']);