UNPKG

@modern-js/module-tools-v2

Version:

The meta-framework suite designed from scratch for frontend-focused modern web development.

140 lines (137 loc) 4.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.padSpaceWith = exports.isInternalError = exports.isExecaError = exports.ModuleBuildError = exports.InternalDTSError = exports.InternalBuildError = void 0; var _utils = require("@modern-js/utils"); function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } const padSpaceWith = (str, targetL, opts = {}) => { const { endStr = '|', style } = opts; const l = str.length; const endStrL = endStr.length; const resetL = targetL - l - endStrL; // str is 'aaa', targetL is 4, result is 'aaa|' if (resetL === 0) { return (style ? style(str) : str) + endStr; } // str is 'aaa', targetL is 5, result is 'aaa |' if (resetL > 0) { const padStr = str.padEnd(targetL - 1, ' '); if (style) { return style(str) + padStr.replace(str, '') + endStr; } return padStr + endStr; } return str; }; exports.padSpaceWith = padSpaceWith; class InternalBuildError extends Error { constructor(e, opts) { super(e.message); _defineProperty(this, "buildType", void 0); _defineProperty(this, "target", void 0); _defineProperty(this, "format", void 0); _defineProperty(this, "e", void 0); Error.captureStackTrace(this, this.constructor); this.e = e; this.buildType = opts.buildType; this.target = opts.target; this.format = opts.format; } toString() { return this.formatError().join('\n'); } formatError() { const msgs = []; const { e, buildType, target, format } = this; const textL = 25; const title = `│ ${padSpaceWith(`${buildType} failed:`, textL - 2, { style: _utils.chalk.red.underline, endStr: '│' })}`; const formatMsg = padSpaceWith(`│ - format is "${format}"`, textL, { endStr: '│' }); const targetMsg = padSpaceWith(`│ - target is "${target}"`, textL, { endStr: '│' }); const startLine = padSpaceWith('╭'.padEnd(textL - 1, '─'), textL, { endStr: '╮' }); const endLine = padSpaceWith('╰'.padEnd(textL - 1, '─'), textL, { endStr: '╯' }); msgs.push(startLine, title, formatMsg, targetMsg, endLine, _utils.chalk.blue.bold.underline(`\nDetailed Information: `)); msgs.push(e.toString()); msgs.push(e.stack || ''); return msgs; } } exports.InternalBuildError = InternalBuildError; class InternalDTSError extends Error { constructor(e, opts) { super(e.message); _defineProperty(this, "buildType", void 0); _defineProperty(this, "e", void 0); Error.captureStackTrace(this, this.constructor); this.e = e; this.buildType = opts.buildType; } toString() { return this.formatError().join('\n'); } formatError() { const msgs = []; const { e, buildType } = this; msgs.push(_utils.chalk.red.bold(`${buildType} DTS failed:`)); if (isExecaError(e)) { /** * `shortMeessage` content like: * 'Command failed with exit code 2: /Users/github/modern.js/playground/module/node_modules/.bin/tsc -p /Users/github/modern.js/playground/module/node_modules/tsconfig.temp.json --pretty' * Don`t need it. */ if (e.stack) { var _e$stack; msgs.push((_e$stack = e.stack) === null || _e$stack === void 0 ? void 0 : _e$stack.replace(`${e.name}: ${e.shortMessage}`, '')); } } else { msgs.push(e.stack); } return msgs; } } exports.InternalDTSError = InternalDTSError; class ModuleBuildError extends Error { constructor(e) { super(`\n\n${e}`); Error.captureStackTrace(this, this.constructor); this.name = 'ModuleBuildError'; } } exports.ModuleBuildError = ModuleBuildError; const isInternalError = e => { if (e instanceof InternalBuildError || e instanceof InternalDTSError) { return true; } return false; }; exports.isInternalError = isInternalError; const isExecaError = e => { if (e.stdout) { return true; } return false; }; exports.isExecaError = isExecaError; //# sourceMappingURL=error.js.map