-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathrun-module-as-observable-fork.js
77 lines (77 loc) · 2.95 KB
/
run-module-as-observable-fork.js
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"use strict";
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.runModuleAsObservableFork = runModuleAsObservableFork;
const node_child_process_1 = require("node:child_process");
const node_path_1 = require("node:path");
const rxjs_1 = require("rxjs");
const tree_kill_1 = __importDefault(require("tree-kill"));
function runModuleAsObservableFork(cwd, modulePath, exportName,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
args) {
return new rxjs_1.Observable((obs) => {
const workerPath = (0, node_path_1.resolve)(__dirname, './run-module-worker.js');
const debugArgRegex = /--inspect(?:-brk|-port)?|--debug(?:-brk|-port)/;
const execArgv = process.execArgv.filter((arg) => {
// Remove debug args.
// Workaround for https://github.com/nodejs/node/issues/9435
return !debugArgRegex.test(arg);
});
const forkOptions = {
cwd,
execArgv,
};
// TODO: support passing in a logger to use as stdio streams
// if (logger) {
// (forkOptions as any).stdio = [
// 'ignore',
// logger.info, // make it a stream
// logger.error, // make it a stream
// ];
// }
const forkedProcess = (0, node_child_process_1.fork)(workerPath, undefined, forkOptions);
// Cleanup.
const killForkedProcess = () => {
if (forkedProcess && forkedProcess.pid) {
(0, tree_kill_1.default)(forkedProcess.pid, 'SIGTERM');
}
};
// Handle child process exit.
const handleChildProcessExit = (code) => {
killForkedProcess();
if (code && code !== 0) {
obs.error();
}
obs.next({ success: true });
obs.complete();
};
forkedProcess.once('exit', handleChildProcessExit);
forkedProcess.once('SIGINT', handleChildProcessExit);
forkedProcess.once('uncaughtException', handleChildProcessExit);
// Handle parent process exit.
const handleParentProcessExit = () => {
killForkedProcess();
};
process.once('exit', handleParentProcessExit);
process.once('SIGINT', handleParentProcessExit);
process.once('uncaughtException', handleParentProcessExit);
// Run module.
forkedProcess.send({
hash: '5d4b9a5c0a4e0f9977598437b0e85bcc',
modulePath,
exportName,
args,
});
// Teardown logic. When unsubscribing, kill the forked process.
return killForkedProcess;
});
}