MDL-52591 grunt: make shifter task respect --no-color
[moodle.git] / Gruntfile.js
CommitLineData
adeb96d2
DW
1// This file is part of Moodle - http://moodle.org/
2//
3// Moodle is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7//
8// Moodle is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
15
16/**
17 * @copyright 2014 Andrew Nicols
18 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
19 */
20
21/**
22 * Grunt configuration
23 */
24
25module.exports = function(grunt) {
26 var path = require('path'),
8f76bfb6 27 fs = require('fs'),
e67585f8
DW
28 tasks = {},
29 cwd = process.env.PWD || process.cwd();
adeb96d2
DW
30
31 // Project configuration.
32 grunt.initConfig({
33 jshint: {
34 options: {jshintrc: '.jshintrc'},
35 files: ['**/amd/src/*.js']
36 },
37 uglify: {
38 dynamic_mappings: {
39 files: grunt.file.expandMapping(
40 ['**/src/*.js', '!**/node_modules/**'],
41 '',
42 {
e67585f8 43 cwd: cwd,
adeb96d2
DW
44 rename: function(destBase, destPath) {
45 destPath = destPath.replace('src', 'build');
46 destPath = destPath.replace('.js', '.min.js');
e67585f8 47 destPath = path.resolve(cwd, destPath);
adeb96d2
DW
48 return destPath;
49 }
50 }
51 )
52 }
a4a52e56
DP
53 },
54 less: {
55 bootstrapbase: {
56 files: {
57 "theme/bootstrapbase/style/moodle.css": "theme/bootstrapbase/less/moodle.less",
58 "theme/bootstrapbase/style/editor.css": "theme/bootstrapbase/less/editor.less",
59 },
60 options: {
61 compress: true
62 }
63 }
adeb96d2
DW
64 }
65 });
66
67 tasks.shifter = function() {
68 var exec = require('child_process').spawn,
69 done = this.async(),
70 args = [],
71 options = {
72 recursive: true,
73 watch: false,
74 walk: false,
75 module: false
76 },
77 shifter;
78
e67585f8
DW
79 args.push( path.normalize(__dirname + '/node_modules/shifter/bin/shifter'));
80
adeb96d2 81 // Determine the most appropriate options to run with based upon the current location.
e67585f8 82 if (path.basename(cwd) === 'src') {
adeb96d2
DW
83 // Detect whether we're in a src directory.
84 grunt.log.debug('In a src directory');
85 args.push('--walk');
86 options.walk = true;
e67585f8 87 } else if (path.basename(path.dirname(cwd)) === 'src') {
adeb96d2
DW
88 // Detect whether we're in a module directory.
89 grunt.log.debug('In a module directory');
90 options.module = true;
91 }
92
93 if (grunt.option('watch')) {
94 if (!options.walk && !options.module) {
95 grunt.fail.fatal('Unable to watch unless in a src or module directory');
96 }
97
98 // It is not advisable to run with recursivity and watch - this
99 // leads to building the build directory in a race-like fashion.
100 grunt.log.debug('Detected a watch - disabling recursivity');
101 options.recursive = false;
102 args.push('--watch');
103 }
104
105 if (options.recursive) {
106 args.push('--recursive');
107 }
108
109 // Always ignore the node_modules directory.
110 args.push('--excludes', 'node_modules');
111
112 // Add the stderr option if appropriate
113 if (grunt.option('verbose')) {
114 args.push('--lint-stderr');
115 }
116
a07afffc
DP
117 if (grunt.option('no-color')) {
118 args.push('--color=false');
119 }
120
8f76bfb6
DM
121 var execShifter = function() {
122
123 shifter = exec("node", args, {
124 cwd: cwd,
125 stdio: 'inherit',
126 env: process.env
127 });
128
129 // Tidy up after exec.
130 shifter.on('exit', function (code) {
131 if (code) {
132 grunt.fail.fatal('Shifter failed with code: ' + code);
133 } else {
134 grunt.log.ok('Shifter build complete.');
135 done();
136 }
137 });
138 };
139
adeb96d2 140 // Actually run shifter.
8f76bfb6
DM
141 if (!options.recursive) {
142 execShifter();
143 } else {
144 // Check that there are yui modules otherwise shifter ends with exit code 1.
145 var found = false;
146 var hasYuiModules = function(directory, callback) {
147 fs.readdir(directory, function(err, files) {
148 if (err) {
149 return callback(err, null);
150 }
151
152 // If we already found a match there is no need to continue scanning.
153 if (found === true) {
154 return;
155 }
156
157 // We need to track the number of files to know when we return a result.
158 var pending = files.length;
159
160 // We first check files, so if there is a match we don't need further
161 // async calls and we just return a true.
162 for (var i = 0; i < files.length; i++) {
163 if (files[i] === 'yui') {
164 return callback(null, true);
165 }
166 }
167
168 // Iterate through subdirs if there were no matches.
169 files.forEach(function (file) {
170
171 var p = path.join(directory, file);
172 stat = fs.statSync(p);
173 if (!stat.isDirectory()) {
174 pending--;
175 } else {
176
177 // We defer the pending-1 until we scan the whole dir and subdirs.
178 hasYuiModules(p, function(err, result) {
179 if (err) {
180 return callback(err);
181 }
182
183 if (result === true) {
184 // Once we get a true we notify the caller.
185 found = true;
186 return callback(null, true);
187 }
188
189 pending--;
190 if (pending === 0) {
191 // Notify the caller that the whole dir has been scaned and there are no matches.
192 return callback(null, false);
193 }
194 });
195 }
196
197 // No subdirs here, otherwise the return would be deferred until all subdirs are scanned.
198 if (pending === 0) {
199 return callback(null, false);
200 }
201 });
202 });
203 };
204
205 hasYuiModules(cwd, function(err, result) {
206 if (err) {
207 grunt.fail.fatal(err.message);
208 }
209
210 if (result === true) {
211 execShifter();
212 } else {
213 grunt.log.ok('No YUI modules to build.');
214 done();
215 }
216 });
217 }
adeb96d2
DW
218 };
219
220 tasks.startup = function() {
221 // Are we in a YUI directory?
e67585f8 222 if (path.basename(path.resolve(cwd, '../../')) == 'yui') {
adeb96d2
DW
223 grunt.task.run('shifter');
224 // Are we in an AMD directory?
e67585f8 225 } else if (path.basename(cwd) == 'amd') {
65d070ae 226 grunt.task.run('amd');
adeb96d2
DW
227 } else {
228 // Run them all!.
65d070ae
DP
229 grunt.task.run('css');
230 grunt.task.run('js');
adeb96d2
DW
231 }
232 };
233
234
235 // Register NPM tasks.
236 grunt.loadNpmTasks('grunt-contrib-uglify');
237 grunt.loadNpmTasks('grunt-contrib-jshint');
a4a52e56 238 grunt.loadNpmTasks('grunt-contrib-less');
adeb96d2 239
65d070ae 240 // Register JS tasks.
adeb96d2 241 grunt.registerTask('shifter', 'Run Shifter against the current directory', tasks.shifter);
65d070ae
DP
242 grunt.registerTask('amd', ['jshint', 'uglify']);
243 grunt.registerTask('js', ['amd', 'shifter']);
244
245 // Register CSS taks.
246 grunt.registerTask('css', ['less:bootstrapbase']);
adeb96d2
DW
247
248 // Register the startup task.
249 grunt.registerTask('startup', 'Run the correct tasks for the current directory', tasks.startup);
250
251 // Register the default task.
252 grunt.registerTask('default', ['startup']);
253};