grunt.js 5.4 KB

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