UNPKG

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