Skip to content

Instantly share code, notes, and snippets.

@shqld
Last active August 19, 2019 07:29
Show Gist options
  • Save shqld/f2c3ea2b93b3960093fc60b911a5a773 to your computer and use it in GitHub Desktop.
Save shqld/f2c3ea2b93b3960093fc60b911a5a773 to your computer and use it in GitHub Desktop.
Colored console with chalk
import chalk from 'chalk';
export const initChalk = ({ error = chalk.redBright, warn = chalk.yellowBright } = {}) => {
// disable coloring e.g. when piped like `2> err.log`
if (typeof process !== 'undefined' && !process.stderr.isTTY) {
return;
}
if (error) {
const defaultError = console.error.bind(console);
console.error = (...args) => defaultError(error(args.shift()), ...args);
}
if (warn) {
const defaultWarn = console.warn.bind(console);
console.warn = (...args) => defaultWarn(warn(args.shift()), ...args);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment