@ts-bridge/cli
Version:
Bridge the gap between ES modules and CommonJS modules with an easy-to-use alternative to `tsc`.
80 lines (79 loc) • 2.45 kB
JavaScript
import chalk from 'chalk';
import { relative } from 'path';
/**
* Get an error message from an error object.
*
* @param errorObject - The error object.
* @param verbose - Whether to enable verbose logging.
* @returns The error message.
*/
export function getErrorMessage(errorObject, verbose) {
if (errorObject instanceof Error) {
if (verbose && errorObject.stack) {
return errorObject.stack;
}
return errorObject.message;
}
return String(errorObject);
}
/**
* Log an error message. The message is prefixed with a red cross.
*
* @param errorObject - The error to log.
* @param verbose - Whether to enable verbose logging.
*/
export function error(errorObject, verbose) {
console.error(`${chalk.red('✖')} ${getErrorMessage(errorObject, verbose)}`);
}
/**
* Log a warning message. The message is prefixed with a yellow exclamation
* mark.
*
* @param message - The warning message to log.
*/
export function warn(message) {
console.warn(`${chalk.yellow('⚠')} ${chalk.white(message)}`);
}
/**
* Log an information message. The message is prefixed with a blue 'i'.
*
* @param message - The information message to log.
*/
export function info(message) {
console.log(`${chalk.blue('ℹ')} ${chalk.reset(message)}`);
}
/**
* Log a success message. The message is prefixed with a green checkmark.
*
* @param message - The success message to log.
*/
export function success(message) {
console.log(`${chalk.green('✔')} ${chalk.reset(message)}`);
}
/**
* Log a generic message. The message is prefixed with a green arrow.
*
* @param message - The message to log.
*/
export function log(message) {
console.log(`${chalk.green('→')} ${chalk.dim(message)}`);
}
/**
* Get a transformer function that logs the file that is being transformed.
*
* This transformer does not actually transform the source file. It just uses
* TypeScript's transformer API to check which files are being transformed.
*
* @param baseDirectory - The base directory of the project.
* @param verbose - Whether to enable verbose logging.
* @returns The transformer function.
*/
export function getLoggingTransformer(baseDirectory, verbose) {
return () => {
return (sourceFile) => {
verbose &&
log(`Transforming source file "${chalk.underline(relative(baseDirectory, sourceFile.fileName))}".`);
return sourceFile;
};
};
}