source: Dev/trunk/node_modules/grunt/lib/grunt.js @ 516

Last change on this file since 516 was 516, checked in by hendrikvanantwerpen, 11 years ago

Enable deployment with Grunt.

File size: 4.8 KB
Line 
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.
13var path = require('path');
14
15// This allows grunt to require() .coffee files.
16require('coffee-script');
17
18// The module to be exported.
19var grunt = module.exports = {};
20
21// Expose internal grunt libs.
22function gRequire(name) {
23  return grunt[name] = require('./grunt/' + name);
24}
25
26var util = require('grunt-legacy-util');
27grunt.util = util;
28grunt.util.task = require('./util/task');
29
30gRequire('template');
31gRequire('event');
32var fail = gRequire('fail');
33gRequire('file');
34var option = gRequire('option');
35var config = gRequire('config');
36var task = gRequire('task');
37var log = gRequire('log');
38var help = gRequire('help');
39gRequire('cli');
40var verbose = grunt.verbose = log.verbose;
41
42// Expose some grunt metadata.
43grunt.package = require('../package.json');
44grunt.version = grunt.package.version;
45
46// Expose specific grunt lib methods on grunt.
47function gExpose(obj, methodName, newMethodName) {
48  grunt[newMethodName || methodName] = obj[methodName].bind(obj);
49}
50gExpose(task, 'registerTask');
51gExpose(task, 'registerMultiTask');
52gExpose(task, 'registerInitTask');
53gExpose(task, 'renameTask');
54gExpose(task, 'loadTasks');
55gExpose(task, 'loadNpmTasks');
56gExpose(config, 'init', 'initConfig');
57gExpose(fail, 'warn');
58gExpose(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.
62grunt.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};
Note: See TracBrowser for help on using the repository browser.