grunt.js 4.8 KB

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