grunt.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. 'use strict';
  2. // Nodejs libs.
  3. var path = require('path');
  4. // This allows grunt to require() .coffee files.
  5. require('coffeescript/register');
  6. // The module to be exported.
  7. var grunt = module.exports = {};
  8. // Expose internal grunt libs.
  9. function gRequire(name) {
  10. return grunt[name] = require('./grunt/' + name);
  11. }
  12. var util = require('grunt-legacy-util');
  13. grunt.util = util;
  14. grunt.util.task = require('./util/task');
  15. var Log = require('grunt-legacy-log').Log;
  16. var log = new Log({grunt: grunt});
  17. grunt.log = log;
  18. gRequire('template');
  19. gRequire('event');
  20. var fail = gRequire('fail');
  21. gRequire('file');
  22. var option = gRequire('option');
  23. var config = gRequire('config');
  24. var task = gRequire('task');
  25. var help = gRequire('help');
  26. gRequire('cli');
  27. var verbose = grunt.verbose = log.verbose;
  28. // Expose some grunt metadata.
  29. grunt.package = require('../package.json');
  30. grunt.version = grunt.package.version;
  31. // Expose specific grunt lib methods on grunt.
  32. function gExpose(obj, methodName, newMethodName) {
  33. grunt[newMethodName || methodName] = obj[methodName].bind(obj);
  34. }
  35. gExpose(task, 'registerTask');
  36. gExpose(task, 'registerMultiTask');
  37. gExpose(task, 'registerInitTask');
  38. gExpose(task, 'renameTask');
  39. gExpose(task, 'loadTasks');
  40. gExpose(task, 'loadNpmTasks');
  41. gExpose(config, 'init', 'initConfig');
  42. gExpose(fail, 'warn');
  43. gExpose(fail, 'fatal');
  44. // Expose the task interface. I've never called this manually, and have no idea
  45. // how it will work. But it might.
  46. grunt.tasks = function(tasks, options, done) {
  47. // Update options with passed-in options.
  48. option.init(options);
  49. // Display the grunt version and quit if the user did --version.
  50. var _tasks, _options;
  51. if (option('version')) {
  52. // Not --verbose.
  53. log.writeln('grunt v' + grunt.version);
  54. if (option('verbose')) {
  55. // --verbose
  56. verbose.writeln('Install path: ' + path.resolve(__dirname, '..'));
  57. // Yes, this is a total hack, but we don't want to log all that verbose
  58. // task initialization stuff here.
  59. grunt.log.muted = true;
  60. // Initialize task system so that available tasks can be listed.
  61. grunt.task.init([], {help: true});
  62. // Re-enable logging.
  63. grunt.log.muted = false;
  64. // Display available tasks (for shell completion, etc).
  65. _tasks = Object.keys(grunt.task._tasks).sort();
  66. verbose.writeln('Available tasks: ' + _tasks.join(' '));
  67. // Display available options (for shell completion, etc).
  68. _options = [];
  69. Object.keys(grunt.cli.optlist).forEach(function(long) {
  70. var o = grunt.cli.optlist[long];
  71. _options.push('--' + (o.negate ? 'no-' : '') + long);
  72. if (o.short) { _options.push('-' + o.short); }
  73. });
  74. verbose.writeln('Available options: ' + _options.join(' '));
  75. }
  76. return;
  77. }
  78. // Init colors.
  79. log.initColors();
  80. // Display help and quit if the user did --help.
  81. if (option('help')) {
  82. help.display();
  83. return;
  84. }
  85. // A little header stuff.
  86. verbose.header('Initializing').writeflags(option.flags(), 'Command-line options');
  87. // Determine and output which tasks will be run.
  88. var tasksSpecified = tasks && tasks.length > 0;
  89. tasks = task.parseArgs([tasksSpecified ? tasks : 'default']);
  90. // Initialize tasks.
  91. task.init(tasks, options);
  92. verbose.writeln();
  93. if (!tasksSpecified) {
  94. verbose.writeln('No tasks specified, running default tasks.');
  95. }
  96. verbose.writeflags(tasks, 'Running tasks');
  97. // Handle otherwise unhandleable (probably asynchronous) exceptions.
  98. var uncaughtHandler = function(e) {
  99. fail.fatal(e, fail.code.TASK_FAILURE);
  100. };
  101. process.on('uncaughtException', uncaughtHandler);
  102. // Report, etc when all tasks have completed.
  103. task.options({
  104. error: function(e) {
  105. fail.warn(e, fail.code.TASK_FAILURE);
  106. },
  107. done: function() {
  108. // Stop handling uncaught exceptions so that we don't leave any
  109. // unwanted process-level side effects behind. There is no need to do
  110. // this in the error callback, because fail.warn() will either kill
  111. // the process, or with --force keep on going all the way here.
  112. process.removeListener('uncaughtException', uncaughtHandler);
  113. // Output a final fail / success report.
  114. fail.report();
  115. if (done) {
  116. // Execute "done" function when done (only if passed, of course).
  117. done();
  118. } else {
  119. // Otherwise, explicitly exit.
  120. util.exit(0);
  121. }
  122. }
  123. });
  124. // Execute all tasks, in order. Passing each task individually in a forEach
  125. // allows the error callback to execute multiple times.
  126. tasks.forEach(function(name) { task.run(name); });
  127. // Run tasks async internally to reduce call-stack, per:
  128. // https://github.com/gruntjs/grunt/pull/1026
  129. task.start({asyncDone: true});
  130. };