@quenty/cli-output-helpers
Version:
Helpers to generate Nevermore package and game templates
154 lines • 4.97 kB
JavaScript
import chalk from 'chalk';
import { AsyncLocalStorage } from 'async_hooks';
const _outputStorage = new AsyncLocalStorage();
/**
* Helps with output
*/
export class OutputHelper {
static _verbose = true;
/**
* Formats the error with markup
* @param message Message to format
* @returns Formatted string
*/
static formatError(message) {
return chalk.redBright(message);
}
/**
* Formats the information message
* @param message Message to format
* @returns Formatted string
*/
static formatInfo(message) {
return chalk.cyanBright(message);
}
/**
* Formats a warning message
* @param message Message to format
* @returns Formatted string
*/
static formatWarning(message) {
return chalk.yellowBright(message);
}
/**
* Formats the information
* @param message Message to format
* @returns Formatted string
*/
static formatDescription(message) {
return chalk.greenBright(message);
}
/**
* Formats the hint message
* @param message Message to format
* @returns Formatted string
*/
static formatHint(message) {
return chalk.magentaBright(message);
}
static formatDim(message) {
return chalk.dim(message);
}
static formatSuccess(message) {
return chalk.greenBright(message);
}
static _hasAnsi = (text) => text.includes('\x1b[');
/** Strip ANSI escape codes from terminal output. */
static stripAnsi = (text) => text.replace(/\x1b\[[0-9;]*[a-zA-Z]/g, '');
/**
* Helper method to put a box around the output
*/
static formatBox(message, options) {
const lines = message.trim().split('\n');
const width = lines.reduce((a, b) => Math.max(a, OutputHelper.stripAnsi(b).length), 0);
const centered = options?.centered ?? false;
const surround = (text) => {
const first = centered
? Math.floor((width - OutputHelper.stripAnsi(text).length) / 2)
: 0;
const last = width - OutputHelper.stripAnsi(text).length - first;
return ('║ \x1b[0m' +
' '.repeat(first) +
text +
' '.repeat(last) +
'\x1b[31m ║');
};
const bar = '═'.repeat(width);
const top = '\x1b[31m╔═══' + bar + '═══╗';
const pad = surround('');
const bottom = '╚═══' + bar + '═══╝\x1b[0m';
return [top, pad, ...lines.map(surround), pad, bottom].join('\n');
}
/**
* Logs information to the console
* @param message Message to write
*/
static error(message) {
console.error(this._hasAnsi(message) ? message : this.formatError(message));
}
/**
* Logs information to the console
* @param message Message to write
*/
static info(message) {
console.log(this._hasAnsi(message) ? message : this.formatInfo(message));
}
/**
* Sets whether verbose messages are printed.
* Defaults to true. Batch runners set this to false to suppress
* intermediate messages during concurrent execution.
*/
static setVerbose(verbose) {
this._verbose = verbose;
}
/**
* Logs a verbose/intermediate message. Suppressed when verbose is false.
* When running inside a buffered context (see runBuffered), messages are
* captured to the buffer instead of printed.
*/
static verbose(message) {
if (!this._verbose) {
return;
}
const formatted = this._hasAnsi(message) ? message : this.formatDim(message);
const buffer = _outputStorage.getStore();
if (buffer) {
buffer.lines.push(formatted);
}
else {
console.log(formatted);
}
}
/**
* Run an async function with output buffering. All OutputHelper.verbose()
* calls inside the function are captured and returned alongside the result.
* Used by batch runners to collect per-package output without interleaving.
*/
static async runBuffered(fn) {
const buffer = { lines: [] };
const result = await _outputStorage.run(buffer, fn);
return { result, output: buffer.lines };
}
/**
* Logs warning to the console
* @param message Message to write
*/
static warn(message) {
console.log(this._hasAnsi(message) ? message : this.formatWarning(message));
}
/**
* Logs hint to the console
* @param message Message to write
*/
static hint(message) {
console.log(this._hasAnsi(message) ? message : this.formatHint(message));
}
/**
* Renders a box around the message
* @param message
*/
static box(message, options) {
console.log(this.formatBox(message, options));
}
}
//# sourceMappingURL=outputHelper.js.map