help.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. var grunt = require('../grunt');
  11. // Nodejs libs.
  12. var path = require('path');
  13. // Set column widths.
  14. var col1len = 0;
  15. exports.initCol1 = function(str) {
  16. col1len = Math.max(col1len, str.length);
  17. };
  18. exports.initWidths = function() {
  19. // Widths for options/tasks table output.
  20. exports.widths = [1, col1len, 2, 76 - col1len];
  21. };
  22. // Render an array in table form.
  23. exports.table = function(arr) {
  24. arr.forEach(function(item) {
  25. grunt.log.writetableln(exports.widths, ['', grunt.util._.pad(item[0], col1len), '', item[1]]);
  26. });
  27. };
  28. // Methods to run, in-order.
  29. exports.queue = [
  30. 'initOptions',
  31. 'initTasks',
  32. 'initWidths',
  33. 'header',
  34. 'usage',
  35. 'options',
  36. 'optionsFooter',
  37. 'tasks',
  38. 'footer',
  39. ];
  40. // Actually display stuff.
  41. exports.display = function() {
  42. exports.queue.forEach(function(name) { exports[name](); });
  43. };
  44. // Header.
  45. exports.header = function() {
  46. grunt.log.writeln('Grunt: The JavaScript Task Runner (v' + grunt.version + ')');
  47. };
  48. // Usage info.
  49. exports.usage = function() {
  50. grunt.log.header('Usage');
  51. grunt.log.writeln(' ' + path.basename(process.argv[1]) + ' [options] [task [task ...]]');
  52. };
  53. // Options.
  54. exports.initOptions = function() {
  55. // Build 2-column array for table view.
  56. exports._options = Object.keys(grunt.cli.optlist).map(function(long) {
  57. var o = grunt.cli.optlist[long];
  58. var col1 = '--' + (o.negate ? 'no-' : '') + long + (o.short ? ', -' + o.short : '');
  59. exports.initCol1(col1);
  60. return [col1, o.info];
  61. });
  62. };
  63. exports.options = function() {
  64. grunt.log.header('Options');
  65. exports.table(exports._options);
  66. };
  67. exports.optionsFooter = function() {
  68. grunt.log.writeln().writelns(
  69. 'Options marked with * have methods exposed via the grunt API and should ' +
  70. 'instead be specified inside the Gruntfile wherever possible.'
  71. );
  72. };
  73. // Tasks.
  74. exports.initTasks = function() {
  75. // Initialize task system so that the tasks can be listed.
  76. grunt.task.init([], {help: true});
  77. // Build object of tasks by info (where they were loaded from).
  78. exports._tasks = [];
  79. Object.keys(grunt.task._tasks).forEach(function(name) {
  80. exports.initCol1(name);
  81. var task = grunt.task._tasks[name];
  82. exports._tasks.push(task);
  83. });
  84. };
  85. exports.tasks = function() {
  86. grunt.log.header('Available tasks');
  87. if (exports._tasks.length === 0) {
  88. grunt.log.writeln('(no tasks found)');
  89. } else {
  90. exports.table(exports._tasks.map(function(task) {
  91. var info = task.info;
  92. if (task.multi) { info += ' *'; }
  93. return [task.name, info];
  94. }));
  95. grunt.log.writeln().writelns(
  96. 'Tasks run in the order specified. Arguments may be passed to tasks that ' +
  97. 'accept them by using colons, like "lint:files". Tasks marked with * are ' +
  98. '"multi tasks" and will iterate over all sub-targets if no argument is ' +
  99. 'specified.'
  100. );
  101. }
  102. grunt.log.writeln().writelns(
  103. 'The list of available tasks may change based on tasks directories or ' +
  104. 'grunt plugins specified in the Gruntfile or via command-line options.'
  105. );
  106. };
  107. // Footer.
  108. exports.footer = function() {
  109. grunt.log.writeln().writeln('For more information, see http://gruntjs.com/');
  110. };