-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
dynamic-run-many-terminal-output-life-cycle.ts
504 lines (464 loc) · 15.2 KB
/
dynamic-run-many-terminal-output-life-cycle.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
import * as cliCursor from 'cli-cursor';
import { dots } from 'cli-spinners';
import { EOL } from 'os';
import * as readline from 'readline';
import { output } from '../../utils/output';
import type { LifeCycle } from '../life-cycle';
import type { TaskStatus } from '../tasks-runner';
import { Task } from '../../config/task-graph';
import { prettyTime } from './pretty-time';
import { formatFlags, formatTargetsAndProjects } from './formatting-utils';
import { viewLogsFooterRows } from './view-logs-utils';
const LEFT_PAD = ` `;
const SPACER = ` `;
const EXTENDED_LEFT_PAD = ` `;
/**
* The following function is responsible for creating a life cycle with dynamic
* outputs, meaning previous outputs can be rewritten or modified as new outputs
* are added. It is therefore intended for use on a user's local machines.
*
* In CI environments the static equivalent of this life cycle should be used.
*
* NOTE: output.dim() should be preferred over output.colors.gray() because it
* is much more consistently readable across different terminal color themes.
*/
export async function createRunManyDynamicOutputRenderer({
projectNames,
tasks,
args,
overrides,
}: {
projectNames: string[];
tasks: Task[];
args: { targets?: string[]; configuration?: string; parallel?: number };
overrides: Record<string, unknown>;
}): Promise<{ lifeCycle: LifeCycle; renderIsDone: Promise<void> }> {
cliCursor.hide();
let resolveRenderIsDonePromise: (value: void) => void;
const renderIsDone = new Promise<void>(
(resolve) => (resolveRenderIsDonePromise = resolve)
).then(() => {
clearRenderInterval();
cliCursor.show();
});
function clearRenderInterval() {
if (renderIntervalId) {
clearInterval(renderIntervalId);
}
}
process.on('exit', () => clearRenderInterval());
process.on('SIGINT', () => clearRenderInterval());
process.on('SIGTERM', () => clearRenderInterval());
process.on('SIGHUP', () => clearRenderInterval());
const lifeCycle = {} as Partial<LifeCycle>;
const isVerbose = overrides.verbose === true;
const start = process.hrtime();
const figures = await import('figures');
const targets = args.targets;
const totalTasks = tasks.length;
const taskRows = tasks.map((task) => {
return {
task,
status: 'pending',
};
});
const failedTasks = new Set();
const tasksToTerminalOutputs: Record<string, string> = {};
const tasksToProcessStartTimes: Record<
string,
ReturnType<NodeJS.HRTime>
> = {};
let hasTaskOutput = false;
let pinnedFooterNumLines = 0;
let totalCompletedTasks = 0;
let totalSuccessfulTasks = 0;
let totalFailedTasks = 0;
let totalCachedTasks = 0;
// Used to control the rendering of the spinner on each project row
let currentFrame = 0;
let renderIntervalId: NodeJS.Timeout | undefined;
const moveCursorToStartOfPinnedFooter = () => {
readline.moveCursor(process.stdout, 0, -pinnedFooterNumLines);
};
const renderPinnedFooter = (lines: string[], dividerColor = 'cyan') => {
let additionalLines = 0;
if (hasTaskOutput) {
const dividerLines = output.getVerticalSeparatorLines(dividerColor);
for (const line of dividerLines) {
output.overwriteLine(line);
}
additionalLines += dividerLines.length;
}
// Create vertical breathing room for cursor position under the pinned footer
lines.push('');
for (const line of lines) {
output.overwriteLine(line);
}
pinnedFooterNumLines = lines.length + additionalLines;
// clear any possible text below the cursor's position
readline.clearScreenDown(process.stdout);
};
const printTaskResult = (task: Task, status: TaskStatus) => {
moveCursorToStartOfPinnedFooter();
// If this is the very first output, add some vertical breathing room
if (!hasTaskOutput) {
output.addNewline();
}
hasTaskOutput = true;
switch (status) {
case 'local-cache':
writeCompletedTaskResultLine(
`${
output.colors.green(figures.tick) +
SPACER +
output.formatCommand(task.id)
} ${output.dim('[local cache]')}`
);
if (isVerbose) {
writeCommandOutputBlock(tasksToTerminalOutputs[task.id]);
}
break;
case 'local-cache-kept-existing':
writeCompletedTaskResultLine(
`${
output.colors.green(figures.tick) +
SPACER +
output.formatCommand(task.id)
} ${output.dim('[existing outputs match the cache, left as is]')}`
);
if (isVerbose) {
writeCommandOutputBlock(tasksToTerminalOutputs[task.id]);
}
break;
case 'remote-cache':
writeCompletedTaskResultLine(
`${
output.colors.green(figures.tick) +
SPACER +
output.formatCommand(task.id)
} ${output.dim('[remote cache]')}`
);
if (isVerbose) {
writeCommandOutputBlock(tasksToTerminalOutputs[task.id]);
}
break;
case 'success': {
const timeTakenText = prettyTime(
process.hrtime(tasksToProcessStartTimes[task.id])
);
writeCompletedTaskResultLine(
output.colors.green(figures.tick) +
SPACER +
output.formatCommand(task.id) +
output.dim(` (${timeTakenText})`)
);
if (isVerbose) {
writeCommandOutputBlock(tasksToTerminalOutputs[task.id]);
}
break;
}
case 'failure':
output.addNewline();
output.addNewline();
writeCompletedTaskResultLine(
output.colors.red(figures.cross) +
SPACER +
output.formatCommand(output.colors.red(task.id))
);
writeCommandOutputBlock(tasksToTerminalOutputs[task.id]);
break;
}
delete tasksToTerminalOutputs[task.id];
renderPinnedFooter([]);
renderRows();
};
const renderRows = () => {
const max = dots.frames.length - 1;
const curr = currentFrame;
currentFrame = curr >= max ? 0 : curr + 1;
const additionalFooterRows: string[] = [''];
const runningTasks = taskRows.filter((row) => row.status === 'running');
const remainingTasks = totalTasks - totalCompletedTasks;
if (runningTasks.length > 0) {
additionalFooterRows.push(
output.dim(
`${LEFT_PAD}${output.colors.cyan(
figures.arrowRight
)}${SPACER}Executing ${
runningTasks.length
}/${remainingTasks} remaining tasks${
runningTasks.length > 1 ? ' in parallel' : ''
}...`
)
);
additionalFooterRows.push('');
for (const runningTask of runningTasks) {
additionalFooterRows.push(
`${LEFT_PAD}${output.dim.cyan(
dots.frames[currentFrame]
)}${SPACER}${output.formatCommand(runningTask.task.id)}`
);
}
/**
* Reduce layout thrashing by ensuring that there is a relatively consistent
* height for the area in which the task rows are rendered.
*
* We can look at the parallel flag to know how many rows are likely to be
* needed in the common case and always render that at least that many.
*/
if (
totalCompletedTasks !== totalTasks &&
Number.isInteger(args.parallel) &&
runningTasks.length < args.parallel
) {
// Don't bother with this optimization if there are fewer tasks remaining than rows required
if (remainingTasks >= args.parallel) {
for (let i = runningTasks.length; i < args.parallel; i++) {
additionalFooterRows.push('');
}
}
}
}
if (totalSuccessfulTasks > 0 || totalFailedTasks > 0) {
additionalFooterRows.push('');
}
if (totalSuccessfulTasks > 0) {
additionalFooterRows.push(
`${LEFT_PAD}${output.colors.green(
figures.tick
)}${SPACER}${totalSuccessfulTasks}${`/${totalCompletedTasks}`} succeeded ${output.dim(
`[${totalCachedTasks} read from cache]`
)}`
);
}
if (totalFailedTasks > 0) {
additionalFooterRows.push(
`${LEFT_PAD}${output.colors.red(
figures.cross
)}${SPACER}${totalFailedTasks}${`/${totalCompletedTasks}`} failed`
);
}
moveCursorToStartOfPinnedFooter();
if (additionalFooterRows.length > 1) {
const text = `Running ${formatTargetsAndProjects(
projectNames,
targets,
tasks
)}`;
const taskOverridesRows = [];
if (Object.keys(overrides).length > 0) {
taskOverridesRows.push('');
taskOverridesRows.push(
`${EXTENDED_LEFT_PAD}${output.dim.cyan('With additional flags:')}`
);
Object.entries(overrides)
.map(([flag, value]) =>
output.dim.cyan(formatFlags(EXTENDED_LEFT_PAD, flag, value))
)
.forEach((arg) => taskOverridesRows.push(arg));
}
const pinnedFooterLines = [
output.applyNxPrefix('cyan', output.colors.cyan(text)),
...taskOverridesRows,
...additionalFooterRows,
];
// Vertical breathing room when there isn't yet any output or divider
if (!hasTaskOutput) {
pinnedFooterLines.unshift('');
}
renderPinnedFooter(pinnedFooterLines);
} else {
renderPinnedFooter([]);
}
};
lifeCycle.startCommand = () => {
if (projectNames.length <= 0) {
renderPinnedFooter([
'',
output.applyNxPrefix(
'gray',
`No projects with ${formatTargetsAndProjects(
projectNames,
targets,
tasks
)} were run`
),
]);
resolveRenderIsDonePromise();
return;
}
renderPinnedFooter([]);
};
lifeCycle.endCommand = () => {
clearRenderInterval();
const timeTakenText = prettyTime(process.hrtime(start));
moveCursorToStartOfPinnedFooter();
if (totalSuccessfulTasks === totalTasks) {
const text = `Successfully ran ${formatTargetsAndProjects(
projectNames,
targets,
tasks
)}`;
const taskOverridesRows = [];
if (Object.keys(overrides).length > 0) {
taskOverridesRows.push('');
taskOverridesRows.push(
`${EXTENDED_LEFT_PAD}${output.dim.green('With additional flags:')}`
);
Object.entries(overrides)
.map(([flag, value]) =>
output.dim.green(formatFlags(EXTENDED_LEFT_PAD, flag, value))
)
.forEach((arg) => taskOverridesRows.push(arg));
}
const pinnedFooterLines = [
output.applyNxPrefix(
'green',
output.colors.green(text) + output.dim.white(` (${timeTakenText})`)
),
...taskOverridesRows,
];
if (totalCachedTasks > 0) {
pinnedFooterLines.push(
output.dim(
`${EOL}Nx read the output from the cache instead of running the command for ${totalCachedTasks} out of ${totalTasks} tasks.`
)
);
}
renderPinnedFooter(pinnedFooterLines, 'green');
} else {
const text = `Ran ${formatTargetsAndProjects(
projectNames,
targets,
tasks
)}`;
const taskOverridesRows = [];
if (Object.keys(overrides).length > 0) {
taskOverridesRows.push('');
taskOverridesRows.push(
`${EXTENDED_LEFT_PAD}${output.dim.red('With additional flags:')}`
);
Object.entries(overrides)
.map(([flag, value]) =>
output.dim.red(formatFlags(EXTENDED_LEFT_PAD, flag, value))
)
.forEach((arg) => taskOverridesRows.push(arg));
}
const numFailedToPrint = 5;
const failedTasksForPrinting = Array.from(failedTasks).slice(
0,
numFailedToPrint
);
const failureSummaryRows = [
output.applyNxPrefix(
'red',
output.colors.red(text) + output.dim.white(` (${timeTakenText})`)
),
...taskOverridesRows,
'',
output.dim(
`${LEFT_PAD}${output.dim(
figures.tick
)}${SPACER}${totalSuccessfulTasks}${`/${totalCompletedTasks}`} succeeded ${output.dim(
`[${totalCachedTasks} read from cache]`
)}`
),
'',
`${LEFT_PAD}${output.colors.red(
figures.cross
)}${SPACER}${totalFailedTasks}${`/${totalCompletedTasks}`} targets failed, including the following:`,
'',
`${failedTasksForPrinting
.map(
(t) =>
`${EXTENDED_LEFT_PAD}${output.colors.red(
'-'
)} ${output.formatCommand(t.toString())}`
)
.join('\n ')}`,
];
if (failedTasks.size > numFailedToPrint) {
failureSummaryRows.push(
output.dim(
`${EXTENDED_LEFT_PAD}...and ${
failedTasks.size - numFailedToPrint
} more...`
)
);
}
failureSummaryRows.push(...viewLogsFooterRows(failedTasks.size));
renderPinnedFooter(failureSummaryRows, 'red');
}
resolveRenderIsDonePromise();
};
lifeCycle.startTasks = (tasks: Task[]) => {
for (const task of tasks) {
tasksToProcessStartTimes[task.id] = process.hrtime();
}
for (const taskRow of taskRows) {
if (tasks.indexOf(taskRow.task) > -1) {
taskRow.status = 'running';
}
}
if (!renderIntervalId) {
renderIntervalId = setInterval(renderRows, 100);
}
};
lifeCycle.printTaskTerminalOutput = (task, _cacheStatus, output) => {
tasksToTerminalOutputs[task.id] = output;
};
lifeCycle.endTasks = (taskResults) => {
for (let t of taskResults) {
totalCompletedTasks++;
const matchingTaskRow = taskRows.find((r) => r.task.id === t.task.id);
if (matchingTaskRow) {
matchingTaskRow.status = t.status;
}
switch (t.status) {
case 'remote-cache':
case 'local-cache':
case 'local-cache-kept-existing':
totalCachedTasks++;
totalSuccessfulTasks++;
break;
case 'success':
totalSuccessfulTasks++;
break;
case 'failure':
totalFailedTasks++;
failedTasks.add(t.task.id);
break;
}
printTaskResult(t.task, t.status);
}
};
return { lifeCycle, renderIsDone };
}
function writeCompletedTaskResultLine(line: string) {
output.overwriteLine(LEFT_PAD + line);
}
/**
* There's not much we can do in order to "neaten up" the outputs of
* commands we do not control, but at the very least we can trim any
* leading whitespace and any _excess_ trailing newlines so that there
* isn't unncecessary vertical whitespace.
*/
function writeCommandOutputBlock(commandOutput: string) {
commandOutput = commandOutput || '';
commandOutput = commandOutput.trimStart();
const lines = commandOutput.split(EOL);
let totalTrailingEmptyLines = 0;
for (let i = lines.length - 1; i >= 0; i--) {
if (lines[i] !== '') {
break;
}
totalTrailingEmptyLines++;
}
if (totalTrailingEmptyLines > 1) {
const linesToRemove = totalTrailingEmptyLines - 1;
lines.splice(lines.length - linesToRemove, linesToRemove);
}
lines.push('');
// Indent the command output to make it look more "designed" in the context of the dynamic output
lines.forEach((l) => output.overwriteLine(`${EXTENDED_LEFT_PAD}${l}`));
}