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