Skip to content

Commit

Permalink
Fix: Suppress logging when --tasks-json, --help, --version flag…
Browse files Browse the repository at this point in the history
… is specified (closes #157) (#159)
  • Loading branch information
sttk authored and phated committed Mar 18, 2019
1 parent be9d25a commit a4236f2
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 20 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "gulp",
"rules": {
"max-len": [1, 90],
"max-statements": [1, 40],
"no-console": "off"
}
Expand Down
16 changes: 6 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ var usage =
var parser = yargs.usage(usage, cliOptions);
var opts = parser.argv;

// Set up event listeners for logging temporarily.
toConsole(log, opts);

cli.on('require', function(name) {
log.info('Requiring external module', ansi.magenta(name));
});
Expand All @@ -85,21 +82,22 @@ cli.on('respawn', function(flags, child) {
log.info('Respawned to PID:', pid);
});


function run() {
cli.prepare({
cwd: opts.cwd,
configPath: opts.gulpfile,
require: opts.require,
completion: opts.completion,
}, function(env) {

var cfgLoadOrder = ['home', 'cwd'];
var cfg = loadConfigFiles(env.configFiles['.gulp'], cfgLoadOrder);
opts = mergeConfigToCliFlags(opts, cfg);
env = mergeConfigToEnvFlags(env, cfg);
env.configProps = cfg;

// Set up event listeners for logging again after configuring.
toConsole(log, opts);

cli.execute(env, handleArguments);
});
}
Expand All @@ -117,18 +115,16 @@ function handleArguments(env) {
process.env.UNDERTAKER_SETTLE = 'true';
}

// Set up event listeners for logging again after configuring.
toConsole(log, opts);

if (opts.help) {
parser.showHelp(console.log);
exit(0);
}

// Anything that needs to print outside of the logging mechanism should use console.log
if (opts.version) {
log.info('CLI version', cliVersion);
console.log('CLI version', cliVersion);
if (env.modulePackage && typeof env.modulePackage.version !== 'undefined') {
log.info('Local version', env.modulePackage.version);
console.log('Local version', env.modulePackage.version);
}
exit(0);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/shared/log/to-console.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function toConsole(log, opts) {

// Return immediately if logging is
// not desired.
if (opts.tasksSimple || opts.silent) {
if (opts.tasksSimple || opts.tasksJson || opts.help || opts.version || opts.silent) {
// Keep from crashing process when silent.
log.on('error', noop);
return;
Expand Down
13 changes: 8 additions & 5 deletions test/config-flags-gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,14 @@ describe('config: flags.gulpfile', function() {
function cb(err, stdout, stderr) {
expect(err).toEqual(null);
expect(stderr).toEqual('');
expect(eraseTime(stdout)).toEqual(
'Requiring external module babel-register\n' +
'clean!\n' +
'build!\n' +
'');

var requiring = eraseTime(headLines(stdout, 1));
expect(requiring).toEqual('Requiring external module babel-register');
var clean = eraseTime(headLines(stdout, 1, 4));
expect(clean).toEqual('clean!');
var build = eraseTime(headLines(stdout, 1, 7));
expect(build).toEqual('build!');

done(err);
}
});
Expand Down
1 change: 0 additions & 1 deletion test/fixtures/config/flags/gulpfile/autoload/.gulp.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"flags": {
"silent": true,
"gulpfile": "other_folder/gulpfile-exports.babel.js"
}
}
43 changes: 43 additions & 0 deletions test/fixtures/gulpfiles/gulpfile-babel.babel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

var gulp = require('gulp');

function noop(cb) {
cb();
}

function described(cb) {
cb();
}

function delayed(cb) {
setTimeout(cb, 100);
}

function errorFunction() {
throw new Error('Error!');
}
function anon(cb) {
cb();
}

function notCompleting1() {
// Callback is not called
}

function notCompleting2() {
// Callback is not called
}

described.description = 'description';

gulp.task('test1', gulp.series(noop));
gulp.task('test2', gulp.series('test1', noop));
gulp.task('test3', gulp.series(described));
gulp.task('test4', gulp.series(errorFunction, anon));
gulp.task('test5', delayed);
gulp.task('test6', noop);
gulp.task('test7', notCompleting1);
gulp.task('test8', notCompleting2);

gulp.task('default', gulp.series('test1', 'test3', noop));
14 changes: 14 additions & 0 deletions test/flags-help.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,18 @@ describe('flag: --help', function() {
}
});

it('avoids printing "Requiring external module *"', function(done) {
runner({ verbose: false })
.gulp('--help --gulpfile ./test/fixtures/gulpfiles/gulpfile-babel.babel.js')
.run(cb);

function cb(err, stdout, stderr) {
expect(err).toEqual(null);
expect(stderr).toEqual('');
stdout = eraseFirstSpace(stdout);
expect(stdout).toEqual(outputText);
done(err);
}
});

});
16 changes: 15 additions & 1 deletion test/flags-tasks-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ describe('flag: --tasks-json', function() {
function cb(err, stdout, stderr) {
expect(err).toEqual(null);
expect(stderr).toEqual('');
stdout = skipLines(stdout, 1);
expect(JSON.parse(stdout)).toEqual(expected);
done(err);
}
Expand Down Expand Up @@ -48,4 +47,19 @@ describe('flag: --tasks-json', function() {
}
});

it('avoids printing "Requiring external module *"', function(done) {
// Disable the timeout for old node versions
this.timeout(0);

runner({ verbose: false })
.gulp('--tasks-json --gulpfile ./test/fixtures/gulpfiles/gulpfile-babel.babel.js')
.run(cb);

function cb(err, stdout, stderr) {
expect(err).toEqual(null);
expect(stderr).toEqual('');
expect(JSON.parse(stdout)).toEqual(expected);
done(err);
}
});
});
16 changes: 16 additions & 0 deletions test/flags-tasks-simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,20 @@ describe('flag: --tasks-simple', function() {
}
});

it('avoids printing "Requiring external module *"', function(done) {
// Disable the timeout for old node versions
this.timeout(0);

runner({ verbose: false })
.gulp('--tasks-simple --gulpfile ./test/fixtures/gulpfiles/gulpfile-babel.babel.js')
.run(cb);

function cb(err, stdout, stderr) {
expect(err).toEqual(null);
expect(stderr).toEqual('');
expect(stdout).toEqual(outputText);
done(err);
}
});

});
19 changes: 17 additions & 2 deletions test/flags-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

var expect = require('expect');
var runner = require('gulp-test-tools').gulpRunner;
var eraseTime = require('gulp-test-tools').eraseTime;

var cliVersion = require('../package.json').version;
var gulpVersion = require('gulp/package.json').version;
Expand All @@ -17,7 +16,23 @@ describe('flag: --version', function() {
function cb(err, stdout, stderr) {
expect(err).toEqual(null);
expect(stderr).toEqual('');
stdout = eraseTime(stdout);
expect(stdout).toEqual(
'CLI version ' + cliVersion + '\n' +
'Local version ' + gulpVersion + '\n' +
''
);
done(err);
}
});

it('avoids printing "Requiring external module *"', function(done) {
runner({ verbose: false })
.gulp('--version --gulpfile ./test/fixtures/gulpfiles/gulpfile-babel.babel.js')
.run(cb);

function cb(err, stdout, stderr) {
expect(err).toEqual(null);
expect(stderr).toEqual('');
expect(stdout).toEqual(
'CLI version ' + cliVersion + '\n' +
'Local version ' + gulpVersion + '\n' +
Expand Down

0 comments on commit a4236f2

Please sign in to comment.