-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
output.ts
271 lines (227 loc) · 6.15 KB
/
output.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
import * as chalk from 'chalk';
import { EOL } from 'os';
import { isCI } from './is-ci';
import { TaskStatus } from '../tasks-runner/tasks-runner';
export interface CLIErrorMessageConfig {
title: string;
bodyLines?: string[];
slug?: string;
}
export interface CLIWarnMessageConfig {
title: string;
bodyLines?: string[];
slug?: string;
}
export interface CLINoteMessageConfig {
title: string;
bodyLines?: string[];
}
export interface CLISuccessMessageConfig {
title: string;
bodyLines?: string[];
}
/**
* Automatically disable styling applied by chalk if CI=true
*/
const forceColor =
process.env.FORCE_COLOR === '' || process.env.FORCE_COLOR === 'true';
if (isCI() && !forceColor) {
(chalk as any).level = 0;
}
class CLIOutput {
readonly X_PADDING = ' ';
cliName = 'NX';
formatCommand = (message: string) => `${chalk.dim('> nx run')} ${message}`;
/**
* Longer dash character which forms more of a continuous line when place side to side
* with itself, unlike the standard dash character
*/
private get VERTICAL_SEPARATOR() {
let divider = '';
for (
let i = 0;
i < process.stdout.columns - this.X_PADDING.length * 2;
i++
) {
divider += '\u2014';
}
return divider;
}
/**
* Expose some color and other utility functions so that other parts of the codebase that need
* more fine-grained control of message bodies are still using a centralized
* implementation.
*/
colors = {
gray: chalk.gray,
green: chalk.green,
red: chalk.red,
cyan: chalk.cyan,
white: chalk.white,
};
bold = chalk.bold;
underline = chalk.underline;
dim = chalk.dim;
private writeToStdOut(str: string) {
process.stdout.write(str);
}
private writeOutputTitle({
color,
title,
}: {
color: string;
title: string;
}): void {
this.writeToStdOut(` ${this.applyNxPrefix(color, title)}${EOL}`);
}
private writeOptionalOutputBody(bodyLines?: string[]): void {
if (!bodyLines) {
return;
}
this.addNewline();
bodyLines.forEach((bodyLine) => this.writeToStdOut(` ${bodyLine}${EOL}`));
}
applyNxPrefix(color = 'cyan', text: string): string {
let nxPrefix = '';
if (chalk[color]) {
nxPrefix = `${chalk[color]('>')} ${chalk.reset.inverse.bold[color](
` ${this.cliName} `
)}`;
} else {
nxPrefix = `${chalk.keyword(color)(
'>'
)} ${chalk.reset.inverse.bold.keyword(color)(` ${this.cliName} `)}`;
}
return `${nxPrefix} ${text}`;
}
addNewline() {
this.writeToStdOut(EOL);
}
addVerticalSeparator(color = 'gray') {
this.addNewline();
this.addVerticalSeparatorWithoutNewLines(color);
this.addNewline();
}
addVerticalSeparatorWithoutNewLines(color = 'gray') {
this.writeToStdOut(
`${this.X_PADDING}${chalk.dim[color](this.VERTICAL_SEPARATOR)}${EOL}`
);
}
error({ title, slug, bodyLines }: CLIErrorMessageConfig) {
this.addNewline();
this.writeOutputTitle({
color: 'red',
title: chalk.red(title),
});
this.writeOptionalOutputBody(bodyLines);
/**
* Optional slug to be used in an Nx error message redirect URL
*/
if (slug && typeof slug === 'string') {
this.addNewline();
this.writeToStdOut(
`${chalk.grey(
' Learn more about this error: '
)}https://errors.nx.dev/${slug}${EOL}`
);
}
this.addNewline();
}
warn({ title, slug, bodyLines }: CLIWarnMessageConfig) {
this.addNewline();
this.writeOutputTitle({
color: 'yellow',
title: chalk.yellow(title),
});
this.writeOptionalOutputBody(bodyLines);
/**
* Optional slug to be used in an Nx warning message redirect URL
*/
if (slug && typeof slug === 'string') {
this.addNewline();
this.writeToStdOut(
`${chalk.grey(
' Learn more about this warning: '
)}https://errors.nx.dev/${slug}\n`
);
}
this.addNewline();
}
note({ title, bodyLines }: CLINoteMessageConfig) {
this.addNewline();
this.writeOutputTitle({
color: 'orange',
title: chalk.keyword('orange')(title),
});
this.writeOptionalOutputBody(bodyLines);
this.addNewline();
}
success({ title, bodyLines }: CLISuccessMessageConfig) {
this.addNewline();
this.writeOutputTitle({
color: 'green',
title: chalk.green(title),
});
this.writeOptionalOutputBody(bodyLines);
this.addNewline();
}
logSingleLine(message: string) {
this.addNewline();
this.writeOutputTitle({
color: 'gray',
title: message,
});
this.addNewline();
}
logCommand(message: string, taskStatus?: TaskStatus) {
this.addNewline();
const commandOutput = this.formatCommand(this.normalizeMessage(message));
const commandOutputWithStatus = this.addTaskStatus(
taskStatus,
commandOutput
);
this.writeToStdOut(commandOutputWithStatus);
this.addNewline();
}
private normalizeMessage(message: string) {
if (message.startsWith('nx run ')) {
return message.substring('nx run '.length);
} else if (message.startsWith('run ')) {
return message.substring('run '.length);
} else {
return message;
}
}
private addTaskStatus(
taskStatus:
| 'success'
| 'failure'
| 'skipped'
| 'local-cache-kept-existing'
| 'local-cache'
| 'remote-cache',
commandOutput: string
) {
if (taskStatus === 'local-cache') {
return `${commandOutput} ${chalk.dim('[local cache]')}`;
} else if (taskStatus === 'remote-cache') {
return `${commandOutput} ${chalk.dim('[remote cache]')}`;
} else if (taskStatus === 'local-cache-kept-existing') {
return `${commandOutput} ${chalk.dim(
'[existing outputs match the cache, left as is]'
)}`;
} else {
return commandOutput;
}
}
log({ title, bodyLines, color }: CLIWarnMessageConfig & { color?: string }) {
this.addNewline();
this.writeOutputTitle({
color: 'cyan',
title: color ? chalk[color](title) : title,
});
this.writeOptionalOutputBody(bodyLines);
this.addNewline();
}
}
export const output = new CLIOutput();