[6a3a178] | 1 | "use strict";
|
---|
| 2 | /**
|
---|
| 3 | * @license
|
---|
| 4 | * Copyright Google LLC All Rights Reserved.
|
---|
| 5 | *
|
---|
| 6 | * Use of this source code is governed by an MIT-style license that can be
|
---|
| 7 | * found in the LICENSE file at https://angular.io/license
|
---|
| 8 | */
|
---|
| 9 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
| 10 | exports.runModuleAsObservableFork = void 0;
|
---|
| 11 | const child_process_1 = require("child_process");
|
---|
| 12 | const path_1 = require("path");
|
---|
| 13 | const rxjs_1 = require("rxjs");
|
---|
| 14 | const treeKill = require('tree-kill');
|
---|
| 15 | function runModuleAsObservableFork(cwd, modulePath, exportName,
|
---|
| 16 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
---|
| 17 | args) {
|
---|
| 18 | return new rxjs_1.Observable((obs) => {
|
---|
| 19 | const workerPath = path_1.resolve(__dirname, './run-module-worker.js');
|
---|
| 20 | const debugArgRegex = /--inspect(?:-brk|-port)?|--debug(?:-brk|-port)/;
|
---|
| 21 | const execArgv = process.execArgv.filter((arg) => {
|
---|
| 22 | // Remove debug args.
|
---|
| 23 | // Workaround for https://github.com/nodejs/node/issues/9435
|
---|
| 24 | return !debugArgRegex.test(arg);
|
---|
| 25 | });
|
---|
| 26 | const forkOptions = {
|
---|
| 27 | cwd,
|
---|
| 28 | execArgv,
|
---|
| 29 | };
|
---|
| 30 | // TODO: support passing in a logger to use as stdio streams
|
---|
| 31 | // if (logger) {
|
---|
| 32 | // (forkOptions as any).stdio = [
|
---|
| 33 | // 'ignore',
|
---|
| 34 | // logger.info, // make it a stream
|
---|
| 35 | // logger.error, // make it a stream
|
---|
| 36 | // ];
|
---|
| 37 | // }
|
---|
| 38 | const forkedProcess = child_process_1.fork(workerPath, undefined, forkOptions);
|
---|
| 39 | // Cleanup.
|
---|
| 40 | const killForkedProcess = () => {
|
---|
| 41 | if (forkedProcess && forkedProcess.pid) {
|
---|
| 42 | treeKill(forkedProcess.pid, 'SIGTERM');
|
---|
| 43 | }
|
---|
| 44 | };
|
---|
| 45 | // Handle child process exit.
|
---|
| 46 | const handleChildProcessExit = (code) => {
|
---|
| 47 | killForkedProcess();
|
---|
| 48 | if (code && code !== 0) {
|
---|
| 49 | obs.error();
|
---|
| 50 | }
|
---|
| 51 | obs.next({ success: true });
|
---|
| 52 | obs.complete();
|
---|
| 53 | };
|
---|
| 54 | forkedProcess.once('exit', handleChildProcessExit);
|
---|
| 55 | forkedProcess.once('SIGINT', handleChildProcessExit);
|
---|
| 56 | forkedProcess.once('uncaughtException', handleChildProcessExit);
|
---|
| 57 | // Handle parent process exit.
|
---|
| 58 | const handleParentProcessExit = () => {
|
---|
| 59 | killForkedProcess();
|
---|
| 60 | };
|
---|
| 61 | process.once('exit', handleParentProcessExit);
|
---|
| 62 | process.once('SIGINT', handleParentProcessExit);
|
---|
| 63 | process.once('uncaughtException', handleParentProcessExit);
|
---|
| 64 | // Run module.
|
---|
| 65 | forkedProcess.send({
|
---|
| 66 | hash: '5d4b9a5c0a4e0f9977598437b0e85bcc',
|
---|
| 67 | modulePath,
|
---|
| 68 | exportName,
|
---|
| 69 | args,
|
---|
| 70 | });
|
---|
| 71 | // Teardown logic. When unsubscribing, kill the forked process.
|
---|
| 72 | return killForkedProcess;
|
---|
| 73 | });
|
---|
| 74 | }
|
---|
| 75 | exports.runModuleAsObservableFork = runModuleAsObservableFork;
|
---|