/
grunt.js
138 lines (115 loc) · 4.01 KB
/
grunt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* grunt
* https://github.com/cowboy/grunt
*
* Copyright (c) 2012 "Cowboy" Ben Alman
* Licensed under the MIT license.
* http://benalman.com/about/license/
*/
var path = require('path');
// Perhaps, someday, this will be the only global.
global.grunt = {};
// Used to require grunt libs. Sweet, sugary goodness. Mmmmn.
grunt.require = function(libname) { return require(path.join(__dirname, 'grunt', libname)); };
// External libs, exposed globally for convenience.
global.async = require('async');
global.underscore = require('underscore');
// Internal grunt libs, exposed globally for convenience.
global.util = grunt.require('util');
global.task = grunt.require('task');
global.file = grunt.require('file');
global.fail = grunt.require('fail');
global.config = grunt.require('config');
global.option = grunt.require('option');
global.template = grunt.require('template');
global.log = grunt.require('log');
global.verbose = log.verbose;
grunt.version = file.readJson(path.join(__dirname, '../package.json')).version;
exports.version = grunt.version;
// Handle exceptions.
process.on('uncaughtException', function (e) {
fail.warn(e, 3);
});
// Disable colors if --no-colors was passed.
function initColors() {
var methods = Object.keys(String.prototype);
// Requiring this here will modify String prototype everywhere.
require('colors');
// Disable colors.
if (option('no-color')) {
// Override "colors".
Object.keys(String.prototype).filter(function(method) {
// Filter out methods that existed before "colors" was required.
return methods.indexOf(method) === -1;
}).forEach(function(method) {
// Replace each new method with a function that just returns `this`.
String.prototype.__defineGetter__(method, function() { return this; });
});
// Override console.log (nodeunit, maybe others).
console._log = console.log;
console.log = function() {
var args = util.toArray(arguments).map(function(value) {
if (typeof value === 'string') {
return value.replace(/\033\[[\d;]+m/g, '');
}
return value;
});
console._log.apply(console, args);
};
}
}
// Expose the task interface. I've never called this manually, and have no idea
// how it will work. But it might.
exports.tasks = function(tasks, options, done) {
// Update options with passed-in options.
option.init(options);
// Init colors.
initColors();
if (option('help')) {
// Load and display help if the user did --help.
grunt.require('help');
} else if (option('version')) {
// Display the current grunt version if the user did --version.
log.writeln('grunt v' + grunt.version);
return;
}
// A little header stuff.
verbose.header('Initializing').writeflags(option.flags(), 'Command-line options');
// Determine and output which tasks will be run.
var tasksSpecified = tasks && tasks.length > 0;
tasks = task.parseArgs([tasksSpecified ? tasks : 'default']);
// Initialize tasks.
task.init(tasks);
verbose.writeln();
if (!tasksSpecified) {
verbose.writeln('No tasks specified, running default tasks.');
}
verbose.writeflags(tasks, 'Running tasks');
// Report, etc when all tasks have completed.
task.options({
error: function(e) {
fail.warn(e, 3);
},
done: function() {
// Output a final fail / success report.
fail.report();
if (done) {
// Execute "done" function when done (only if passed, of course).
done();
}
}
});
// Execute all tasks, in order. Passing each task individually in a forEach
// allows the error callback to execute multiple times.
tasks.forEach(function(name) { task.run(name); });
task.start();
};
// This is only executed when run via command line.
exports.cli = function(options) {
// Parse task list and options from the command line.
var cli = grunt.require('cli');
// CLI-parsed options override any passed-in "default" options.
underscore.defaults(cli.options, options);
// Run tasks.
exports.tasks(cli.tasks, cli.options);
};