grunt 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/env node
  2. 'use strict';
  3. process.title = 'grunt';
  4. var Liftoff = require('liftoff');
  5. var v8flags = require('v8flags');
  6. var extensions = require('interpret').jsVariants;
  7. var nopt = require('nopt');
  8. var gruntOptions = require('grunt-known-options');
  9. var completion = require('../lib/completion.js');
  10. var info = require('../lib/info.js');
  11. var pkg = require('../package.json');
  12. // Parse `gruntOptions` into a form that nopt can handle.
  13. var aliases = {};
  14. var known = {};
  15. Object.keys(gruntOptions).forEach(function(key) {
  16. var short = gruntOptions[key].short;
  17. if (short) {
  18. aliases[short] = '--' + key;
  19. }
  20. known[key] = gruntOptions[key].type;
  21. });
  22. // Parse them and return an options object.
  23. var options = nopt(known, aliases, process.argv, 2);
  24. if ('completion' in options) {
  25. completion.print(options.completion);
  26. } else if (options.version) {
  27. console.log('grunt-cli v' + pkg.version);
  28. }
  29. v8flags(function (err, v8flags) {
  30. var Grunt = new Liftoff({
  31. name: 'grunt',
  32. configName: 'Gruntfile',
  33. // Support a number of languages based on file extension
  34. extensions: extensions,
  35. // Flags that are v8 flags will be loaded into node instead of Gruntfile
  36. v8flags: v8flags
  37. });
  38. Grunt.launch({
  39. cwd: options.base,
  40. configPath: options.gruntfile,
  41. require: options.require,
  42. verbose: options.verbose
  43. }, function (env) {
  44. var tasks = options.argv.remain;
  45. delete options.argv;
  46. // No grunt install found!
  47. if (!env.modulePath) {
  48. if (options.version) {
  49. process.exit();
  50. }
  51. if (options.help) {
  52. info.help();
  53. }
  54. info.fatal('Unable to find local grunt.', 99);
  55. } else {
  56. options.gruntfile = env.configPath;
  57. var grunt = require(env.modulePath);
  58. grunt.tasks(tasks, options);
  59. }
  60. });
  61. });