1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const tslib_1 = require("tslib");
|
4 | const chalk_1 = tslib_1.__importDefault(require("chalk"));
|
5 | const clean_stack_1 = tslib_1.__importDefault(require("clean-stack"));
|
6 | const exit_1 = tslib_1.__importDefault(require("exit"));
|
7 | class 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 | }
|
15 | exports.CLIError = CLIError;
|
16 | function formatError(err) {
|
17 | if (!err.stack)
|
18 | return err.message;
|
19 | const stack = clean_stack_1.default(err.stack, { pretty: true });
|
20 |
|
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 | }
|
26 | exports.formatError = formatError;
|
27 | function 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 |
|
39 | console.error(msg);
|
40 | exit_1.default(code);
|
41 | }
|
42 | exports.handleError = handleError;
|
43 |
|
\ | No newline at end of file |