Skip to content

Commit

Permalink
CLI cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Contra committed Feb 7, 2014
1 parent b5404f8 commit eaf8229
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 39 deletions.
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ In particular, this community seeks the following types of contributions:

- ideas: participate in an Issues thread or start your own to have your voice
heard.
- resources: submit a PR to add to [docs README.md](docs/README.md) with links to related content
- resources: submit a PR to add to [docs README.md](/docs/README.md) with links to related content
- outline sections: help us ensure that this repository is comprehensive. if
there is a topic that is overlooked, please add it, even if it is just a stub
in the form of a header and single sentence. Initially, most things fall into
Expand Down Expand Up @@ -77,4 +77,4 @@ on what we're actually doing, not wandering off into too much imaginary stuff.

# Frequently Asked Questions

See [the FAQ docs page](docs/FAQ.md)
See [the FAQ docs page](/docs/FAQ.md)
72 changes: 37 additions & 35 deletions bin/gulp.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,97 +18,99 @@ cli.on('require', function (name) {
});

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

cli.launch(function () {
handleArguments(this);
});

var argv = this.argv;
function handleArguments(args) {
var argv = args.argv;
var cliPackage = require('../package');
// TODO: drop argv.v in the next breaking release
var versionFlag = argv.v || argv.V || argv.version;
var tasksFlag = argv.T || argv.tasks;
var tasks = argv._;
var toRun = tasks.length ? tasks : ['default'];

if (versionFlag) {
gutil.log('CLI version', cliPackage.version);
if (this.localPackage) {
gutil.log('Local version', this.modulePackage.version);
if (args.localPackage) {
gutil.log('Local version', args.modulePackage.version);
}
process.exit(0);
}
if (!this.modulePath) {
gutil.log(gutil.colors.red('No local gulp install found in'), gutil.colors.magenta(this.cwd));

if (!args.modulePath) {
gutil.log(gutil.colors.red('No local gulp install found in'), gutil.colors.magenta(args.cwd));
gutil.log(gutil.colors.red('Try running: npm install gulp'));
process.exit(1);
}

if (!this.configPath) {
if (!args.configPath) {
gutil.log(gutil.colors.red('No gulpfile found'));
process.exit(1);
}

// check for semver difference between cli and local installation
if (semver.gt(cliPackage.version, this.modulePackage.version)) {
gutil.log(gutil.colors.red('gulp version mismatch:'));
if (semver.gt(cliPackage.version, args.modulePackage.version)) {
gutil.log(gutil.colors.red('Warning: gulp version mismatch:'));
gutil.log(gutil.colors.red('Running gulp is', cliPackage.version));
gutil.log(gutil.colors.red('Local gulp (installed in gulpfile dir) is', this.modulePackage.version));
gutil.log(gutil.colors.red('Local gulp (installed in gulpfile dir) is', args.modulePackage.version));
}

var Gulpfile = require(this.configPath);
gutil.log('Using file', gutil.colors.magenta(this.configPath));
var gulpFile = require(args.configPath);
gutil.log('Using gulpfile', gutil.colors.magenta(args.configPath));

var Gulp = require(this.modulePath);
logEvents(Gulp);
process.chdir(this.cwd);
gutil.log('Working directory changed to', gutil.colors.magenta(this.cwd));
var gulpInst = require(args.modulePath);
logEvents(gulpInst);
process.chdir(args.cwd);
gutil.log('Working directory changed to', gutil.colors.magenta(args.cwd));

process.nextTick(function(){
process.nextTick(function () {
if (tasksFlag) {
return logTasks(Gulpfile, Gulp);
return logTasks(gulpFile, gulpInst);
}
gulpInst.start.apply(gulpInst, toRun);
});

Gulp.start.apply(Gulp, toRun);
});
}

function logTasks(gulpFile, localGulp) {
var tree = taskTree(localGulp.tasks);
tree.label = 'Tasks for '+gutil.colors.magenta(gulpFile);
archy(tree).split('\n').forEach(function(v){
tree.label = 'Tasks for ' + gutil.colors.magenta(gulpFile);
archy(tree).split('\n').forEach(function (v) {
if (v.trim().length === 0) return;
gutil.log(v);
});
}

// format orchestrator errors
function formatError (e) {
function formatError(e) {
if (!e.err) return e.message;
if (e.err.message) return e.err.message;
return JSON.stringify(e.err);
}

// wire up logging events
function logEvents(gulp) {
gulp.on('task_start', function(e){
gutil.log('Running', "'"+gutil.colors.cyan(e.task)+"'...");
function logEvents(gulpInst) {
gulpInst.on('task_start', function (e) {
gutil.log('Running', "'" + gutil.colors.cyan(e.task) + "'...");
});

gulp.on('task_stop', function(e){
gulpInst.on('task_stop', function (e) {
var time = prettyTime(e.hrDuration);
gutil.log('Finished', "'"+gutil.colors.cyan(e.task)+"'", 'in', gutil.colors.magenta(time));
gutil.log('Finished', "'" + gutil.colors.cyan(e.task) + "'", 'in', gutil.colors.magenta(time));
});

gulp.on('task_err', function(e){
gulpInst.on('task_err', function (e) {
var msg = formatError(e);
var time = prettyTime(e.hrDuration);
gutil.log('Errored', "'"+gutil.colors.cyan(e.task)+"'", 'in', gutil.colors.magenta(time), gutil.colors.red(msg));
gutil.log('Errored', "'" + gutil.colors.cyan(e.task) + "'", 'in', gutil.colors.magenta(time), gutil.colors.red(msg));
});

gulp.on('task_not_found', function(err){
gutil.log(gutil.colors.red("Task '"+err.task+"' was not defined in your gulpfile but you tried to run it."));
gulpInst.on('task_not_found', function (err) {
gutil.log(gutil.colors.red("Task '" + err.task + "' was not defined in your gulpfile but you tried to run it."));
gutil.log('Please check the documentation for proper gulpfile formatting.');
process.exit(1);
});
}
}
2 changes: 1 addition & 1 deletion docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

gulp has very few flags to know about. All other flags are for tasks to use if needed.

- `-v` or `--version` will display the global and local gulp versions
- `-v` `-V` or `--version` will display the global and local gulp versions
- `--require <module path>` will require a module before running the gulpfile. This is useful for transpilers but also has other applications. You can use multiple `--require` flags
- `--gulpfile <gulpfile path>` manually set path of gulpfile. Useful if you have multiple gulpfiles. This will set the CWD to the gulpfile directory as well.
- `--cwd <dir path>` manually set the CWD. The search for the gulpfile, as well as the relativity of all requires will be from here.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "gulp",
"description": "The streaming build system",
"version": "3.5.3",
"version": "3.5.4",
"homepage": "http://github.com/wearefractal/gulp",
"repository": "git://github.com/wearefractal/gulp.git",
"author": "Fractal <[email protected]> (http://wearefractal.com/)",
Expand Down

0 comments on commit eaf8229

Please sign in to comment.