-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
run.ts
293 lines (272 loc) · 7.7 KB
/
run.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import {
combineOptionsForExecutor,
handleErrors,
Schema,
} from '../utils/params';
import { printHelp } from '../utils/print-help';
import { Workspaces } from '../config/workspaces';
import { NxJsonConfiguration } from '../config/nx-json';
import { readJsonFile } from '../utils/fileutils';
import { buildTargetFromScript, PackageJson } from '../utils/package-json';
import { join } from 'path';
import { existsSync } from 'fs';
import {
loadNxPlugins,
mergePluginTargetsWithNxTargets,
} from '../utils/nx-plugin';
import {
ProjectConfiguration,
TargetConfiguration,
ProjectsConfigurations,
} from '../config/workspace-json-project-json';
import { Executor, ExecutorContext } from '../config/misc-interfaces';
import { serializeOverridesIntoCommandLine } from 'nx/src/utils/serialize-overrides-into-command-line';
export interface Target {
project: string;
target: string;
configuration?: string;
}
export function printRunHelp(
opts: { project: string; target: string },
schema: Schema,
plugin: { plugin: string; entity: string }
) {
printHelp(`run ${opts.project}:${opts.target}`, schema, {
mode: 'run',
...plugin,
});
}
export function validateProject(
workspace: ProjectsConfigurations,
projectName: string
) {
const project = workspace.projects[projectName];
if (!project) {
throw new Error(`Could not find project "${projectName}"`);
}
}
function isPromise<T extends { success: boolean }>(
v: Promise<T> | AsyncIterableIterator<T>
): v is Promise<T> {
return typeof (v as any)?.then === 'function';
}
function isAsyncIterator<T extends { success: boolean }>(
v: Promise<{ success: boolean }> | AsyncIterableIterator<T>
): v is AsyncIterableIterator<T> {
return typeof (v as any)?.[Symbol.asyncIterator] === 'function';
}
async function* promiseToIterator<T extends { success: boolean }>(
v: Promise<T>
): AsyncIterableIterator<T> {
yield await v;
}
async function iteratorToProcessStatusCode(
i: AsyncIterableIterator<{ success: boolean }>
): Promise<number> {
let success: boolean;
// This is a workaround to fix an issue that only happens with
// the @angular-devkit/build-angular:browser builder. Starting
// on version 12.0.1, a SASS compilation implementation was
// introduced making use of workers and it's unref()-ing the worker
// too early, causing the process to exit early in environments
// like CI or when running Docker builds.
const keepProcessAliveInterval = setInterval(() => {}, 1000);
try {
let prev: IteratorResult<{ success: boolean }>;
let current: IteratorResult<{ success: boolean }>;
do {
prev = current;
current = await i.next();
} while (!current.done);
success =
current.value !== undefined || !prev
? current.value.success
: prev.value.success;
return success ? 0 : 1;
} finally {
clearInterval(keepProcessAliveInterval);
}
}
function createImplicitTargetConfig(
root: string,
proj: ProjectConfiguration,
targetName: string
): TargetConfiguration | null {
const packageJsonPath = join(root, proj.root, 'package.json');
if (!existsSync(packageJsonPath)) {
return null;
}
const { scripts, nx } = readJsonFile<PackageJson>(packageJsonPath);
if (!(targetName in (scripts || {}))) {
return null;
}
return buildTargetFromScript(targetName, nx);
}
async function runExecutorInternal<T extends { success: boolean }>(
{
project,
target,
configuration,
}: {
project: string;
target: string;
configuration?: string;
},
overrides: { [k: string]: any },
root: string,
cwd: string,
workspace: ProjectsConfigurations & NxJsonConfiguration,
isVerbose: boolean,
printHelp: boolean
): Promise<AsyncIterableIterator<T>> {
validateProject(workspace, project);
const ws = new Workspaces(root);
const proj = workspace.projects[project];
const targetConfig =
proj.targets?.[target] ||
createImplicitTargetConfig(root, proj, target) ||
mergePluginTargetsWithNxTargets(
proj.root,
proj.targets,
loadNxPlugins(workspace.plugins)
)[target];
if (!targetConfig) {
throw new Error(`Cannot find target '${target}' for project '${project}'`);
}
configuration = configuration ?? targetConfig.defaultConfiguration;
const [nodeModule, executor] = targetConfig.executor.split(':');
const { schema, implementationFactory } = ws.readExecutor(
nodeModule,
executor
);
if (printHelp) {
printRunHelp({ project, target }, schema, {
plugin: nodeModule,
entity: executor,
});
process.exit(0);
}
const combinedOptions = combineOptionsForExecutor(
overrides,
configuration,
targetConfig,
schema,
project,
ws.relativeCwd(cwd),
isVerbose
);
if (ws.isNxExecutor(nodeModule, executor)) {
const implementation = implementationFactory() as Executor<any>;
const r = implementation(combinedOptions, {
root,
target: targetConfig,
workspace,
projectName: project,
targetName: target,
configurationName: configuration,
cwd,
isVerbose,
}) as Promise<T> | AsyncIterableIterator<T>;
if (isPromise<T>(r)) {
return promiseToIterator<T>(r);
} else if (isAsyncIterator<T>(r)) {
return r;
} else {
throw new TypeError(
`NX Executor "${targetConfig.executor}" should return either a Promise or an AsyncIterator`
);
}
} else {
require('../adapter/compat');
const observable = await (
await import('../adapter/ngcli-adapter')
).scheduleTarget(
root,
{
project,
target,
configuration,
runOptions: combinedOptions,
executor: targetConfig.executor,
},
isVerbose
);
const { eachValueFrom } = await import('../adapter/rxjs-for-await');
return eachValueFrom(observable as any);
}
}
/**
* Loads and invokes executor.
*
* This is analogous to invoking executor from the terminal, with the exception
* that the params aren't parsed from the string, but instead provided parsed already.
*
* Apart from that, it works the same way:
*
* - it will load the workspace configuration
* - it will resolve the target
* - it will load the executor and the schema
* - it will load the options for the appropriate configuration
* - it will run the validations and will set the default
* - and, of course, it will invoke the executor
*
* Example:
*
* ```typescript
* for await (const s of await runExecutor({project: 'myproj', target: 'serve'}, {watch: true}, context)) {
* // s.success
* }
* ```
*
* Note that the return value is a promise of an iterator, so you need to await before iterating over it.
*/
export async function runExecutor<T extends { success: boolean }>(
targetDescription: {
project: string;
target: string;
configuration?: string;
},
overrides: { [k: string]: any },
context: ExecutorContext
): Promise<AsyncIterableIterator<T>> {
return await runExecutorInternal<T>(
targetDescription,
{
...overrides,
__overrides_unparsed__: serializeOverridesIntoCommandLine(overrides),
},
context.root,
context.cwd,
context.workspace,
context.isVerbose,
false
);
}
export async function run(
cwd: string,
root: string,
targetDescription: {
project: string;
target: string;
configuration?: string;
},
overrides: { [k: string]: any },
isVerbose: boolean,
isHelp: boolean
) {
const ws = new Workspaces(root);
return handleErrors(isVerbose, async () => {
const workspace = ws.readWorkspaceConfiguration();
return iteratorToProcessStatusCode(
await runExecutorInternal(
targetDescription,
overrides,
root,
cwd,
workspace,
isVerbose,
isHelp
)
);
});
}