/
static-run-many-terminal-output-life-cycle.ts
150 lines (135 loc) · 4.4 KB
/
static-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
import type { Task } from '@nrwl/devkit';
import { output } from '../../utilities/output';
import { TaskStatus } from '../tasks-runner';
import { getCommandArgsForTask } from '../utils';
import type { LifeCycle } from '../life-cycle';
/**
* The following life cycle's outputs are static, meaning no previous content
* is rewritten or modified as new outputs are added. It is therefore intended
* for use in CI environments.
*
* For the common case of a user executing a command on their local machine,
* the dynamic equivalent of this life cycle is usually preferable.
*/
export class StaticRunManyTerminalOutputLifeCycle implements LifeCycle {
failedTasks = [] as Task[];
cachedTasks = [] as Task[];
allCompletedTasks = new Set<Task>();
constructor(
private readonly projectNames: string[],
private readonly tasks: Task[],
private readonly args: {
target?: string;
configuration?: string;
},
private readonly taskOverrides: any
) {}
startCommand(): void {
if (this.projectNames.length <= 0) {
let description = `with "${this.args.target}"`;
if (this.args.configuration) {
description += ` that are configured for "${this.args.configuration}"`;
}
output.logSingleLine(`No projects ${description} were run`);
return;
}
const bodyLines = this.projectNames.map(
(affectedProject) => ` ${output.dim('-')} ${affectedProject}`
);
if (Object.keys(this.taskOverrides).length > 0) {
bodyLines.push('');
bodyLines.push(`${output.dim('With flags:')}`);
Object.entries(this.taskOverrides)
.map(([flag, value]) => ` --${flag}=${value}`)
.forEach((arg) => bodyLines.push(arg));
}
let title = `Running target ${output.bold(
this.args.target
)} for ${output.bold(this.projectNames.length)} project(s)`;
const dependentTasksCount = this.tasks.length - this.projectNames.length;
if (dependentTasksCount > 0) {
title += ` and ${output.bold(
dependentTasksCount
)} task(s) they depend on`;
}
title += ':';
output.log({
color: 'cyan',
title,
bodyLines,
});
output.addVerticalSeparatorWithoutNewLines('cyan');
}
endCommand(): void {
output.addNewline();
if (this.failedTasks.length === 0) {
output.addVerticalSeparatorWithoutNewLines('green');
const bodyLines =
this.cachedTasks.length > 0
? [
output.dim(
`Nx read the output from the cache instead of running the command for ${this.cachedTasks.length} out of ${this.tasks.length} tasks.`
),
]
: [];
output.success({
title: `Successfully ran target ${output.bold(
this.args.target
)} for ${output.bold(this.projectNames.length)} projects`,
bodyLines,
});
} else {
output.addVerticalSeparatorWithoutNewLines('red');
const bodyLines = [];
const skippedTasks = this.skippedTasks();
if (skippedTasks.length > 0) {
bodyLines.push(
output.dim('Tasks not run because their dependencies failed:'),
'',
...skippedTasks.map((task) => `${output.dim('-')} ${task.id}`),
''
);
}
bodyLines.push(
output.dim('Failed tasks:'),
'',
...[...this.failedTasks.values()].map(
(task) => `${output.dim('-')} ${task.id}`
)
);
output.error({
title: `Running target "${this.args.target}" failed`,
bodyLines,
});
}
}
private skippedTasks() {
return this.tasks.filter((t) => !this.allCompletedTasks.has(t));
}
endTasks(
taskResults: { task: Task; status: TaskStatus; code: number }[]
): void {
for (let t of taskResults) {
this.allCompletedTasks.add(t.task);
if (t.status === 'failure') {
this.failedTasks.push(t.task);
} else if (t.status === 'local-cache') {
this.cachedTasks.push(t.task);
} else if (t.status === 'local-cache-kept-existing') {
this.cachedTasks.push(t.task);
} else if (t.status === 'remote-cache') {
this.cachedTasks.push(t.task);
}
}
}
printTaskTerminalOutput(
task: Task,
cacheStatus: TaskStatus,
terminalOutput: string
) {
const args = getCommandArgsForTask(task);
output.logCommand(args.join(' '), cacheStatus);
output.addNewline();
process.stdout.write(terminalOutput);
}
}