c09abfc3ffa3acb34b8cc4543e908c7c7a663e3c.svn-base
4.25 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* grunt
* http://gruntjs.com/
*
* Copyright (c) 2012 "Cowboy" Ben Alman
* Licensed under the MIT license.
* https://github.com/gruntjs/grunt/blob/master/LICENSE-MIT
*/
// Nodejs libs.
var path = require('path');
// The module to be exported.
var grunt = module.exports = {};
// Expose internal grunt libs.
function gRequire(name) {
return grunt[name] = require('./grunt/' + name);
}
var utils = gRequire('utils');
var template = gRequire('template');
var fail = gRequire('fail');
var file = gRequire('file');
var option = gRequire('option');
var config = gRequire('config');
var task = gRequire('task');
var log = gRequire('log');
var cli = gRequire('cli');
var verbose = grunt.verbose = log.verbose;
grunt.version = file.readJSON(path.join(__dirname, '../package.json')).version;
// Expose specific grunt lib methods on grunt.
function gExpose(obj, methodName, newMethodName) {
grunt[newMethodName || methodName] = obj[methodName].bind(obj);
}
gExpose(task, 'registerTask');
gExpose(task, 'registerMultiTask');
gExpose(task, 'registerInitTask');
gExpose(task, 'renameTask');
gExpose(task, 'registerHelper');
gExpose(task, 'renameHelper');
gExpose(task, 'helper');
gExpose(task, 'loadTasks');
gExpose(task, 'registerTask');
gExpose(task, 'loadNpmTasks');
gExpose(config, 'init', 'initConfig');
gExpose(fail, 'warn');
gExpose(fail, 'fatal');
// 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 = utils.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.
grunt.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.
require('./grunt/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();
} else {
// Otherwise, explicitly exit.
process.exit(0);
}
}
});
// 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();
};
// Register one or more Npm-installed grunt plugins inside that plugin's bin.
grunt._npmTasks = [];
grunt.npmTasks = function(tasks) {
grunt._npmTasks = Array.isArray(tasks) ? tasks : [tasks];
return grunt;
};