@ts-bridge/cli
Version:
Bridge the gap between ES modules and CommonJS modules with an easy-to-use alternative to `tsc`.
83 lines (82 loc) • 2.88 kB
JavaScript
import chalk from 'chalk';
import typescript from 'typescript';
import { getCanonicalFileName } from './file-system.js';
import { getErrorMessage } from './logging.js';
import { isObject } from './utils.js';
const { formatDiagnosticsWithColorAndContext, sys } = typescript;
const NODE_ERROR_CODES = {
ENOENT: 'The file does not exist.',
EACCES: 'Permission denied.',
EISDIR: 'The file is a directory.',
EEXIST: 'The file already exists.',
ENOTDIR: 'The path is not a directory.',
EPERM: 'Operation not permitted.',
EROFS: 'The file is read-only.',
ENOTEMPTY: 'The directory is not empty.',
EBUSY: 'The file is busy.',
EMFILE: 'The process has too many files open.',
};
/**
* Get the error's `code` property if it has one.
*
* @param error - The error to get the code for.
* @returns The error code, or `null`.
*/
export function getErrorCode(error) {
if (isObject(error) && typeof error.code === 'string') {
return error.code;
}
return null;
}
/**
* An error thrown when a TypeScript build fails. This will format the
* diagnostics with colors and context.
*/
export class TypeScriptError extends Error {
constructor(message, diagnostics) {
const diagnosticsArray = Array.isArray(diagnostics)
? diagnostics
: [diagnostics];
const formattedDiagnostics = formatDiagnosticsWithColorAndContext(diagnosticsArray, {
getCanonicalFileName,
getCurrentDirectory: () => sys.getCurrentDirectory(),
getNewLine: () => sys.newLine,
});
super(`${chalk.red(message)}\n${formattedDiagnostics}`);
}
}
/**
* An error thrown when a Node.js operation fails. This will try and get a
* human-readable error message from the error code.
*/
export class NodeError extends Error {
/**
* Create a new Node.js error from the given message and original error. This
* will try and get a human-readable error message from the error code.
*
* @param message - The error message.
* @param originalError - The original error.
*/
constructor(message, originalError) {
const code = getErrorCode(originalError);
const errorMessage = code
? NODE_ERROR_CODES[code] ?? 'An unknown error occurred.'
: 'An unknown error occurred.';
super(`${chalk.red(message)}\n${errorMessage}`);
}
}
/**
* An error thrown when a worker fails. This will include the original error
* message in the error message.
*/
export class WorkerError extends Error {
/**
* Create a new worker error from the given message and original error.
*
* @param message - The error message.
* @param originalError - The original error.
*/
constructor(message, originalError) {
super(`${chalk.red(message)}: ${getErrorMessage(originalError)}`);
}
}