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