UNPKG

1.44 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const chalk_1 = tslib_1.__importDefault(require("chalk"));
5const clean_stack_1 = tslib_1.__importDefault(require("clean-stack"));
6const exit_1 = tslib_1.__importDefault(require("exit"));
7class CLIError extends Error {
8 constructor(msg, { output, code } = {}) {
9 super(msg);
10 this.name = "CLIError";
11 this.output = output;
12 this.code = code;
13 }
14}
15exports.CLIError = CLIError;
16function formatError(err) {
17 if (!err.stack)
18 return err.message;
19 const stack = clean_stack_1.default(err.stack, { pretty: true });
20 // Regular expression is from: https://github.com/sindresorhus/extract-stack
21 const pos = stack.search(/(?:\n {4}at .*)+/);
22 if (!~pos)
23 return stack;
24 return stack.substring(0, pos) + chalk_1.default.gray(stack.substring(pos));
25}
26exports.formatError = formatError;
27function handleError(err) {
28 let code = 1;
29 let msg = err;
30 if (err instanceof CLIError) {
31 msg = err.output || formatError(err);
32 if (err.code != null)
33 code = err.code;
34 }
35 else if (err instanceof Error) {
36 msg = formatError(err);
37 }
38 // tslint:disable-next-line:no-console
39 console.error(msg);
40 exit_1.default(code);
41}
42exports.handleError = handleError;
43//# sourceMappingURL=error.js.map
\No newline at end of file