-
-
Notifications
You must be signed in to change notification settings - Fork 550
Expand file tree
/
Copy pathspawn-child.ts
More file actions
50 lines (48 loc) · 1.56 KB
/
spawn-child.ts
File metadata and controls
50 lines (48 loc) · 1.56 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import type { BootstrapState } from '../bin';
import { spawn } from 'child_process';
import { pathToFileURL } from 'url';
import { argPrefix, compress } from './argv-payload';
/**
* @internal
* @param state Bootstrap state to be transferred into the child process.
* @param targetCwd Working directory to be preserved when transitioning to
* the child process.
*/
export function callInChild(state: BootstrapState) {
const loaderURL = pathToFileURL(require.resolve('../../child-loader.mjs'));
const compressedState = compress(state);
loaderURL.searchParams.set(argPrefix, compressedState);
const child = spawn(
process.execPath,
[
'--require',
require.resolve('./child-require.js'),
'--loader',
// Node on Windows doesn't like `c:\` absolute paths here; must be `file:///c:/`
loaderURL.toString(),
require.resolve('./child-entrypoint.js'),
`${argPrefix}${compressedState}`,
...state.parseArgvResult.restArgs,
],
{
stdio: 'inherit',
argv0: process.argv0,
}
);
child.on('error', (error) => {
console.error(error);
process.exit(1);
});
child.on('exit', (code) => {
child.removeAllListeners();
process.off('SIGINT', sendSignalToChild);
process.off('SIGTERM', sendSignalToChild);
process.exitCode = code === null ? 1 : code;
});
// Ignore sigint and sigterm in parent; pass them to child
process.on('SIGINT', sendSignalToChild);
process.on('SIGTERM', sendSignalToChild);
function sendSignalToChild(signal: string) {
process.kill(child.pid, signal);
}
}