You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
4.6 KiB

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