Skip to content

Commit b14cfd9

Browse files
khalillecheltphated
authored andcommitted
New: Introduce gulplog & wire up listeners
1 parent 7876f7e commit b14cfd9

File tree

14 files changed

+182
-51
lines changed

14 files changed

+182
-51
lines changed

.editorconfig

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# EditorConfig is Awesome: http://editorconfig.org
2+
3+
# Top-most EditorConfig file.
4+
root = true
5+
6+
# Unix-style newlines with a newline ending every file.
7+
[*]
8+
end_of_line = lf
9+
insert_final_newline = true
10+
charset = utf-8
11+
indent_style = space
12+
indent_size = 2
13+
trim_trailing_whitespace = true
14+
15+
# Don't trim whitespace in Markdown in order to be able
16+
# to do two spaces for line breaks.
17+
[*.md]
18+
trim_trailing_whitespace = false

index.js

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
'use strict';
44
var fs = require('fs');
55
var path = require('path');
6-
var gutil = require('gulp-util');
6+
var log = require('gulplog');
77
var chalk = require('chalk');
88
var yargs = require('yargs');
99
var Liftoff = require('liftoff');
@@ -17,6 +17,7 @@ var completion = require('./lib/shared/completion');
1717
var verifyDeps = require('./lib/shared/verifyDependencies');
1818
var cliVersion = require('./package.json').version;
1919
var getBlacklist = require('./lib/shared/getBlacklist');
20+
var toConsole = require('./lib/shared/log/toConsole');
2021

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

54-
// This is a hold-over until we have a better logging system
55-
// with log levels
56-
var shouldLog = !opts.silent && !opts.tasksSimple;
57-
58-
if (!shouldLog) {
59-
gutil.log = function() {};
60-
}
55+
// Set up event listeners for logging.
56+
toConsole(log, opts);
6157

6258
cli.on('require', function(name) {
63-
gutil.log('Requiring external module', chalk.magenta(name));
59+
log.info('Requiring external module', chalk.magenta(name));
6460
});
6561

6662
cli.on('requireFail', function(name) {
67-
gutil.log(chalk.red('Failed to load external module'), chalk.magenta(name));
63+
log.error(chalk.red('Failed to load external module'), chalk.magenta(name));
6864
});
6965

7066
cli.on('respawn', function(flags, child) {
7167
var nodeFlags = chalk.magenta(flags.join(', '));
7268
var pid = chalk.magenta(child.pid);
73-
gutil.log('Node flags detected:', nodeFlags);
74-
gutil.log('Respawned to PID:', pid);
69+
log.info('Node flags detected:', nodeFlags);
70+
log.info('Respawned to PID:', pid);
7571
});
7672

7773
function run() {
@@ -93,9 +89,9 @@ function handleArguments(env) {
9389
}
9490

9591
if (opts.version) {
96-
gutil.log('CLI version', cliVersion);
92+
log.info('CLI version', cliVersion);
9793
if (env.modulePackage && typeof env.modulePackage.version !== 'undefined') {
98-
gutil.log('Local version', env.modulePackage.version);
94+
log.info('Local version', env.modulePackage.version);
9995
}
10096
exit(0);
10197
}
@@ -105,7 +101,7 @@ function handleArguments(env) {
105101
if (path.resolve(pkgPath) !== path.normalize(pkgPath)) {
106102
pkgPath = path.join(env.configBase, pkgPath);
107103
}
108-
gutil.log('Verifying plugins in ' + pkgPath);
104+
log.info('Verifying plugins in ' + pkgPath);
109105
return getBlacklist(function(err, blacklist) {
110106
if (err) {
111107
return logBlacklistError(err);
@@ -118,24 +114,24 @@ function handleArguments(env) {
118114
}
119115

120116
if (!env.modulePath) {
121-
gutil.log(
117+
log.error(
122118
chalk.red('Local gulp not found in'),
123119
chalk.magenta(tildify(env.cwd))
124120
);
125-
gutil.log(chalk.red('Try running: npm install gulp'));
121+
log.error(chalk.red('Try running: npm install gulp'));
126122
exit(1);
127123
}
128124

129125
if (!env.configPath) {
130-
gutil.log(chalk.red('No gulpfile found'));
126+
log.error(chalk.red('No gulpfile found'));
131127
exit(1);
132128
}
133129

134130
// Chdir before requiring gulpfile to make sure
135131
// we let them chdir as needed
136132
if (process.cwd() !== env.cwd) {
137133
process.chdir(env.cwd);
138-
gutil.log(
134+
log.info(
139135
'Working directory changed to',
140136
chalk.magenta(tildify(env.cwd))
141137
);
@@ -145,7 +141,7 @@ function handleArguments(env) {
145141
var range = findRange(env.modulePackage.version, ranges);
146142

147143
if (!range) {
148-
return gutil.log(
144+
return log.error(
149145
chalk.red('Unsupported gulp version', env.modulePackage.version)
150146
);
151147
}

lib/shared/cliOptions.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ module.exports = {
7171
'even when color support is detected.'),
7272
},
7373
silent: {
74+
alias: 's',
7475
type: 'boolean',
7576
desc: chalk.gray(
7677
'Suppress all gulp logging.'),
@@ -80,4 +81,12 @@ module.exports = {
8081
desc: chalk.gray(
8182
'Continue execution of tasks upon failure.'),
8283
},
84+
loglevel: {
85+
alias: 'l',
86+
type: 'boolean',
87+
count: true,
88+
desc: chalk.gray(
89+
'Set the loglevel. -l for least verbose and -llll for most verbose.' +
90+
' -lll is default.'),
91+
},
8392
};

lib/shared/log/blacklistError.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
'use strict';
22

33
var chalk = require('chalk');
4-
var gutil = require('gulp-util');
4+
var log = require('gulplog');
55

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

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

lib/shared/log/tasks.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
var archy = require('archy');
44
var chalk = require('chalk');
5-
var gutil = require('gulp-util');
5+
var log = require('gulplog');
66

77
function logTasks(tree, getDescription) {
88
var padding = 0;
@@ -12,7 +12,7 @@ function logTasks(tree, getDescription) {
1212
.filter(function(v, i) {
1313
// Log first line as is
1414
if (i === 0) {
15-
gutil.log(v);
15+
log.info(v);
1616
return false;
1717
}
1818
// Search for longest line
@@ -26,12 +26,12 @@ function logTasks(tree, getDescription) {
2626

2727
// Log dependencies as is
2828
if (rdependency.test(v)) {
29-
gutil.log(v);
29+
log.info(v);
3030
return;
3131
}
3232

3333
// Pretty task with optional description
34-
gutil.log(
34+
log.info(
3535
line[0] + ' ' + chalk.cyan(task) +
3636
Array(padding + 3 - v.length).join(' ') +
3737
(getDescription(task) || '')

lib/shared/log/toConsole.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
3+
var fancyLog = require('fancy-log');
4+
5+
// The sorting of the levels is
6+
// significant.
7+
var levels = [
8+
'error', // -l: Logs error events.
9+
'warn', // -ll: Logs warn and error events.
10+
'info', // -lll: Logs info, warn and error events.
11+
'debug', // -llll: Logs all log levels.
12+
];
13+
14+
function toConsole(log, opts) {
15+
// Return immediately if logging is
16+
// not desired.
17+
if (opts.tasksSimple || opts.silent) {
18+
return;
19+
}
20+
21+
// For some reason yargs sets
22+
// the default count for opts.loglevel to 2
23+
// instead of 0 when we configure count: true
24+
// in the cliOptions.
25+
var loglevel = (opts.loglevel > 2) ? opts.loglevel - 2 : 3;
26+
27+
levels
28+
.filter(function(item, i) {
29+
return i < loglevel;
30+
})
31+
.forEach(function(level) {
32+
if (level === 'error') {
33+
log.on(level, fancyLog.error);
34+
} else {
35+
log.on(level, fancyLog);
36+
}
37+
});
38+
}
39+
40+
module.exports = toConsole;

lib/shared/log/verify.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
'use strict';
22

33
var chalk = require('chalk');
4-
var gutil = require('gulp-util');
4+
var log = require('gulplog');
55

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

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

1111
if (!pluginNames.length) {
12-
gutil.log(
12+
log.info(
1313
chalk.green('There are no blacklisted plugins in this project')
1414
);
1515
exit(0);
1616
}
1717

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

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

2525
exit(1);

lib/versioned/^3.7.0/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
var chalk = require('chalk');
4-
var gutil = require('gulp-util');
4+
var log = require('gulplog');
55
var stdout = require('mute-stdout');
66
var tildify = require('tildify');
77

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

1313
function execute(opts, env) {
14-
1514
var tasks = opts._;
1615
var toRun = tasks.length ? tasks : ['default'];
1716

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

2322
// This is what actually loads up the gulpfile
2423
require(env.configPath);
25-
gutil.log('Using gulpfile', chalk.magenta(tildify(env.configPath)));
24+
log.info('Using gulpfile', chalk.magenta(tildify(env.configPath)));
2625

2726
var gulpInst = require(env.modulePath);
2827
logEvents(gulpInst);

lib/versioned/^3.7.0/log/events.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
var chalk = require('chalk');
4-
var gutil = require('gulp-util');
4+
var log = require('gulplog');
55
var prettyTime = require('pretty-hrtime');
66

77
var exit = require('../../../shared/exit');
@@ -26,12 +26,12 @@ function logEvents(gulpInst) {
2626
gulpInst.on('task_start', function(e) {
2727
// TODO: batch these
2828
// so when 5 tasks start at once it only logs one time with all 5
29-
gutil.log('Starting', '\'' + chalk.cyan(e.task) + '\'...');
29+
log.info('Starting', '\'' + chalk.cyan(e.task) + '\'...');
3030
});
3131

3232
gulpInst.on('task_stop', function(e) {
3333
var time = prettyTime(e.hrDuration);
34-
gutil.log(
34+
log.info(
3535
'Finished', '\'' + chalk.cyan(e.task) + '\'',
3636
'after', chalk.magenta(time)
3737
);
@@ -40,19 +40,19 @@ function logEvents(gulpInst) {
4040
gulpInst.on('task_err', function(e) {
4141
var msg = formatError(e);
4242
var time = prettyTime(e.hrDuration);
43-
gutil.log(
43+
log.error(
4444
'\'' + chalk.cyan(e.task) + '\'',
4545
chalk.red('errored after'),
4646
chalk.magenta(time)
4747
);
48-
gutil.log(msg);
48+
log.error(msg);
4949
});
5050

5151
gulpInst.on('task_not_found', function(err) {
52-
gutil.log(
52+
log.error(
5353
chalk.red('Task \'' + err.task + '\' is not in your gulpfile')
5454
);
55-
gutil.log('Please check the documentation for proper gulpfile formatting');
55+
log.error('Please check the documentation for proper gulpfile formatting');
5656
exit(1);
5757
});
5858
}

lib/versioned/^4.0.0-alpha.1/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
var fs = require('fs');
44

5+
var log = require('gulplog');
56
var chalk = require('chalk');
6-
var gutil = require('gulp-util');
77
var stdout = require('mute-stdout');
88
var tildify = require('tildify');
99

@@ -54,15 +54,15 @@ function execute(opts, env) {
5454
}
5555
}
5656
try {
57-
gutil.log('Using gulpfile', chalk.magenta(tildify(env.configPath)));
57+
log.info('Using gulpfile', chalk.magenta(tildify(env.configPath)));
5858
gulpInst.parallel(toRun)(function(err) {
5959
if (err) {
6060
exit(1);
6161
}
6262
});
6363
} catch (err) {
64-
gutil.log(chalk.red(err.message));
65-
gutil.log(
64+
log.error(chalk.red(err.message));
65+
log.error(
6666
'Please check the documentation for proper gulpfile formatting'
6767
);
6868
exit(1);

0 commit comments

Comments
 (0)