UNPKG

2.41 kBJavaScriptView Raw
1"use strict";
2// tslint:disable no-implicit-dependencies
3Object.defineProperty(exports, "__esModule", { value: true });
4exports.CLIError = exports.addOclifExitCode = void 0;
5const config_1 = require("../config");
6function addOclifExitCode(error, options) {
7 if (!('oclif' in error)) {
8 error.oclif = {};
9 }
10 error.oclif.exit = (options === null || options === void 0 ? void 0 : options.exit) === undefined ? 2 : options.exit;
11 return error;
12}
13exports.addOclifExitCode = addOclifExitCode;
14class CLIError extends Error {
15 constructor(error, options = {}) {
16 super(error instanceof Error ? error.message : error);
17 this.oclif = {};
18 addOclifExitCode(this, options);
19 this.code = options.code;
20 }
21 get stack() {
22 const clean = require('clean-stack');
23 return clean(super.stack, { pretty: true });
24 }
25 /**
26 * @deprecated `render` Errors display should be handled by display function, like pretty-print
27 * @return {string} returns a string representing the dispay of the error
28 */
29 render() {
30 if (config_1.config.debug) {
31 return this.stack;
32 }
33 const wrap = require('wrap-ansi');
34 const indent = require('indent-string');
35 let output = `${this.name}: ${this.message}`;
36 // eslint-disable-next-line node/no-missing-require
37 output = wrap(output, require('../screen').errtermwidth - 6, { trim: false, hard: true });
38 output = indent(output, 3);
39 output = indent(output, 1, { indent: this.bang, includeEmptyLines: true });
40 output = indent(output, 1);
41 return output;
42 }
43 get bang() {
44 let red = ((s) => s);
45 try {
46 red = require('chalk').red;
47 }
48 catch (_a) { }
49 return red(process.platform === 'win32' ? '»' : '›');
50 }
51}
52exports.CLIError = CLIError;
53(function (CLIError) {
54 class Warn extends CLIError {
55 constructor(err) {
56 super(err instanceof Error ? err.message : err);
57 this.name = 'Warning';
58 }
59 get bang() {
60 let yellow = ((s) => s);
61 try {
62 yellow = require('chalk').yellow;
63 }
64 catch (_a) { }
65 return yellow(process.platform === 'win32' ? '»' : '›');
66 }
67 }
68 CLIError.Warn = Warn;
69})(CLIError = exports.CLIError || (exports.CLIError = {}));