@formatjs/cli-lib
Version:
Lib for CLI for formatjs.
68 lines (67 loc) • 2.27 kB
JavaScript
import { green, red, supportsColor, yellow } from 'chalk';
import readline from 'readline';
import { format, promisify } from 'util';
const CLEAR_WHOLE_LINE = 0;
export const writeStderr = promisify(process.stderr.write).bind(process.stderr);
export const writeStdout = promisify(process.stdout.write).bind(process.stdout);
const nativeClearLine = promisify(readline.clearLine).bind(readline);
const nativeCursorTo = promisify(readline.cursorTo).bind(readline);
// From:
// https://github.com/yarnpkg/yarn/blob/53d8004229f543f342833310d5af63a4b6e59c8a/src/reporters/console/util.js
export async function clearLine(terminal) {
if (!supportsColor) {
if (terminal.isTTY) {
// terminal
if (terminal.columns > 0) {
await writeStderr(`\r${' '.repeat(terminal.columns - 1)}`);
}
await writeStderr(`\r`);
}
// ignore piping to file
}
else {
await nativeClearLine(terminal, CLEAR_WHOLE_LINE);
await nativeCursorTo(terminal, 0, undefined);
}
}
const LEVEL_COLORS = {
debug: green,
warn: yellow,
error: red,
};
function label(level, message) {
return `[@formatjs/cli] [${LEVEL_COLORS[level](level.toUpperCase())}] ${message}`;
}
export async function debug(message, ...args) {
if (process.env.LOG_LEVEL !== 'debug') {
return;
}
await clearLine(process.stderr);
await writeStderr(format(label('debug', message), ...args));
await writeStderr('\n');
}
export async function warn(message, ...args) {
await clearLine(process.stderr);
await writeStderr(format(label('warn', message), ...args));
await writeStderr('\n');
}
export async function error(message, ...args) {
await clearLine(process.stderr);
await writeStderr(format(label('error', message), ...args));
await writeStderr('\n');
}
export function getStdinAsString() {
let result = '';
return new Promise(resolve => {
process.stdin.setEncoding('utf-8');
process.stdin.on('readable', () => {
let chunk;
while ((chunk = process.stdin.read())) {
result += chunk;
}
});
process.stdin.on('end', () => {
resolve(result);
});
});
}