UNPKG

4.53 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.output = void 0;
4const chalk = require("chalk");
5/**
6 * Automatically disable styling applied by chalk if CI=true
7 */
8if (process.env.CI === 'true') {
9 chalk.level = 0;
10}
11class CLIOutput {
12 constructor() {
13 this.NX_PREFIX = `${chalk.cyan('>')} ${chalk.reset.inverse.bold.cyan(' NX ')}`;
14 /**
15 * Longer dash character which forms more of a continuous line when place side to side
16 * with itself, unlike the standard dash character
17 */
18 this.VERTICAL_SEPARATOR = '———————————————————————————————————————————————';
19 /**
20 * Expose some color and other utility functions so that other parts of the codebase that need
21 * more fine-grained control of message bodies are still using a centralized
22 * implementation.
23 */
24 this.colors = {
25 gray: chalk.gray,
26 };
27 this.bold = chalk.bold;
28 this.underline = chalk.underline;
29 }
30 writeToStdOut(str) {
31 process.stdout.write(str);
32 }
33 writeOutputTitle({ label, title, }) {
34 let outputTitle;
35 if (label) {
36 outputTitle = `${this.NX_PREFIX} ${label} ${title}\n`;
37 }
38 else {
39 outputTitle = `${this.NX_PREFIX} ${title}\n`;
40 }
41 this.writeToStdOut(outputTitle);
42 }
43 writeOptionalOutputBody(bodyLines) {
44 if (!bodyLines) {
45 return;
46 }
47 this.addNewline();
48 bodyLines.forEach((bodyLine) => this.writeToStdOut(` ${bodyLine}\n`));
49 }
50 addNewline() {
51 this.writeToStdOut('\n');
52 }
53 addVerticalSeparator() {
54 this.writeToStdOut(`\n${chalk.gray(this.VERTICAL_SEPARATOR)}\n\n`);
55 }
56 addVerticalSeparatorWithoutNewLines() {
57 this.writeToStdOut(`${chalk.gray(this.VERTICAL_SEPARATOR)}\n`);
58 }
59 error({ title, slug, bodyLines }) {
60 this.addNewline();
61 this.writeOutputTitle({
62 label: chalk.reset.inverse.bold.red(' ERROR '),
63 title: chalk.bold.red(title),
64 });
65 this.writeOptionalOutputBody(bodyLines);
66 /**
67 * Optional slug to be used in an Nx error message redirect URL
68 */
69 if (slug && typeof slug === 'string') {
70 this.addNewline();
71 this.writeToStdOut(`${chalk.grey(' Learn more about this error: ')}https://errors.nx.dev/${slug}\n`);
72 }
73 this.addNewline();
74 }
75 warn({ title, slug, bodyLines }) {
76 this.addNewline();
77 this.writeOutputTitle({
78 label: chalk.reset.inverse.bold.yellow(' WARNING '),
79 title: chalk.bold.yellow(title),
80 });
81 this.writeOptionalOutputBody(bodyLines);
82 /**
83 * Optional slug to be used in an Nx warning message redirect URL
84 */
85 if (slug && typeof slug === 'string') {
86 this.addNewline();
87 this.writeToStdOut(`${chalk.grey(' Learn more about this warning: ')}https://errors.nx.dev/${slug}\n`);
88 }
89 this.addNewline();
90 }
91 note({ title, bodyLines }) {
92 this.addNewline();
93 this.writeOutputTitle({
94 label: chalk.reset.inverse.bold.keyword('orange')(' NOTE '),
95 title: chalk.bold.keyword('orange')(title),
96 });
97 this.writeOptionalOutputBody(bodyLines);
98 this.addNewline();
99 }
100 success({ title, bodyLines }) {
101 this.addNewline();
102 this.writeOutputTitle({
103 label: chalk.reset.inverse.bold.green(' SUCCESS '),
104 title: chalk.bold.green(title),
105 });
106 this.writeOptionalOutputBody(bodyLines);
107 this.addNewline();
108 }
109 logSingleLine(message) {
110 this.addNewline();
111 this.writeOutputTitle({
112 title: message,
113 });
114 this.addNewline();
115 }
116 logCommand(message, isCached = false) {
117 this.addNewline();
118 this.writeToStdOut(chalk.bold(`> ${message} `));
119 if (isCached) {
120 this.writeToStdOut(chalk.bold.grey(`[retrieved from cache]`));
121 }
122 this.addNewline();
123 }
124 log({ title, bodyLines }) {
125 this.addNewline();
126 this.writeOutputTitle({
127 title: chalk.white(title),
128 });
129 this.writeOptionalOutputBody(bodyLines);
130 this.addNewline();
131 }
132}
133exports.output = new CLIOutput();
134//# sourceMappingURL=output.js.map
\No newline at end of file