1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | var path = require('path');
|
11 |
|
12 |
|
13 | global.grunt = {};
|
14 |
|
15 |
|
16 | grunt.require = function(libname) { return require(path.join(__dirname, 'grunt', libname)); };
|
17 |
|
18 |
|
19 | global.async = require('async');
|
20 | global.underscore = require('underscore');
|
21 |
|
22 |
|
23 | global.util = grunt.require('util');
|
24 | global.task = grunt.require('task');
|
25 | global.file = grunt.require('file');
|
26 | global.fail = grunt.require('fail');
|
27 | global.config = grunt.require('config');
|
28 | global.option = grunt.require('option');
|
29 | global.template = grunt.require('template');
|
30 | global.log = grunt.require('log');
|
31 | global.verbose = log.verbose;
|
32 |
|
33 | grunt.version = file.readJson(path.join(__dirname, '../package.json')).version;
|
34 | exports.version = grunt.version;
|
35 |
|
36 |
|
37 | process.on('uncaughtException', function (e) {
|
38 | fail.warn(e, 3);
|
39 | });
|
40 |
|
41 |
|
42 | function initColors() {
|
43 | var methods = Object.keys(String.prototype);
|
44 |
|
45 | require('colors');
|
46 |
|
47 |
|
48 | if (option('no-color')) {
|
49 |
|
50 | Object.keys(String.prototype).filter(function(method) {
|
51 |
|
52 | return methods.indexOf(method) === -1;
|
53 | }).forEach(function(method) {
|
54 |
|
55 | String.prototype.__defineGetter__(method, function() { return this; });
|
56 | });
|
57 |
|
58 |
|
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 |
|
73 |
|
74 | exports.tasks = function(tasks, options, done) {
|
75 |
|
76 | option.init(options);
|
77 |
|
78 |
|
79 | initColors();
|
80 |
|
81 | if (option('help')) {
|
82 |
|
83 | grunt.require('help');
|
84 | } else if (option('version')) {
|
85 |
|
86 | log.writeln('grunt v' + grunt.version);
|
87 | return;
|
88 | }
|
89 |
|
90 |
|
91 | verbose.header('Initializing').writeflags(option.flags(), 'Command-line options');
|
92 |
|
93 |
|
94 | var tasksSpecified = tasks && tasks.length > 0;
|
95 | tasks = task.parseArgs([tasksSpecified ? tasks : 'default']);
|
96 |
|
97 |
|
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 |
|
107 | task.options({
|
108 | error: function(e) {
|
109 | fail.warn(e, 3);
|
110 | },
|
111 | done: function() {
|
112 |
|
113 | fail.report();
|
114 |
|
115 | if (done) { done(); }
|
116 | }
|
117 | });
|
118 |
|
119 |
|
120 |
|
121 | tasks.forEach(function(name) { task.run(name); });
|
122 | task.start();
|
123 | };
|
124 |
|
125 |
|
126 | exports.cli = function(options) {
|
127 |
|
128 | var cli = grunt.require('cli');
|
129 |
|
130 |
|
131 | underscore.defaults(cli.options, options);
|
132 |
|
133 |
|
134 | exports.tasks(cli.tasks, cli.options);
|
135 | };
|