Skip to content

Commit

Permalink
New: Introduce gulplog & wire up listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
kahlil authored and phated committed Dec 21, 2017
1 parent 7876f7e commit b14cfd9
Show file tree
Hide file tree
Showing 14 changed files with 182 additions and 51 deletions.
18 changes: 18 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# EditorConfig is Awesome: http://editorconfig.org

# Top-most EditorConfig file.
root = true

# Unix-style newlines with a newline ending every file.
[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
indent_style = space
indent_size = 2
trim_trailing_whitespace = true

# Don't trim whitespace in Markdown in order to be able
# to do two spaces for line breaks.
[*.md]
trim_trailing_whitespace = false
36 changes: 16 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'use strict';
var fs = require('fs');
var path = require('path');
var gutil = require('gulp-util');
var log = require('gulplog');
var chalk = require('chalk');
var yargs = require('yargs');
var Liftoff = require('liftoff');
Expand All @@ -17,6 +17,7 @@ var completion = require('./lib/shared/completion');
var verifyDeps = require('./lib/shared/verifyDependencies');
var cliVersion = require('./package.json').version;
var getBlacklist = require('./lib/shared/getBlacklist');
var toConsole = require('./lib/shared/log/toConsole');

// Logging functions
var logVerify = require('./lib/shared/log/verify');
Expand Down Expand Up @@ -51,27 +52,22 @@ if (opts.continue) {
process.env.UNDERTAKER_SETTLE = 'true';
}

// This is a hold-over until we have a better logging system
// with log levels
var shouldLog = !opts.silent && !opts.tasksSimple;

if (!shouldLog) {
gutil.log = function() {};
}
// Set up event listeners for logging.
toConsole(log, opts);

cli.on('require', function(name) {
gutil.log('Requiring external module', chalk.magenta(name));
log.info('Requiring external module', chalk.magenta(name));
});

cli.on('requireFail', function(name) {
gutil.log(chalk.red('Failed to load external module'), chalk.magenta(name));
log.error(chalk.red('Failed to load external module'), chalk.magenta(name));
});

cli.on('respawn', function(flags, child) {
var nodeFlags = chalk.magenta(flags.join(', '));
var pid = chalk.magenta(child.pid);
gutil.log('Node flags detected:', nodeFlags);
gutil.log('Respawned to PID:', pid);
log.info('Node flags detected:', nodeFlags);
log.info('Respawned to PID:', pid);
});

function run() {
Expand All @@ -93,9 +89,9 @@ function handleArguments(env) {
}

if (opts.version) {
gutil.log('CLI version', cliVersion);
log.info('CLI version', cliVersion);
if (env.modulePackage && typeof env.modulePackage.version !== 'undefined') {
gutil.log('Local version', env.modulePackage.version);
log.info('Local version', env.modulePackage.version);
}
exit(0);
}
Expand All @@ -105,7 +101,7 @@ function handleArguments(env) {
if (path.resolve(pkgPath) !== path.normalize(pkgPath)) {
pkgPath = path.join(env.configBase, pkgPath);
}
gutil.log('Verifying plugins in ' + pkgPath);
log.info('Verifying plugins in ' + pkgPath);
return getBlacklist(function(err, blacklist) {
if (err) {
return logBlacklistError(err);
Expand All @@ -118,24 +114,24 @@ function handleArguments(env) {
}

if (!env.modulePath) {
gutil.log(
log.error(
chalk.red('Local gulp not found in'),
chalk.magenta(tildify(env.cwd))
);
gutil.log(chalk.red('Try running: npm install gulp'));
log.error(chalk.red('Try running: npm install gulp'));
exit(1);
}

if (!env.configPath) {
gutil.log(chalk.red('No gulpfile found'));
log.error(chalk.red('No gulpfile found'));
exit(1);
}

// Chdir before requiring gulpfile to make sure
// we let them chdir as needed
if (process.cwd() !== env.cwd) {
process.chdir(env.cwd);
gutil.log(
log.info(
'Working directory changed to',
chalk.magenta(tildify(env.cwd))
);
Expand All @@ -145,7 +141,7 @@ function handleArguments(env) {
var range = findRange(env.modulePackage.version, ranges);

if (!range) {
return gutil.log(
return log.error(
chalk.red('Unsupported gulp version', env.modulePackage.version)
);
}
Expand Down
9 changes: 9 additions & 0 deletions lib/shared/cliOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ module.exports = {
'even when color support is detected.'),
},
silent: {
alias: 's',
type: 'boolean',
desc: chalk.gray(
'Suppress all gulp logging.'),
Expand All @@ -80,4 +81,12 @@ module.exports = {
desc: chalk.gray(
'Continue execution of tasks upon failure.'),
},
loglevel: {
alias: 'l',
type: 'boolean',
count: true,
desc: chalk.gray(
'Set the loglevel. -l for least verbose and -llll for most verbose.' +
' -lll is default.'),
},
};
6 changes: 3 additions & 3 deletions lib/shared/log/blacklistError.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict';

var chalk = require('chalk');
var gutil = require('gulp-util');
var log = require('gulplog');

var exit = require('../exit');

function logBlacklistError(err) {
gutil.log(chalk.red('Error: failed to retrieve plugins black-list'));
gutil.log(err.message); // Avoid duplicating for each version
log.error(chalk.red('Error: failed to retrieve plugins black-list'));
log.error(err.message); // Avoid duplicating for each version
exit(1);
}

Expand Down
8 changes: 4 additions & 4 deletions lib/shared/log/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var archy = require('archy');
var chalk = require('chalk');
var gutil = require('gulp-util');
var log = require('gulplog');

function logTasks(tree, getDescription) {
var padding = 0;
Expand All @@ -12,7 +12,7 @@ function logTasks(tree, getDescription) {
.filter(function(v, i) {
// Log first line as is
if (i === 0) {
gutil.log(v);
log.info(v);
return false;
}
// Search for longest line
Expand All @@ -26,12 +26,12 @@ function logTasks(tree, getDescription) {

// Log dependencies as is
if (rdependency.test(v)) {
gutil.log(v);
log.info(v);
return;
}

// Pretty task with optional description
gutil.log(
log.info(
line[0] + ' ' + chalk.cyan(task) +
Array(padding + 3 - v.length).join(' ') +
(getDescription(task) || '')
Expand Down
40 changes: 40 additions & 0 deletions lib/shared/log/toConsole.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

var fancyLog = require('fancy-log');

// The sorting of the levels is
// significant.
var levels = [
'error', // -l: Logs error events.
'warn', // -ll: Logs warn and error events.
'info', // -lll: Logs info, warn and error events.
'debug', // -llll: Logs all log levels.
];

function toConsole(log, opts) {
// Return immediately if logging is
// not desired.
if (opts.tasksSimple || opts.silent) {
return;
}

// For some reason yargs sets
// the default count for opts.loglevel to 2
// instead of 0 when we configure count: true
// in the cliOptions.
var loglevel = (opts.loglevel > 2) ? opts.loglevel - 2 : 3;

levels
.filter(function(item, i) {
return i < loglevel;
})
.forEach(function(level) {
if (level === 'error') {
log.on(level, fancyLog.error);
} else {
log.on(level, fancyLog);
}
});
}

module.exports = toConsole;
8 changes: 4 additions & 4 deletions lib/shared/log/verify.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
'use strict';

var chalk = require('chalk');
var gutil = require('gulp-util');
var log = require('gulplog');

var exit = require('../exit');

function logVerify(blacklisted) {
var pluginNames = Object.keys(blacklisted);

if (!pluginNames.length) {
gutil.log(
log.info(
chalk.green('There are no blacklisted plugins in this project')
);
exit(0);
}

gutil.log(chalk.red('Blacklisted plugins found in this project:'));
log.warn(chalk.red('Blacklisted plugins found in this project:'));

pluginNames.map(function(pluginName) {
var reason = blacklisted[pluginName];
gutil.log(chalk.bgRed(pluginName) + ': ' + reason);
log.warn(chalk.bgRed(pluginName) + ': ' + reason);
});

exit(1);
Expand Down
5 changes: 2 additions & 3 deletions lib/versioned/^3.7.0/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var chalk = require('chalk');
var gutil = require('gulp-util');
var log = require('gulplog');
var stdout = require('mute-stdout');
var tildify = require('tildify');

Expand All @@ -11,7 +11,6 @@ var logEvents = require('./log/events');
var logTasksSimple = require('./log/tasksSimple');

function execute(opts, env) {

var tasks = opts._;
var toRun = tasks.length ? tasks : ['default'];

Expand All @@ -22,7 +21,7 @@ function execute(opts, env) {

// This is what actually loads up the gulpfile
require(env.configPath);
gutil.log('Using gulpfile', chalk.magenta(tildify(env.configPath)));
log.info('Using gulpfile', chalk.magenta(tildify(env.configPath)));

var gulpInst = require(env.modulePath);
logEvents(gulpInst);
Expand Down
14 changes: 7 additions & 7 deletions lib/versioned/^3.7.0/log/events.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var chalk = require('chalk');
var gutil = require('gulp-util');
var log = require('gulplog');
var prettyTime = require('pretty-hrtime');

var exit = require('../../../shared/exit');
Expand All @@ -26,12 +26,12 @@ 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
gutil.log('Starting', '\'' + chalk.cyan(e.task) + '\'...');
log.info('Starting', '\'' + chalk.cyan(e.task) + '\'...');
});

gulpInst.on('task_stop', function(e) {
var time = prettyTime(e.hrDuration);
gutil.log(
log.info(
'Finished', '\'' + chalk.cyan(e.task) + '\'',
'after', chalk.magenta(time)
);
Expand All @@ -40,19 +40,19 @@ function logEvents(gulpInst) {
gulpInst.on('task_err', function(e) {
var msg = formatError(e);
var time = prettyTime(e.hrDuration);
gutil.log(
log.error(
'\'' + chalk.cyan(e.task) + '\'',
chalk.red('errored after'),
chalk.magenta(time)
);
gutil.log(msg);
log.error(msg);
});

gulpInst.on('task_not_found', function(err) {
gutil.log(
log.error(
chalk.red('Task \'' + err.task + '\' is not in your gulpfile')
);
gutil.log('Please check the documentation for proper gulpfile formatting');
log.error('Please check the documentation for proper gulpfile formatting');
exit(1);
});
}
Expand Down
8 changes: 4 additions & 4 deletions lib/versioned/^4.0.0-alpha.1/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

var fs = require('fs');

var log = require('gulplog');
var chalk = require('chalk');
var gutil = require('gulp-util');
var stdout = require('mute-stdout');
var tildify = require('tildify');

Expand Down Expand Up @@ -54,15 +54,15 @@ function execute(opts, env) {
}
}
try {
gutil.log('Using gulpfile', chalk.magenta(tildify(env.configPath)));
log.info('Using gulpfile', chalk.magenta(tildify(env.configPath)));
gulpInst.parallel(toRun)(function(err) {
if (err) {
exit(1);
}
});
} catch (err) {
gutil.log(chalk.red(err.message));
gutil.log(
log.error(chalk.red(err.message));
log.error(
'Please check the documentation for proper gulpfile formatting'
);
exit(1);
Expand Down
Loading

0 comments on commit b14cfd9

Please sign in to comment.