UNPKG

4.09 kBJavaScriptView Raw
1/*
2 * grunt
3 * https://github.com/cowboy/grunt
4 *
5 * Copyright (c) 2012 "Cowboy" Ben Alman
6 * Licensed under the MIT license.
7 * http://benalman.com/about/license/
8 */
9
10var path = require('path');
11
12// Perhaps, someday, this will be the only global.
13global.grunt = {};
14
15// Used to require grunt libs. Sweet, sugary goodness. Mmmmn.
16grunt.require = function(libname) { return require(path.join(__dirname, 'grunt', libname)); };
17
18// External libs, exposed globally for convenience.
19global.async = require('async');
20global.underscore = require('underscore');
21
22// Internal grunt libs, exposed globally for convenience.
23global.util = grunt.require('util');
24global.task = grunt.require('task');
25global.file = grunt.require('file');
26global.fail = grunt.require('fail');
27global.config = grunt.require('config');
28global.option = grunt.require('option');
29global.template = grunt.require('template');
30global.log = grunt.require('log');
31global.verbose = log.verbose;
32
33grunt.version = file.readJson(path.join(__dirname, '../package.json')).version;
34exports.version = grunt.version;
35
36// Handle exceptions.
37process.on('uncaughtException', function (e) {
38 fail.warn(e, 3);
39});
40
41// Disable colors if --no-colors was passed.
42function initColors() {
43 var methods = Object.keys(String.prototype);
44 // Requiring this here will modify String prototype everywhere.
45 require('colors');
46
47 // Disable colors.
48 if (option('no-color')) {
49 // Override "colors".
50 Object.keys(String.prototype).filter(function(method) {
51 // Filter out methods that existed before "colors" was required.
52 return methods.indexOf(method) === -1;
53 }).forEach(function(method) {
54 // Replace each new method with a function that just returns `this`.
55 String.prototype.__defineGetter__(method, function() { return this; });
56 });
57
58 // Override console.log (nodeunit, maybe others).
59 console._log = console.log;
60 console.log = function() {
61 var args = util.toArray(arguments).map(function(value) {
62 if (typeof value === 'string') {
63 return value.replace(/\033\[[\d;]+m/g, '');
64 }
65 return value;
66 });
67 console._log.apply(console, args);
68 };
69 }
70}
71
72// Expose the task interface. I've never called this manually, and have no idea
73// how it will work. But it might.
74exports.tasks = function(tasks, options, done) {
75 // Update options with passed-in options.
76 option.init(options);
77
78 // Init colors.
79 initColors();
80
81 if (option('help')) {
82 // Load and display help if the user did --help.
83 grunt.require('help');
84 } else if (option('version')) {
85 // Display the current grunt version if the user did --version.
86 log.writeln('grunt v' + grunt.version);
87 return;
88 }
89
90 // A little header stuff.
91 verbose.header('Initializing').writeflags(option.flags(), 'Command-line options');
92
93 // Determine and output which tasks will be run.
94 var tasksSpecified = tasks && tasks.length > 0;
95 tasks = task.parseArgs([tasksSpecified ? tasks : 'default']);
96
97 // Initialize tasks.
98 task.init(tasks);
99
100 verbose.writeln();
101 if (!tasksSpecified) {
102 verbose.writeln('No tasks specified, running default tasks.');
103 }
104 verbose.writeflags(tasks, 'Running tasks');
105
106 // Report, etc when all tasks have completed.
107 task.options({
108 error: function(e) {
109 fail.warn(e, 3);
110 },
111 done: function() {
112 // Output a final fail / success report.
113 fail.report();
114 // Execute "done" function when done (only if passed, of course).
115 if (done) { done(); }
116 }
117 });
118
119 // Execute all tasks, in order. Passing each task individually in a forEach
120 // allows the error callback to execute multiple times.
121 tasks.forEach(function(name) { task.run(name); });
122 task.start();
123};
124
125// This is only executed when run via command line.
126exports.cli = function(options) {
127 // Parse task list and options from the command line.
128 var cli = grunt.require('cli');
129
130 // CLI-parsed options override any passed-in "default" options.
131 underscore.defaults(cli.options, options);
132
133 // Run tasks.
134 exports.tasks(cli.tasks, cli.options);
135};