4 var path = require('path');
6 // This allows grunt to require() .coffee files.
7 require('coffee-script/register');
9 // The module to be exported.
10 var grunt = module.exports = {};
12 // Expose internal grunt libs.
13 function gRequire(name) {
14 return grunt[name] = require('./grunt/' + name);
17 var util = require('grunt-legacy-util');
19 grunt.util.task = require('./util/task');
21 var Log = require('grunt-legacy-log').Log;
22 var log = new Log({grunt: grunt});
27 var fail = gRequire('fail');
29 var option = gRequire('option');
30 var config = gRequire('config');
31 var task = gRequire('task');
32 var help = gRequire('help');
34 var verbose = grunt.verbose = log.verbose;
36 // Expose some grunt metadata.
37 grunt.package = require('../package.json');
38 grunt.version = grunt.package.version;
40 // Expose specific grunt lib methods on grunt.
41 function gExpose(obj, methodName, newMethodName) {
42 grunt[newMethodName || methodName] = obj[methodName].bind(obj);
44 gExpose(task, 'registerTask');
45 gExpose(task, 'registerMultiTask');
46 gExpose(task, 'registerInitTask');
47 gExpose(task, 'renameTask');
48 gExpose(task, 'loadTasks');
49 gExpose(task, 'loadNpmTasks');
50 gExpose(config, 'init', 'initConfig');
51 gExpose(fail, 'warn');
52 gExpose(fail, 'fatal');
54 // Expose the task interface. I've never called this manually, and have no idea
55 // how it will work. But it might.
56 grunt.tasks = function(tasks, options, done) {
57 // Update options with passed-in options.
60 // Display the grunt version and quit if the user did --version.
62 if (option('version')) {
64 log.writeln('grunt v' + grunt.version);
66 if (option('verbose')) {
68 verbose.writeln('Install path: ' + path.resolve(__dirname, '..'));
69 // Yes, this is a total hack, but we don't want to log all that verbose
70 // task initialization stuff here.
71 grunt.log.muted = true;
72 // Initialize task system so that available tasks can be listed.
73 grunt.task.init([], {help: true});
75 grunt.log.muted = false;
77 // Display available tasks (for shell completion, etc).
78 _tasks = Object.keys(grunt.task._tasks).sort();
79 verbose.writeln('Available tasks: ' + _tasks.join(' '));
81 // Display available options (for shell completion, etc).
83 Object.keys(grunt.cli.optlist).forEach(function(long) {
84 var o = grunt.cli.optlist[long];
85 _options.push('--' + (o.negate ? 'no-' : '') + long);
86 if (o.short) { _options.push('-' + o.short); }
88 verbose.writeln('Available options: ' + _options.join(' '));
97 // Display help and quit if the user did --help.
103 // A little header stuff.
104 verbose.header('Initializing').writeflags(option.flags(), 'Command-line options');
106 // Determine and output which tasks will be run.
107 var tasksSpecified = tasks && tasks.length > 0;
108 tasks = task.parseArgs([tasksSpecified ? tasks : 'default']);
111 task.init(tasks, options);
114 if (!tasksSpecified) {
115 verbose.writeln('No tasks specified, running default tasks.');
117 verbose.writeflags(tasks, 'Running tasks');
119 // Handle otherwise unhandleable (probably asynchronous) exceptions.
120 var uncaughtHandler = function(e) {
121 fail.fatal(e, fail.code.TASK_FAILURE);
123 process.on('uncaughtException', uncaughtHandler);
125 // Report, etc when all tasks have completed.
128 fail.warn(e, fail.code.TASK_FAILURE);
131 // Stop handling uncaught exceptions so that we don't leave any
132 // unwanted process-level side effects behind. There is no need to do
133 // this in the error callback, because fail.warn() will either kill
134 // the process, or with --force keep on going all the way here.
135 process.removeListener('uncaughtException', uncaughtHandler);
137 // Output a final fail / success report.
141 // Execute "done" function when done (only if passed, of course).
144 // Otherwise, explicitly exit.
150 // Execute all tasks, in order. Passing each task individually in a forEach
151 // allows the error callback to execute multiple times.
152 tasks.forEach(function(name) { task.run(name); });
153 // Run tasks async internally to reduce call-stack, per:
154 // https://github.com/gruntjs/grunt/pull/1026
155 task.start({asyncDone: true});