MDL-49046 javascript: Add support for AMD modules and jquery.
[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'),
27 tasks = {};
28
29 // Project configuration.
30 grunt.initConfig({
31 jshint: {
32 options: {jshintrc: '.jshintrc'},
33 files: ['**/amd/src/*.js']
34 },
35 uglify: {
36 dynamic_mappings: {
37 files: grunt.file.expandMapping(
38 ['**/src/*.js', '!**/node_modules/**'],
39 '',
40 {
41 cwd: process.env.PWD,
42 rename: function(destBase, destPath) {
43 destPath = destPath.replace('src', 'build');
44 destPath = destPath.replace('.js', '.min.js');
45 destPath = path.resolve(process.env.PWD, destPath);
46 return destPath;
47 }
48 }
49 )
50 }
51 }
52 });
53
54 tasks.shifter = function() {
55 var exec = require('child_process').spawn,
56 done = this.async(),
57 args = [],
58 options = {
59 recursive: true,
60 watch: false,
61 walk: false,
62 module: false
63 },
64 shifter;
65
66 // Determine the most appropriate options to run with based upon the current location.
67 if (path.basename(process.env.PWD) === 'src') {
68 // Detect whether we're in a src directory.
69 grunt.log.debug('In a src directory');
70 args.push('--walk');
71 options.walk = true;
72 } else if (path.basename(path.dirname(process.env.PWD)) === 'src') {
73 // Detect whether we're in a module directory.
74 grunt.log.debug('In a module directory');
75 options.module = true;
76 }
77
78 if (grunt.option('watch')) {
79 if (!options.walk && !options.module) {
80 grunt.fail.fatal('Unable to watch unless in a src or module directory');
81 }
82
83 // It is not advisable to run with recursivity and watch - this
84 // leads to building the build directory in a race-like fashion.
85 grunt.log.debug('Detected a watch - disabling recursivity');
86 options.recursive = false;
87 args.push('--watch');
88 }
89
90 if (options.recursive) {
91 args.push('--recursive');
92 }
93
94 // Always ignore the node_modules directory.
95 args.push('--excludes', 'node_modules');
96
97 // Add the stderr option if appropriate
98 if (grunt.option('verbose')) {
99 args.push('--lint-stderr');
100 }
101
102 // Actually run shifter.
103 shifter = exec(process.cwd() + '/node_modules/shifter/bin/shifter', args, {
104 cwd: process.env.PWD,
105 stdio: 'inherit',
106 env: process.env
107 });
108
109 // Tidy up after exec.
110 shifter.on('exit', function (code) {
111 if (code) {
112 grunt.fail.fatal('Shifter failed with code: ' + code);
113 } else {
114 grunt.log.ok('Shifter build complete.');
115 done();
116 }
117 });
118 };
119
120 tasks.startup = function() {
121 // Are we in a YUI directory?
122 if (path.basename(path.resolve(process.env.PWD, '../../')) == 'yui') {
123 grunt.task.run('shifter');
124 // Are we in an AMD directory?
125 } else if (path.basename(process.env.PWD) == 'amd') {
126 grunt.task.run('jshint');
127 grunt.task.run('uglify');
128 } else {
129 // Run them all!.
130 grunt.task.run('shifter');
131 grunt.task.run('jshint');
132 grunt.task.run('uglify');
133 }
134 };
135
136
137 // Register NPM tasks.
138 grunt.loadNpmTasks('grunt-contrib-uglify');
139 grunt.loadNpmTasks('grunt-contrib-jshint');
140
141 // Register the shifter task.
142 grunt.registerTask('shifter', 'Run Shifter against the current directory', tasks.shifter);
143
144 // Register the startup task.
145 grunt.registerTask('startup', 'Run the correct tasks for the current directory', tasks.startup);
146
147 // Register the default task.
148 grunt.registerTask('default', ['startup']);
149};