-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
execute.impl.ts
176 lines (154 loc) · 4.39 KB
/
execute.impl.ts
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import 'dotenv/config';
import {
runExecutor,
stripIndents,
parseTargetString,
ExecutorContext,
logger,
readTargetOptions,
} from '@nrwl/devkit';
import { ChildProcess, fork } from 'child_process';
import { promisify } from 'util';
import * as treeKill from 'tree-kill';
import { NodeBuildEvent } from '../build/build.impl';
import { BuildNodeBuilderOptions } from '../../utils/types';
export const enum InspectType {
Inspect = 'inspect',
InspectBrk = 'inspect-brk',
}
export interface NodeExecuteBuilderOptions {
inspect: boolean | InspectType;
runtimeArgs: string[];
args: string[];
waitUntilTargets: string[];
buildTarget: string;
host: string;
port: number;
watch: boolean;
}
let subProcess: ChildProcess = null;
export async function* executeExecutor(
options: NodeExecuteBuilderOptions,
context: ExecutorContext
) {
process.on('SIGTERM', () => {
subProcess?.kill();
process.exit(128 + 15);
});
process.on('exit', (code) => {
process.exit(code);
});
if (options.waitUntilTargets && options.waitUntilTargets.length > 0) {
const results = await runWaitUntilTargets(options, context);
for (const [i, result] of results.entries()) {
if (!result.success) {
console.log('throw');
throw new Error(
`Wait until target failed: ${options.waitUntilTargets[i]}.`
);
}
}
}
for await (const event of startBuild(options, context)) {
if (!event.success) {
logger.error('There was an error with the build. See above.');
logger.info(`${event.outfile} was not restarted.`);
}
await handleBuildEvent(event, options);
yield event;
}
}
function runProcess(event: NodeBuildEvent, options: NodeExecuteBuilderOptions) {
if (subProcess || !event.success) {
return;
}
subProcess = fork(event.outfile, options.args, {
execArgv: getExecArgv(options),
});
}
function getExecArgv(options: NodeExecuteBuilderOptions) {
const args = ['-r', 'source-map-support/register', ...options.runtimeArgs];
if (options.inspect === true) {
options.inspect = InspectType.Inspect;
}
if (options.inspect) {
args.push(`--${options.inspect}=${options.host}:${options.port}`);
}
return args;
}
async function handleBuildEvent(
event: NodeBuildEvent,
options: NodeExecuteBuilderOptions
) {
if ((!event.success || options.watch) && subProcess) {
await killProcess();
}
runProcess(event, options);
}
async function killProcess() {
if (!subProcess) {
return;
}
const promisifiedTreeKill: (pid: number, signal: string) => Promise<void> =
promisify(treeKill);
try {
await promisifiedTreeKill(subProcess.pid, 'SIGTERM');
} catch (err) {
if (Array.isArray(err) && err[0] && err[2]) {
const errorMessage = err[2];
logger.error(errorMessage);
} else if (err.message) {
logger.error(err.message);
}
} finally {
subProcess = null;
}
}
async function* startBuild(
options: NodeExecuteBuilderOptions,
context: ExecutorContext
) {
const buildTarget = parseTargetString(options.buildTarget);
const buildOptions = readTargetOptions<BuildNodeBuilderOptions>(
buildTarget,
context
);
if (buildOptions.optimization) {
logger.warn(stripIndents`
************************************************
This is a simple process manager for use in
testing or debugging Node applications locally.
DO NOT USE IT FOR PRODUCTION!
You should look into proper means of deploying
your node application to production.
************************************************`);
}
yield* await runExecutor<NodeBuildEvent>(
buildTarget,
{
watch: options.watch,
},
context
);
}
function runWaitUntilTargets(
options: NodeExecuteBuilderOptions,
context: ExecutorContext
): Promise<{ success: boolean }[]> {
return Promise.all(
options.waitUntilTargets.map(async (waitUntilTarget) => {
const target = parseTargetString(waitUntilTarget);
const output = await runExecutor(target, {}, context);
return new Promise<{ success: boolean }>(async (resolve) => {
let event = await output.next();
// Resolve after first event
resolve(event.value as { success: boolean });
// Continue iterating
while (!event.done) {
event = await output.next();
}
});
})
);
}
export default executeExecutor;