Skip to content

Commit

Permalink
Fix version and help with no gruntfile
Browse files Browse the repository at this point in the history
  • Loading branch information
shama committed Mar 6, 2018
1 parent ea01dc5 commit 0c5f397
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
18 changes: 15 additions & 3 deletions bin/grunt
Expand Up @@ -10,6 +10,7 @@ var extensions = require('interpret').jsVariants;
var nopt = require('nopt');
var gruntOptions = require('grunt-known-options');
var completion = require('../lib/completion.js');
var info = require('../lib/info.js');
var pkg = require('../package.json');

// Parse `gruntOptions` into a form that nopt can handle.
Expand Down Expand Up @@ -49,8 +50,19 @@ v8flags(function (err, v8flags) {
}, function (env) {
var tasks = options.argv.remain;
delete options.argv;
options.gruntfile = env.configPath;
var grunt = require(env.modulePath);
grunt.tasks(tasks, options);
// No grunt install found!
if (!env.modulePath) {
if (options.version) {
process.exit();
}
if (options.help) {
info.help();
}
info.fatal('Unable to find local grunt.', 99);
} else {
options.gruntfile = env.configPath;
var grunt = require(env.modulePath);
grunt.tasks(tasks, options);
}
});
});
51 changes: 51 additions & 0 deletions lib/info.js
@@ -0,0 +1,51 @@
/*
* grunt-cli
* http://gruntjs.com/
*
* Copyright (c) 2016 Tyler Kellen, contributors
* Licensed under the MIT license.
* https://github.com/gruntjs/grunt-init/blob/master/LICENSE-MIT
*/

'use strict';

// Project metadata.
var pkg = require('../package.json');

// Display grunt-cli version.
exports.version = function() {
console.log('grunt-cli v' + pkg.version);
};

// Show help, then exit with a message and error code.
exports.fatal = function(msg, code) {
exports.helpHeader();
console.log('Fatal error: ' + msg);
console.log('');
exports.helpFooter();
process.exit(code);
};

// Show help and exit.
exports.help = function() {
exports.helpHeader();
exports.helpFooter();
process.exit();
};

// Help header.
exports.helpHeader = function() {
console.log('grunt-cli: ' + pkg.description + ' (v' + pkg.version + ')');
console.log('');
};

// Help footer.
exports.helpFooter = function() {
[
'If you\'re seeing this message, grunt hasn\'t been installed locally to',
'your project. For more information about installing and configuring grunt,',
'please see the Getting Started guide:',
'',
'https://gruntjs.com/getting-started',
].forEach(function(str) { console.log(str); });
};

0 comments on commit 0c5f397

Please sign in to comment.