@kosko/cli
Version:
Organize Kubernetes manifests in JavaScript.
43 lines • 1.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleError = exports.formatError = exports.CLIError = void 0;
const tslib_1 = require("tslib");
const chalk_1 = tslib_1.__importDefault(require("chalk"));
const clean_stack_1 = tslib_1.__importDefault(require("clean-stack"));
const exit_1 = tslib_1.__importDefault(require("exit"));
class CLIError extends Error {
constructor(msg, { output, code } = {}) {
super(msg);
this.name = "CLIError";
this.output = output;
this.code = code;
}
}
exports.CLIError = CLIError;
function formatError(err) {
if (!err.stack)
return err.message;
const stack = clean_stack_1.default(err.stack, { pretty: true });
// Regular expression is from: https://github.com/sindresorhus/extract-stack
const pos = stack.search(/(?:\n {4}at .*)+/);
if (!~pos)
return stack;
return stack.substring(0, pos) + chalk_1.default.gray(stack.substring(pos));
}
exports.formatError = formatError;
function handleError(err) {
let code = 1;
let msg = err;
if (err instanceof CLIError) {
msg = err.output || formatError(err);
if (err.code != null)
code = err.code;
}
else if (err instanceof Error) {
msg = formatError(err);
}
console.error(msg);
exit_1.default(code);
}
exports.handleError = handleError;
//# sourceMappingURL=error.js.map