From 9df91cc4dd737c36eae825867c5d30d3c256f3d8 Mon Sep 17 00:00:00 2001 From: tim Date: Tue, 22 Aug 2017 20:06:48 +0200 Subject: [PATCH] Breaking: Replace chalk with ansi-colors (closes #125) --- index.js | 26 +++++++++--------- lib/shared/ansi.js | 38 +++++++++++++++++++++++++++ lib/shared/cliOptions.js | 36 ++++++++++++------------- lib/shared/log/blacklistError.js | 4 +-- lib/shared/log/tasks.js | 14 +++++----- lib/shared/log/verify.js | 8 +++--- lib/versioned/^3.7.0/index.js | 6 ++--- lib/versioned/^3.7.0/log/events.js | 16 +++++------ lib/versioned/^4.0.0-alpha.1/index.js | 8 +++--- lib/versioned/^4.0.0-alpha.2/index.js | 8 +++--- lib/versioned/^4.0.0/index.js | 8 +++--- lib/versioned/^4.0.0/log/events.js | 14 +++++----- lib/versioned/^4.0.0/log/syncTask.js | 8 +++--- package.json | 3 ++- 14 files changed, 118 insertions(+), 79 deletions(-) create mode 100644 lib/shared/ansi.js diff --git a/index.js b/index.js index 6031c9cf..dc586c75 100644 --- a/index.js +++ b/index.js @@ -3,12 +3,12 @@ var fs = require('fs'); var path = require('path'); var log = require('gulplog'); -var chalk = require('chalk'); var yargs = require('yargs'); var Liftoff = require('liftoff'); var interpret = require('interpret'); var v8flags = require('v8flags'); var findRange = require('semver-greatest-satisfied-range'); +var ansi = require('./lib/shared/ansi'); var exit = require('./lib/shared/exit'); var tildify = require('./lib/shared/tildify'); var cliOptions = require('./lib/shared/cliOptions'); @@ -53,8 +53,8 @@ var cli = new Liftoff({ }); var usage = - '\n' + chalk.bold('Usage:') + - ' gulp ' + chalk.blue('[options]') + ' tasks'; + '\n' + ansi.bold('Usage:') + + ' gulp ' + ansi.blue('[options]') + ' tasks'; var parser = yargs.usage(usage, cliOptions); var opts = parser.argv; @@ -63,16 +63,16 @@ var opts = parser.argv; toConsole(log, opts); cli.on('require', function(name) { - log.info('Requiring external module', chalk.magenta(name)); + log.info('Requiring external module', ansi.magenta(name)); }); cli.on('requireFail', function(name) { - log.error(chalk.red('Failed to load external module'), chalk.magenta(name)); + log.error(ansi.red('Failed to load external module'), ansi.magenta(name)); }); cli.on('respawn', function(flags, child) { - var nodeFlags = chalk.magenta(flags.join(', ')); - var pid = chalk.magenta(child.pid); + var nodeFlags = ansi.magenta(flags.join(', ')); + var pid = ansi.magenta(child.pid); log.info('Node flags detected:', nodeFlags); log.info('Respawned to PID:', pid); }); @@ -138,15 +138,15 @@ function handleArguments(env) { if (!env.modulePath) { log.error( - chalk.red('Local gulp not found in'), - chalk.magenta(tildify(env.cwd)) + ansi.red('Local gulp not found in'), + ansi.magenta(tildify(env.cwd)) ); - log.error(chalk.red('Try running: npm install gulp')); + log.error(ansi.red('Try running: npm install gulp')); exit(1); } if (!env.configPath) { - log.error(chalk.red('No gulpfile found')); + log.error(ansi.red('No gulpfile found')); exit(1); } @@ -156,7 +156,7 @@ function handleArguments(env) { process.chdir(env.cwd); log.info( 'Working directory changed to', - chalk.magenta(tildify(env.cwd)) + ansi.magenta(tildify(env.cwd)) ); } @@ -165,7 +165,7 @@ function handleArguments(env) { if (!range) { return log.error( - chalk.red('Unsupported gulp version', env.modulePackage.version) + ansi.red('Unsupported gulp version', env.modulePackage.version) ); } diff --git a/lib/shared/ansi.js b/lib/shared/ansi.js new file mode 100644 index 00000000..587bda64 --- /dev/null +++ b/lib/shared/ansi.js @@ -0,0 +1,38 @@ +'use strict'; + +var colors = require('ansi-colors'); +var supportsColor = require('color-support'); + +var hasColors = colorize(); + +module.exports = { + red: hasColors ? colors.red : noColor, + green: hasColors ? colors.green : noColor, + blue: hasColors ? colors.blue : noColor, + magenta: hasColors ? colors.magenta : noColor, + cyan: hasColors ? colors.cyan : noColor, + white: hasColors ? colors.white : noColor, + gray: hasColors ? colors.gray : noColor, + bgred: hasColors ? colors.bgred : noColor, + bold: hasColors ? colors.bold : noColor, +}; + +function noColor(message) { + return message; +} + +function hasFlag(flag) { + return (process.argv.indexOf('--' + flag) !== -1); +} + +function colorize() { + if (hasFlag('no-color')) { + return false; + } + + if (hasFlag('color')) { + return true; + } + + return supportsColor(); +} diff --git a/lib/shared/cliOptions.js b/lib/shared/cliOptions.js index ccc20c88..76408294 100644 --- a/lib/shared/cliOptions.js +++ b/lib/shared/cliOptions.js @@ -1,24 +1,24 @@ 'use strict'; -var chalk = require('chalk'); +var ansi = require('./ansi'); module.exports = { help: { alias: 'h', type: 'boolean', - desc: chalk.gray( + desc: ansi.gray( 'Show this help.'), }, version: { alias: 'v', type: 'boolean', - desc: chalk.gray( + desc: ansi.gray( 'Print the global and local gulp versions.'), }, require: { type: 'string', requiresArg: true, - desc: chalk.gray( + desc: ansi.gray( 'Will require a module before running the gulpfile. ' + 'This is useful for transpilers but also has other applications.'), }, @@ -26,35 +26,35 @@ module.exports = { alias: 'f', type: 'string', requiresArg: true, - desc: chalk.gray( + desc: ansi.gray( 'Manually set path of gulpfile. Useful if you have multiple gulpfiles. ' + 'This will set the CWD to the gulpfile directory as well.'), }, cwd: { type: 'string', requiresArg: true, - desc: chalk.gray( + desc: ansi.gray( 'Manually set the CWD. The search for the gulpfile, ' + 'as well as the relativity of all requires will be from here.'), }, verify: { - desc: chalk.gray( + desc: ansi.gray( 'Will verify plugins referenced in project\'s package.json against ' + 'the plugins blacklist.'), }, tasks: { alias: 'T', type: 'boolean', - desc: chalk.gray( + desc: ansi.gray( 'Print the task dependency tree for the loaded gulpfile.'), }, 'tasks-simple': { type: 'boolean', - desc: chalk.gray( + desc: ansi.gray( 'Print a plaintext list of tasks for the loaded gulpfile.'), }, 'tasks-json': { - desc: chalk.gray( + desc: ansi.gray( 'Print the task dependency tree, ' + 'in JSON format, for the loaded gulpfile.'), }, @@ -62,41 +62,41 @@ module.exports = { alias: 'depth', type: 'number', requiresArg: true, - desc: chalk.gray( + desc: ansi.gray( 'Specify the depth of the task dependency tree.'), }, 'compact-tasks': { type: 'boolean', - desc: chalk.gray( + desc: ansi.gray( 'Reduce the output of task dependency tree by printing ' + 'only top tasks and their child tasks.'), }, 'sort-tasks': { type: 'boolean', - desc: chalk.gray( + desc: ansi.gray( 'Will sort top tasks of task dependency tree.'), }, color: { type: 'boolean', - desc: chalk.gray( + desc: ansi.gray( 'Will force gulp and gulp plugins to display colors, ' + 'even when no color support is detected.'), }, 'no-color': { type: 'boolean', - desc: chalk.gray( + desc: ansi.gray( 'Will force gulp and gulp plugins to not display colors, ' + 'even when color support is detected.'), }, silent: { alias: 'S', type: 'boolean', - desc: chalk.gray( + desc: ansi.gray( 'Suppress all gulp logging.'), }, continue: { type: 'boolean', - desc: chalk.gray( + desc: ansi.gray( 'Continue execution of tasks upon failure.'), }, 'log-level': { @@ -104,7 +104,7 @@ module.exports = { // Type isn't needed because count acts as a boolean count: true, // Can't use `default` because it seems to be off by one - desc: chalk.gray( + desc: ansi.gray( 'Set the loglevel. -L for least verbose and -LLLL for most verbose. ' + '-LLL is default.'), }, diff --git a/lib/shared/log/blacklistError.js b/lib/shared/log/blacklistError.js index c3c5bf32..396436ac 100644 --- a/lib/shared/log/blacklistError.js +++ b/lib/shared/log/blacklistError.js @@ -1,12 +1,12 @@ 'use strict'; -var chalk = require('chalk'); var log = require('gulplog'); +var ansi = require('../ansi'); var exit = require('../exit'); function logBlacklistError(err) { - log.error(chalk.red('Error: failed to retrieve plugins black-list')); + log.error(ansi.red('Error: failed to retrieve plugins black-list')); log.error(err.message); // Avoid duplicating for each version exit(1); } diff --git a/lib/shared/log/tasks.js b/lib/shared/log/tasks.js index 697066db..93451ac5 100644 --- a/lib/shared/log/tasks.js +++ b/lib/shared/log/tasks.js @@ -1,12 +1,12 @@ 'use strict'; var archy = require('archy'); -var chalk = require('chalk'); var log = require('gulplog'); var sortBy = require('array-sort'); var isObject = require('isobject'); +var ansi = require('../ansi'); var copyTree = require('./copy-tree'); function logTasks(tree, opts, getTask) { @@ -140,20 +140,20 @@ function printTreeList(lines, spacer, lineInfos) { lines.forEach(function(branch, index) { var info = lineInfos[index]; - var line = chalk.white(branch); + var line = ansi.white(branch); if (info.type === 'top') { - line += chalk.cyan(info.name); + line += ansi.cyan(info.name); if (info.desc.length > 0) { - line += spacer(index) + chalk.white(info.desc); + line += spacer(index) + ansi.white(info.desc); } } else if (info.type === 'option') { - line += chalk.magenta(info.name); + line += ansi.magenta(info.name); if (info.desc.length > 0) { - line += spacer(index) + chalk.white('…' + info.desc); + line += spacer(index) + ansi.white('…' + info.desc); } } else { // If (info.type === 'child') { - line += chalk.white(info.name); + line += ansi.white(info.name); } log.info(line); diff --git a/lib/shared/log/verify.js b/lib/shared/log/verify.js index 795867a7..71046237 100644 --- a/lib/shared/log/verify.js +++ b/lib/shared/log/verify.js @@ -1,8 +1,8 @@ 'use strict'; -var chalk = require('chalk'); var log = require('gulplog'); +var ansi = require('../ansi'); var exit = require('../exit'); function logVerify(blacklisted) { @@ -10,16 +10,16 @@ function logVerify(blacklisted) { if (!pluginNames.length) { log.info( - chalk.green('There are no blacklisted plugins in this project') + ansi.green('There are no blacklisted plugins in this project') ); exit(0); } - log.warn(chalk.red('Blacklisted plugins found in this project:')); + log.warn(ansi.red('Blacklisted plugins found in this project:')); pluginNames.map(function(pluginName) { var reason = blacklisted[pluginName]; - log.warn(chalk.bgRed(pluginName) + ': ' + reason); + log.warn(ansi.bgred(pluginName) + ': ' + reason); }); exit(1); diff --git a/lib/versioned/^3.7.0/index.js b/lib/versioned/^3.7.0/index.js index c2a91228..040d113c 100644 --- a/lib/versioned/^3.7.0/index.js +++ b/lib/versioned/^3.7.0/index.js @@ -1,6 +1,5 @@ 'use strict'; -var chalk = require('chalk'); var log = require('gulplog'); var stdout = require('mute-stdout'); @@ -8,6 +7,7 @@ var taskTree = require('./taskTree'); var tildify = require('../../shared/tildify'); var logTasks = require('../../shared/log/tasks'); +var ansi = require('../../shared/ansi'); var logEvents = require('./log/events'); var logTasksSimple = require('./log/tasksSimple'); var registerExports = require('../../shared/registerExports'); @@ -23,7 +23,7 @@ function execute(opts, env, config) { // This is what actually loads up the gulpfile var exported = require(env.configPath); - log.info('Using gulpfile', chalk.magenta(tildify(env.configPath))); + log.info('Using gulpfile', ansi.magenta(tildify(env.configPath))); var gulpInst = require(env.modulePath); logEvents(gulpInst); @@ -44,7 +44,7 @@ function execute(opts, env, config) { if (config.description && typeof config.description === 'string') { tree.label = config.description; } else { - tree.label = 'Tasks for ' + chalk.magenta(tildify(env.configPath)); + tree.label = 'Tasks for ' + ansi.magenta(tildify(env.configPath)); } return logTasks(tree, opts, function(task) { return gulpInst.tasks[task].fn; diff --git a/lib/versioned/^3.7.0/log/events.js b/lib/versioned/^3.7.0/log/events.js index 3b15be3b..f88136d0 100644 --- a/lib/versioned/^3.7.0/log/events.js +++ b/lib/versioned/^3.7.0/log/events.js @@ -1,9 +1,9 @@ 'use strict'; -var chalk = require('chalk'); var log = require('gulplog'); var prettyTime = require('pretty-hrtime'); +var ansi = require('../../../shared/ansi'); var exit = require('../../../shared/exit'); var formatError = require('../formatError'); @@ -26,14 +26,14 @@ function logEvents(gulpInst) { gulpInst.on('task_start', function(e) { // TODO: batch these // so when 5 tasks start at once it only logs one time with all 5 - log.info('Starting', '\'' + chalk.cyan(e.task) + '\'...'); + log.info('Starting', '\'' + ansi.cyan(e.task) + '\'...'); }); gulpInst.on('task_stop', function(e) { var time = prettyTime(e.hrDuration); log.info( - 'Finished', '\'' + chalk.cyan(e.task) + '\'', - 'after', chalk.magenta(time) + 'Finished', '\'' + ansi.cyan(e.task) + '\'', + 'after', ansi.magenta(time) ); }); @@ -41,16 +41,16 @@ function logEvents(gulpInst) { var msg = formatError(e); var time = prettyTime(e.hrDuration); log.error( - '\'' + chalk.cyan(e.task) + '\'', - chalk.red('errored after'), - chalk.magenta(time) + '\'' + ansi.cyan(e.task) + '\'', + ansi.red('errored after'), + ansi.magenta(time) ); log.error(msg); }); gulpInst.on('task_not_found', function(err) { log.error( - chalk.red('Task \'' + err.task + '\' is not in your gulpfile') + ansi.red('Task \'' + err.task + '\' is not in your gulpfile') ); log.error('Please check the documentation for proper gulpfile formatting'); exit(1); diff --git a/lib/versioned/^4.0.0-alpha.1/index.js b/lib/versioned/^4.0.0-alpha.1/index.js index e255a03d..78489644 100644 --- a/lib/versioned/^4.0.0-alpha.1/index.js +++ b/lib/versioned/^4.0.0-alpha.1/index.js @@ -3,9 +3,9 @@ var fs = require('fs'); var log = require('gulplog'); -var chalk = require('chalk'); var stdout = require('mute-stdout'); +var ansi = require('../../shared/ansi'); var exit = require('../../shared/exit'); var tildify = require('../../shared/tildify'); @@ -50,7 +50,7 @@ function execute(opts, env, config) { if (config.description && typeof config.description === 'string') { tree.label = config.description; } else { - tree.label = 'Tasks for ' + chalk.magenta(tildify(env.configPath)); + tree.label = 'Tasks for ' + ansi.magenta(tildify(env.configPath)); } tree.nodes = gulpInst.tree({ deep: true }); return logTasks(tree, opts, function(taskname) { @@ -73,14 +73,14 @@ function execute(opts, env, config) { return fs.writeFileSync(opts.tasksJson, output, 'utf-8'); } try { - log.info('Using gulpfile', chalk.magenta(tildify(env.configPath))); + log.info('Using gulpfile', ansi.magenta(tildify(env.configPath))); gulpInst.parallel(toRun)(function(err) { if (err) { exit(1); } }); } catch (err) { - log.error(chalk.red(err.message)); + log.error(ansi.red(err.message)); log.error('To list available tasks, try running: gulp --tasks'); exit(1); } diff --git a/lib/versioned/^4.0.0-alpha.2/index.js b/lib/versioned/^4.0.0-alpha.2/index.js index 70eddf39..441452cf 100644 --- a/lib/versioned/^4.0.0-alpha.2/index.js +++ b/lib/versioned/^4.0.0-alpha.2/index.js @@ -3,9 +3,9 @@ var fs = require('fs'); var log = require('gulplog'); -var chalk = require('chalk'); var stdout = require('mute-stdout'); +var ansi = require('../../shared/ansi'); var exit = require('../../shared/exit'); var tildify = require('../../shared/tildify'); @@ -52,7 +52,7 @@ function execute(opts, env, config) { if (config.description && typeof config.description === 'string') { tree.label = config.description; } else { - tree.label = 'Tasks for ' + chalk.magenta(tildify(env.configPath)); + tree.label = 'Tasks for ' + ansi.magenta(tildify(env.configPath)); } return logTasks(tree, opts, getTask(gulpInst)); @@ -73,14 +73,14 @@ function execute(opts, env, config) { return fs.writeFileSync(opts.tasksJson, output, 'utf-8'); } try { - log.info('Using gulpfile', chalk.magenta(tildify(env.configPath))); + log.info('Using gulpfile', ansi.magenta(tildify(env.configPath))); gulpInst.parallel(toRun)(function(err) { if (err) { exit(1); } }); } catch (err) { - log.error(chalk.red(err.message)); + log.error(ansi.red(err.message)); log.error('To list available tasks, try running: gulp --tasks'); exit(1); } diff --git a/lib/versioned/^4.0.0/index.js b/lib/versioned/^4.0.0/index.js index 19408365..af0ccab0 100644 --- a/lib/versioned/^4.0.0/index.js +++ b/lib/versioned/^4.0.0/index.js @@ -3,9 +3,9 @@ var fs = require('fs'); var log = require('gulplog'); -var chalk = require('chalk'); var stdout = require('mute-stdout'); +var ansi = require('../../shared/ansi'); var exit = require('../../shared/exit'); var tildify = require('../../shared/tildify'); @@ -52,7 +52,7 @@ function execute(opts, env, config) { if (config.description && typeof config.description === 'string') { tree.label = config.description; } else { - tree.label = 'Tasks for ' + chalk.magenta(tildify(env.configPath)); + tree.label = 'Tasks for ' + ansi.magenta(tildify(env.configPath)); } return logTasks(tree, opts, getTask(gulpInst)); @@ -73,14 +73,14 @@ function execute(opts, env, config) { return fs.writeFileSync(opts.tasksJson, output, 'utf-8'); } try { - log.info('Using gulpfile', chalk.magenta(tildify(env.configPath))); + log.info('Using gulpfile', ansi.magenta(tildify(env.configPath))); gulpInst.parallel(toRun)(function(err) { if (err) { exit(1); } }); } catch (err) { - log.error(chalk.red(err.message)); + log.error(ansi.red(err.message)); log.error('To list available tasks, try running: gulp --tasks'); exit(1); } diff --git a/lib/versioned/^4.0.0/log/events.js b/lib/versioned/^4.0.0/log/events.js index 8c2f2d22..64938dcc 100644 --- a/lib/versioned/^4.0.0/log/events.js +++ b/lib/versioned/^4.0.0/log/events.js @@ -1,9 +1,9 @@ 'use strict'; var log = require('gulplog'); -var chalk = require('chalk'); var prettyTime = require('pretty-hrtime'); var formatError = require('../formatError'); +var ansi = require('../../../shared/ansi'); // Wire up logging events function logEvents(gulpInst) { @@ -14,15 +14,15 @@ function logEvents(gulpInst) { // TODO: batch these // so when 5 tasks start at once it only logs one time with all 5 var level = evt.branch ? 'debug' : 'info'; - log[level]('Starting', '\'' + chalk.cyan(evt.name) + '\'...'); + log[level]('Starting', '\'' + ansi.cyan(evt.name) + '\'...'); }); gulpInst.on('stop', function(evt) { var time = prettyTime(evt.duration); var level = evt.branch ? 'debug' : 'info'; log[level]( - 'Finished', '\'' + chalk.cyan(evt.name) + '\'', - 'after', chalk.magenta(time) + 'Finished', '\'' + ansi.cyan(evt.name) + '\'', + 'after', ansi.magenta(time) ); }); @@ -31,9 +31,9 @@ function logEvents(gulpInst) { var time = prettyTime(evt.duration); var level = evt.branch ? 'debug' : 'error'; log[level]( - '\'' + chalk.cyan(evt.name) + '\'', - chalk.red('errored after'), - chalk.magenta(time) + '\'' + ansi.cyan(evt.name) + '\'', + ansi.red('errored after'), + ansi.magenta(time) ); // If we haven't logged this before, log it and add to list diff --git a/lib/versioned/^4.0.0/log/syncTask.js b/lib/versioned/^4.0.0/log/syncTask.js index 493654fc..1932e89c 100644 --- a/lib/versioned/^4.0.0/log/syncTask.js +++ b/lib/versioned/^4.0.0/log/syncTask.js @@ -1,7 +1,7 @@ 'use strict'; var log = require('gulplog'); -var chalk = require('chalk'); +var ansi = require('../../../shared/ansi'); var tasks = {}; @@ -17,11 +17,11 @@ function warn() { }).join(', '); log.warn( - chalk.red('The following tasks did not complete:'), - chalk.cyan(taskNames) + ansi.red('The following tasks did not complete:'), + ansi.cyan(taskNames) ); log.warn( - chalk.red('Did you forget to signal async completion?') + ansi.red('Did you forget to signal async completion?') ); } diff --git a/package.json b/package.json index c667a1e5..5f1d768e 100644 --- a/package.json +++ b/package.json @@ -32,10 +32,11 @@ "changelog": "github-changes -o gulpjs -r gulp-cli -b master -f ./CHANGELOG.md --order-semver --use-commit-body" }, "dependencies": { + "ansi-colors": "^1.0.1", "archy": "^1.0.0", "array-sort": "^1.0.0", - "chalk": "^1.1.0", "concat-stream": "^1.6.0", + "color-support": "^1.1.3", "copy-props": "^2.0.1", "fancy-log": "^1.1.0", "gulplog": "^1.0.0",