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