Skip to content

Commit

Permalink
Remove chalk hack (fixes #374)
Browse files Browse the repository at this point in the history
  • Loading branch information
valeriangalliat committed Feb 25, 2015
1 parent 1222446 commit f0e8003
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 34 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -79,7 +79,7 @@
],
"dependencies": {
"babel-runtime": "^4.4.0",
"chalk": "^0.5.0",
"chalk": "^1.0.0",
"concat-stream": "^1.4.7",
"docopt": "^0.4.1",
"glob": "^4.3.1",
Expand Down
48 changes: 15 additions & 33 deletions src/logger.js
@@ -1,7 +1,11 @@
import { is } from './utils';
import * as errors from './errors';
import { format as fmt } from 'util';
import chalk from 'chalk';
import chalkModule from 'chalk';

const chalk = new chalkModule.constructor({
enabled: process.stderr.isTTY,
});

// Special chars.
const chevron = '\xBB';
Expand Down Expand Up @@ -32,20 +36,16 @@ export default class Logger {
* Always log arguments as warning into stderr.
*/
warn(...args) {
chalkHack(() => {
let str = fmt(`${yellow} [WARNING] ${args.shift()}`, ...args);
this._stderr.write(`${str}\n`);
});
let str = fmt(`${yellow} [WARNING] ${args.shift()}`, ...args);
this._stderr.write(`${str}\n`);
}

/**
* Always log arguments as error into stderr.
*/
error(...args) {
chalkHack(() => {
let str = fmt(`${red} [ERROR] ${args.shift()}`, ...args);
this._stderr.write(`${str}\n`);
});
let str = fmt(`${red} [ERROR] ${args.shift()}`, ...args);
this._stderr.write(`${str}\n`);
}

/**
Expand Down Expand Up @@ -94,15 +94,13 @@ export default class Logger {
return f;
});

chalkHack(() => {
let str = fmt(
`${chalk.styles.grey.open}${chevron} [DEBUG] ${args.shift()}`,
...args,
chalk.styles.grey.close
);
let str = fmt(
`${chalkModule.styles.grey.open}${chevron} [DEBUG] ${args.shift()}`,
...args,
chalkModule.styles.grey.close
);

this._stderr.write(`${str}\n`);
});
this._stderr.write(`${str}\n`);
}
}

Expand Down Expand Up @@ -147,19 +145,3 @@ export function checkLogger(logger) {
debug: empty.debug,
};
}

/**
* Chalk don't allow us to create a new instance with our own `enabled`
* value (internal functions always reference the global export). Here
* we want to enable it if stderr is a TTY, but it's not acceptable to
* modify the global context for this purpose.
*
* So this hack will set `chalk.enabled` for the time of the synchronous
* callback execution, then reset it to whatever was its default value.
*/
function chalkHack(cb) {
let enabled = chalk.enabled;
chalk.enabled = process.stderr.isTTY;
cb();
chalk.enabled = enabled;
}

0 comments on commit f0e8003

Please sign in to comment.