UNPKG

3.9 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6const cli_table_1 = __importDefault(require("cli-table"));
7const prettyjson_1 = __importDefault(require("prettyjson"));
8const pretty_error_1 = __importDefault(require("pretty-error"));
9const chalk_1 = __importDefault(require("chalk"));
10class Output {
11 constructor() {
12 this.exitCode = 0;
13 this.data = [];
14 this.prettyError = new pretty_error_1.default();
15 }
16 static create() {
17 return new this();
18 }
19 static errorOutput(error, exitCode) {
20 const output = new this();
21 output.error(error, exitCode);
22 return output;
23 }
24 /**
25 * Exit code to use upon send.
26 *
27 * NOTE: Exit codes 1 - 2, 126 - 165, and 255 have special meanings
28 * and should therefore be avoided for user-specified exit parameters.
29 * Try restricting user-defined exit codes to the range 64 - 113 (in addition to 0, for success).
30 *
31 * 1 - Catchall for general errors
32 * 2 - Misuse of shell builtins (according to Bash documentation)
33 * 126 - Command invoked cannot execute
34 * 127 - “command not found”
35 * 128 - Invalid argument to exit
36 * 128+n - Fatal error signal “n”
37 * 130 - Script terminated by Control-C
38 * 255\* - Exit status out of range
39 *
40 * @see http://tldp.org/LDP/abs/html/exitcodes.html
41 *
42 * @param {Number} exitCode
43 *
44 * @return {this}
45 */
46 setExitCode(exitCode) {
47 this.exitCode = exitCode;
48 return this;
49 }
50 addData(...data) {
51 data.forEach(item => {
52 if (item === undefined) {
53 return;
54 }
55 if (item.constructor === Object) {
56 return this.addData(prettyjson_1.default.render(item));
57 }
58 this.data.push(item);
59 });
60 return this;
61 }
62 success(message, data) {
63 this.addData(chalk_1.default `\n {green {bold Success!}} ${message}\n`, data);
64 }
65 resetData() {
66 this.data = [];
67 return this;
68 }
69 addHorizontalTable(head, data, options = {}) {
70 const table = new cli_table_1.default(Object.assign({ head }, options));
71 table.push(...data);
72 this.addData(table.toString());
73 return this;
74 }
75 addVerticalTable(data, options) {
76 const table = new cli_table_1.default(options);
77 table.push(...data);
78 this.addData(table.toString());
79 return this;
80 }
81 addCrossTable(head, data, options = {}) {
82 const table = new cli_table_1.default(options);
83 table.push(...data);
84 this.addData(table.toString());
85 return this;
86 }
87 /**
88 * Add/set an error to output.
89 *
90 * @param {Error|string} [error] The error to write. Default to "Unknown error".
91 * @param {Number|null} [exitCode] The code to exit with. Default to 1. Use null to not change the exitCode.
92 * @param {boolean} [clear] Clear previously set data. Default to false.
93 *
94 * @return {this}
95 */
96 error(error = 'Unknown error', exitCode = 1, clear = false) {
97 if (!this.prettyError) {
98 this.prettyError = new pretty_error_1.default();
99 }
100 if (exitCode) {
101 this.setExitCode(exitCode);
102 }
103 if (clear) {
104 this.resetData();
105 }
106 this.addData(this.prettyError.render(error));
107 return this;
108 }
109 /**
110 * Write the output to the console.
111 */
112 flush() {
113 this.data.forEach((piece) => console.log(piece));
114 }
115 send() {
116 this.flush();
117 process.exit(this.exitCode);
118 }
119}
120exports.Output = Output;
121//# sourceMappingURL=Output.js.map
\No newline at end of file