UNPKG

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