1 // This file is part of Moodle - http://moodle.org/
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.
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.
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 /* jshint node: true, browser: false */
19 * @copyright 2021 Andrew Nicols
20 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 module.exports = grunt => {
27 * Shifter task. Is configured with a path to a specific file or a directory,
28 * in the case of a specific file it will work out the right module to be built.
30 * Note that this task runs the invidiaul shifter jobs async (becase it spawns
31 * so be careful to to call done().
33 const handler = function() {
34 const done = this.async();
35 const options = grunt.config('shifter.options');
36 const async = require('async');
37 const path = require('path');
39 // Run the shifter processes one at a time to avoid confusing output.
40 async.eachSeries(options.paths, function(src, filedone) {
42 args.push(path.normalize(process.cwd() + '/node_modules/shifter/bin/shifter'));
44 // Always ignore the node_modules directory.
45 args.push('--excludes', 'node_modules');
47 // Determine the most appropriate options to run with based upon the current location.
48 if (grunt.file.isMatch('**/yui/**/*.js', src)) {
49 // When passed a JS file, build our containing module (this happen with
51 grunt.log.debug('Shifter passed a specific JS file');
52 src = path.dirname(path.dirname(src));
53 options.recursive = false;
54 } else if (grunt.file.isMatch('**/yui/src', src)) {
55 // When in a src directory --walk all modules.
56 grunt.log.debug('In a src directory');
58 options.recursive = false;
59 } else if (grunt.file.isMatch('**/yui/src/*', src)) {
60 // When in module, only build our module.
61 grunt.log.debug('In a module directory');
62 options.recursive = false;
63 } else if (grunt.file.isMatch('**/yui/src/*/js', src)) {
64 // When in module src, only build our module.
65 grunt.log.debug('In a source directory');
66 src = path.dirname(src);
67 options.recursive = false;
70 if (grunt.option('watch')) {
71 grunt.fail.fatal('The --watch option has been removed, please use `grunt watch` instead');
74 // Add the stderr option if appropriate
75 if (grunt.option('verbose')) {
76 args.push('--lint-stderr');
79 if (grunt.option('no-color')) {
80 args.push('--color=false');
83 var execShifter = function() {
85 grunt.log.ok("Running shifter on " + src);
89 opts: {cwd: src, stdio: 'inherit', env: process.env}
90 }, function(error, result, code) {
92 grunt.fail.fatal('Shifter failed with code: ' + code);
94 grunt.log.ok('Shifter build complete.');
100 // Actually run shifter.
101 if (!options.recursive) {
104 // Check that there are yui modules otherwise shifter ends with exit code 1.
105 if (grunt.file.expand({cwd: src}, '**/yui/src/**/*.js').length > 0) {
106 args.push('--recursive');
109 grunt.log.ok('No YUI modules to build.');
116 // Register the shifter task.
117 grunt.registerTask('shifter', 'Run Shifter against the current directory', handler);
120 grunt.config.set('shifter', {
123 // Shifter takes a relative path.
124 paths: grunt.moodleEnv.files ? grunt.moodleEnv.files : [grunt.moodleEnv.runDir]
131 files: grunt.moodleEnv.inComponent
132 ? ['yui/src/*.json', 'yui/src/**/*.js']
133 : ['**/yui/src/**/*.js'],
139 // On watch, we dynamically modify config to build only affected files. This
140 // method is slightly complicated to deal with multiple changed files at once (copied
141 // from the grunt-contrib-watch readme).
142 let changedFiles = Object.create(null);
143 const onChange = grunt.util._.debounce(function() {
144 const files = Object.keys(changedFiles);
145 grunt.config('shifter.options.paths', files);
146 changedFiles = Object.create(null);
149 grunt.event.on('watch', (action, filepath) => {
150 changedFiles[filepath] = action;