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 2014 Andrew Nicols
20 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 * Calculate the cwd, taking into consideration the `root` option (for Windows).
28 * @param {Object} grunt
29 * @returns {String} The current directory as best we can determine
31 const getCwd = grunt => {
32 const fs = require('fs');
33 const path = require('path');
35 let cwd = fs.realpathSync(process.env.PWD || process.cwd());
37 // Windows users can't run grunt in a subdirectory, so allow them to set
38 // the root by passing --root=path/to/dir.
39 if (grunt.option('root')) {
40 const root = grunt.option('root');
41 if (grunt.file.exists(__dirname, root)) {
42 cwd = fs.realpathSync(path.join(__dirname, root));
43 grunt.log.ok('Setting root to ' + cwd);
45 grunt.fail.fatal('Setting root to ' + root + ' failed - path does not exist');
53 * Register any stylelint tasks.
55 * @param {Object} grunt
56 * @param {Array} files
57 * @param {String} fullRunDir
59 const registerStyleLintTasks = (grunt, files, fullRunDir) => {
60 const getCssConfigForFiles = files => {
64 // Use a fully-qualified path.
69 // These rules have to be disabled in .stylelintrc for scss compat.
70 "at-rule-no-unknown": true,
79 const getScssConfigForFiles = files => {
83 options: {syntax: 'scss'},
94 // Specific files were passed. Just set them up.
95 grunt.config.merge(getCssConfigForFiles(files));
96 grunt.config.merge(getScssConfigForFiles(files));
98 // The stylelint system does not handle the case where there was no file to lint.
99 // Check whether there are any files to lint in the current directory.
100 const glob = require('glob');
103 glob.sync(`${fullRunDir}/**/*.scss`).forEach(path => scssSrc.push(path));
105 if (scssSrc.length) {
106 grunt.config.merge(getScssConfigForFiles(scssSrc));
112 glob.sync(`${fullRunDir}/**/*.css`).forEach(path => cssSrc.push(path));
115 grunt.config.merge(getCssConfigForFiles(cssSrc));
121 const scssTasks = ['sass'];
123 scssTasks.unshift('stylelint:scss');
125 grunt.registerTask('scss', scssTasks);
129 cssTasks.push('stylelint:css');
131 grunt.registerTask('rawcss', cssTasks);
133 grunt.registerTask('css', ['scss', 'rawcss']);
137 * Grunt configuration.
139 * @param {Object} grunt
141 module.exports = function(grunt) {
142 const path = require('path');
144 const async = require('async');
145 const DOMParser = require('xmldom').DOMParser;
146 const xpath = require('xpath');
147 const semver = require('semver');
148 const watchman = require('fb-watchman');
149 const watchmanClient = new watchman.Client();
150 const fs = require('fs');
151 const ComponentList = require(path.resolve('GruntfileComponents.js'));
152 const sass = require('node-sass');
154 // Verify the node version is new enough.
155 var expected = semver.validRange(grunt.file.readJSON('package.json').engines.node);
156 var actual = semver.valid(process.version);
157 if (!semver.satisfies(actual, expected)) {
158 grunt.fail.fatal('Node version not satisfied. Require ' + expected + ', version installed: ' + actual);
161 // Detect directories:
162 // * gruntFilePath The real path on disk to this Gruntfile.js
163 // * cwd The current working directory, which can be overridden by the `root` option
164 // * relativeCwd The cwd, relative to the Gruntfile.js
165 // * componentDirectory The root directory of the component if the cwd is in a valid component
166 // * inComponent Whether the cwd is in a valid component
167 // * runDir The componentDirectory or cwd if not in a component, relative to Gruntfile.js
168 // * fullRunDir The full path to the runDir
169 const gruntFilePath = fs.realpathSync(process.cwd());
170 const cwd = getCwd(grunt);
171 const relativeCwd = path.relative(gruntFilePath, cwd);
172 const componentDirectory = ComponentList.getOwningComponentDirectory(relativeCwd);
173 const inComponent = !!componentDirectory;
174 const runDir = inComponent ? componentDirectory : relativeCwd;
175 const fullRunDir = fs.realpathSync(gruntFilePath + path.sep + runDir);
176 grunt.log.debug('============================================================================');
177 grunt.log.debug(`= Node version: ${process.versions.node}`);
178 grunt.log.debug(`= grunt version: ${grunt.package.version}`);
179 grunt.log.debug(`= process.cwd: '` + process.cwd() + `'`);
180 grunt.log.debug(`= process.env.PWD: '${process.env.PWD}'`);
181 grunt.log.debug(`= path.sep '${path.sep}'`);
182 grunt.log.debug('============================================================================');
183 grunt.log.debug(`= gruntFilePath: '${gruntFilePath}'`);
184 grunt.log.debug(`= relativeCwd: '${relativeCwd}'`);
185 grunt.log.debug(`= componentDirectory: '${componentDirectory}'`);
186 grunt.log.debug(`= inComponent: '${inComponent}'`);
187 grunt.log.debug(`= runDir: '${runDir}'`);
188 grunt.log.debug(`= fullRunDir: '${fullRunDir}'`);
189 grunt.log.debug('============================================================================');
192 grunt.log.ok(`Running tasks for component directory ${componentDirectory}`);
196 if (grunt.option('files')) {
197 // Accept a comma separated list of files to process.
198 files = grunt.option('files').split(',');
201 // If the cwd is the amd directory in the current component then it will be empty.
202 // If the cwd is a child of the component's AMD directory, the relative directory will not start with ..
203 const inAMD = !path.relative(`${componentDirectory}/amd`, cwd).startsWith('..');
205 // Globbing pattern for matching all AMD JS source files.
208 amdSrc.push(componentDirectory + "/amd/src/*.js");
209 amdSrc.push(componentDirectory + "/amd/src/**/*.js");
211 amdSrc = ComponentList.getAmdSrcGlobList();
216 yuiSrc.push(componentDirectory + "/yui/src/**/*.js");
218 yuiSrc = ComponentList.getYuiSrcGlobList(gruntFilePath + '/');
222 * Function to generate the destination for the uglify task
223 * (e.g. build/file.min.js). This function will be passed to
224 * the rename property of files array when building dynamically:
225 * http://gruntjs.com/configuring-tasks#building-the-files-object-dynamically
227 * @param {String} destPath the current destination
228 * @param {String} srcPath the matched src path
229 * @return {String} The rewritten destination path.
231 var babelRename = function(destPath, srcPath) {
232 destPath = srcPath.replace('src', 'build');
233 destPath = destPath.replace('.js', '.min.js');
238 * Find thirdpartylibs.xml and generate an array of paths contained within
239 * them (used to generate ignore files and so on).
241 * @return {array} The list of thirdparty paths.
243 var getThirdPartyPathsFromXML = function() {
244 const thirdpartyfiles = ComponentList.getThirdPartyLibsList(gruntFilePath + '/');
245 const libs = ['node_modules/', 'vendor/'];
247 thirdpartyfiles.forEach(function(file) {
248 const dirname = path.dirname(file);
250 const doc = new DOMParser().parseFromString(grunt.file.read(file));
251 const nodes = xpath.select("/libraries/library/location/text()", doc);
253 nodes.forEach(function(node) {
254 let lib = path.posix.join(dirname, node.toString());
255 if (grunt.file.isDir(lib)) {
256 // Ensure trailing slash on dirs.
257 lib = lib.replace(/\/?$/, '/');
260 // Look for duplicate paths before adding to array.
261 if (libs.indexOf(lib) === -1) {
271 * Get the list of feature files to pass to the gherkin linter.
275 const getGherkinLintTargets = () => {
277 // Specific files were requested. Only check these.
282 return [`${runDir}/tests/behat/*.feature`];
285 return ['**/tests/behat/*.feature'];
288 const babelTransform = require('@babel/core').transform;
289 const babel = (options = {}) => {
293 transform: (code, id) => {
294 grunt.log.debug(`Transforming ${id}`);
295 options.filename = id;
296 const transformed = babelTransform(code, options);
299 code: transformed.code,
306 // Note: We have to use a rate limit plugin here because rollup runs all tasks asynchronously and in parallel.
307 // When we kick off a full run, if we kick off a rollup of every file this will fork-bomb the machine.
308 // To work around this we use a concurrent Promise queue based on the number of available processors.
309 const rateLimit = () => {
313 const startQueue = () => {
318 queueRunner = setTimeout(() => {
319 const limit = Math.max(1, require('os').cpus().length / 2);
320 grunt.log.debug(`Starting rollup with queue size of ${limit}`);
325 // The queue runner will run the next `size` items in the queue.
326 const runQueue = (size = 1) => {
327 queue.splice(0, size).forEach(resolve => {
335 // The options hook is run in parallel.
336 // We can return an unresolved Promise which is queued for later resolution.
337 options: async() => {
338 return new Promise(resolve => {
344 // When an item in the queue completes, start the next item in the queue.
351 const terser = require('rollup-plugin-terser').terser;
353 // Project configuration.
356 // Even though warnings dont stop the build we don't display warnings by default because
357 // at this moment we've got too many core warnings.
358 // To display warnings call: grunt eslint --show-lint-warnings
359 // To fail on warnings call: grunt eslint --max-lint-warnings=0
360 // Also --max-lint-warnings=-1 can be used to display warnings but not fail.
362 quiet: (!grunt.option('show-lint-warnings')) && (typeof grunt.option('max-lint-warnings') === 'undefined'),
363 maxWarnings: ((typeof grunt.option('max-lint-warnings') !== 'undefined') ? grunt.option('max-lint-warnings') : -1)
365 amd: {src: files ? files : amdSrc},
366 // Check YUI module source files.
367 yui: {src: files ? files : yuiSrc},
378 rateLimit({initialDelay: 0}),
384 'transform-es2015-modules-amd-lazy',
385 'system-import-transformer',
386 // This plugin modifies the Babel transpiling for "export default"
387 // so that if it's used then only the exported value is returned
388 // by the generated AMD module.
390 // It also adds the Moodle plugin name to the AMD module definition
391 // so that it can be imported as expected in other modules.
392 path.resolve('babel-plugin-add-module-to-define.js'),
393 '@babel/plugin-syntax-dynamic-import',
394 '@babel/plugin-syntax-import-meta',
395 ['@babel/plugin-proposal-class-properties', {'loose': false}],
396 '@babel/plugin-proposal-json-strings'
399 ['@babel/preset-env', {
417 // Do not mangle variables.
418 // Makes debugging easier.
425 src: files ? files : amdSrc,
433 "theme/boost/style/moodle.css": "theme/boost/scss/preset/default.scss",
434 "theme/classic/style/moodle.css": "theme/classic/scss/classicgrunt.scss"
438 implementation: sass,
439 includePaths: ["theme/boost/scss/", "theme/classic/scss/"]
444 nospawn: true // We need not to spawn so config can be changed dynamically.
448 ? ['amd/src/*.js', 'amd/src/**/*.js']
449 : ['**/amd/src/**/*.js'],
453 files: [inComponent ? 'scss/**/*.scss' : 'theme/boost/scss/**/*.scss'],
468 ? ['yui/src/*.json', 'yui/src/**/*.js']
469 : ['**/yui/src/**/*.js'],
473 files: [inComponent ? 'tests/behat/*.feature' : '**/tests/behat/*.feature'],
474 tasks: ['gherkinlint']
480 // Shifter takes a relative path.
481 paths: files ? files : [runDir]
486 files: getGherkinLintTargets(),
492 * Generate ignore files (utilising thirdpartylibs.xml data)
494 tasks.ignorefiles = function() {
495 // An array of paths to third party directories.
496 const thirdPartyPaths = getThirdPartyPathsFromXML();
497 // Generate .eslintignore.
498 const eslintIgnores = [
499 '# Generated by "grunt ignorefiles"',
500 '*/**/yui/src/*/meta/',
502 ].concat(thirdPartyPaths);
503 grunt.file.write('.eslintignore', eslintIgnores.join('\n') + '\n');
505 // Generate .stylelintignore.
506 const stylelintIgnores = [
507 '# Generated by "grunt ignorefiles"',
509 'theme/boost/style/moodle.css',
510 'theme/classic/style/moodle.css',
511 ].concat(thirdPartyPaths);
512 grunt.file.write('.stylelintignore', stylelintIgnores.join('\n') + '\n');
516 * Shifter task. Is configured with a path to a specific file or a directory,
517 * in the case of a specific file it will work out the right module to be built.
519 * Note that this task runs the invidiaul shifter jobs async (becase it spawns
520 * so be careful to to call done().
522 tasks.shifter = function() {
523 var done = this.async(),
524 options = grunt.config('shifter.options');
526 // Run the shifter processes one at a time to avoid confusing output.
527 async.eachSeries(options.paths, function(src, filedone) {
529 args.push(path.normalize(__dirname + '/node_modules/shifter/bin/shifter'));
531 // Always ignore the node_modules directory.
532 args.push('--excludes', 'node_modules');
534 // Determine the most appropriate options to run with based upon the current location.
535 if (grunt.file.isMatch('**/yui/**/*.js', src)) {
536 // When passed a JS file, build our containing module (this happen with
538 grunt.log.debug('Shifter passed a specific JS file');
539 src = path.dirname(path.dirname(src));
540 options.recursive = false;
541 } else if (grunt.file.isMatch('**/yui/src', src)) {
542 // When in a src directory --walk all modules.
543 grunt.log.debug('In a src directory');
545 options.recursive = false;
546 } else if (grunt.file.isMatch('**/yui/src/*', src)) {
547 // When in module, only build our module.
548 grunt.log.debug('In a module directory');
549 options.recursive = false;
550 } else if (grunt.file.isMatch('**/yui/src/*/js', src)) {
551 // When in module src, only build our module.
552 grunt.log.debug('In a source directory');
553 src = path.dirname(src);
554 options.recursive = false;
557 if (grunt.option('watch')) {
558 grunt.fail.fatal('The --watch option has been removed, please use `grunt watch` instead');
561 // Add the stderr option if appropriate
562 if (grunt.option('verbose')) {
563 args.push('--lint-stderr');
566 if (grunt.option('no-color')) {
567 args.push('--color=false');
570 var execShifter = function() {
572 grunt.log.ok("Running shifter on " + src);
576 opts: {cwd: src, stdio: 'inherit', env: process.env}
577 }, function(error, result, code) {
579 grunt.fail.fatal('Shifter failed with code: ' + code);
581 grunt.log.ok('Shifter build complete.');
587 // Actually run shifter.
588 if (!options.recursive) {
591 // Check that there are yui modules otherwise shifter ends with exit code 1.
592 if (grunt.file.expand({cwd: src}, '**/yui/src/**/*.js').length > 0) {
593 args.push('--recursive');
596 grunt.log.ok('No YUI modules to build.');
603 tasks.gherkinlint = function() {
604 const done = this.async();
605 const options = grunt.config('gherkinlint.options');
607 // Grab the gherkin-lint linter and required scaffolding.
608 const linter = require('gherkin-lint/dist/linter.js');
609 const featureFinder = require('gherkin-lint/dist/feature-finder.js');
610 const configParser = require('gherkin-lint/dist/config-parser.js');
611 const formatter = require('gherkin-lint/dist/formatters/stylish.js');
615 featureFinder.getFeatureFiles(grunt.file.expand(options.files)),
616 configParser.getConfiguration(configParser.defaultConfigFileName)
619 // Print the results out uncondtionally.
620 formatter.printResults(results);
625 // Report on the results.
626 // The done function takes a bool whereby a falsey statement causes the task to fail.
627 return results.every(result => result.errors.length === 0);
629 .then(done); // eslint-disable-line promise/no-callback-in-promise
632 tasks.startup = function() {
633 // Are we in a YUI directory?
634 if (path.basename(path.resolve(cwd, '../../')) == 'yui') {
635 grunt.task.run('yui');
636 // Are we in an AMD directory?
638 grunt.task.run('amd');
641 grunt.task.run('css');
642 grunt.task.run('js');
643 grunt.task.run('gherkinlint');
648 * This is a wrapper task to handle the grunt watch command. It attempts to use
649 * Watchman to monitor for file changes, if it's installed, because it's much faster.
651 * If Watchman isn't installed then it falls back to the grunt-contrib-watch file
652 * watcher for backwards compatibility.
654 tasks.watch = function() {
655 var watchTaskDone = this.async();
656 var watchInitialised = false;
657 var watchTaskQueue = {};
658 var processingQueue = false;
660 // Grab the tasks and files that have been queued up and execute them.
661 var processWatchTaskQueue = function() {
662 if (!Object.keys(watchTaskQueue).length || processingQueue) {
663 // If there is nothing in the queue or we're already processing then wait.
667 processingQueue = true;
669 // Grab all tasks currently in the queue.
670 var queueToProcess = watchTaskQueue;
675 Object.keys(queueToProcess),
676 function(task, next) {
677 var files = queueToProcess[task];
678 var filesOption = '--files=' + files.join(',');
679 grunt.log.ok('Running task ' + task + ' for files ' + filesOption);
681 // Spawn the task in a child process so that it doesn't kill this one
685 // Spawn with the grunt bin.
687 // Run from current working dir and inherit stdio from process.
692 args: [task, filesOption]
694 function(err, res, code) {
696 // The grunt task failed.
697 grunt.log.error(err);
700 // Move on to the next task.
706 // No longer processing.
707 processingQueue = false;
708 // Once all of the tasks are done then recurse just in case more tasks
709 // were queued while we were processing.
710 processWatchTaskQueue();
715 const originalWatchConfig = grunt.config.get(['watch']);
716 const watchConfig = Object.keys(originalWatchConfig).reduce(function(carry, key) {
717 if (key == 'options') {
721 const value = originalWatchConfig[key];
723 const taskNames = value.tasks;
724 const files = value.files;
726 if (value.excludes) {
727 excludes = value.excludes;
730 taskNames.forEach(function(taskName) {
740 watchmanClient.on('error', function(error) {
741 // We have to add an error handler here and parse the error string because the
742 // example way from the docs to check if Watchman is installed doesn't actually work!!
743 // See: https://github.com/facebook/watchman/issues/509
744 if (error.message.match('Watchman was not found')) {
745 // If watchman isn't installed then we should fallback to the other watch task.
746 grunt.log.ok('It is recommended that you install Watchman for better performance using the "watch" command.');
748 // Fallback to the old grunt-contrib-watch task.
749 grunt.renameTask('watch-grunt', 'watch');
750 grunt.task.run(['watch']);
751 // This task is finished.
754 grunt.log.error(error);
760 watchmanClient.on('subscription', function(resp) {
761 if (resp.subscription !== 'grunt-watch') {
765 resp.files.forEach(function(file) {
766 grunt.log.ok('File changed: ' + file.name);
768 var fullPath = fullRunDir + '/' + file.name;
769 Object.keys(watchConfig).forEach(function(task) {
771 const fileGlobs = watchConfig[task].files;
772 var match = fileGlobs.some(function(fileGlob) {
773 return grunt.file.isMatch(`**/${fileGlob}`, fullPath);
777 // If we are watching a subdirectory then the file.name will be relative
778 // to that directory. However the grunt tasks expect the file paths to be
779 // relative to the Gruntfile.js location so let's normalise them before
780 // adding them to the queue.
781 var relativePath = fullPath.replace(gruntFilePath + '/', '');
782 if (task in watchTaskQueue) {
783 if (!watchTaskQueue[task].includes(relativePath)) {
784 watchTaskQueue[task] = watchTaskQueue[task].concat(relativePath);
787 watchTaskQueue[task] = [relativePath];
793 processWatchTaskQueue();
796 process.on('SIGINT', function() {
797 // Let the user know that they may need to manually stop the Watchman daemon if they
798 // no longer want it running.
799 if (watchInitialised) {
800 grunt.log.ok('The Watchman daemon may still be running and may need to be stopped manually.');
806 // Initiate the watch on the current directory.
807 watchmanClient.command(['watch-project', fullRunDir], function(watchError, watchResponse) {
809 grunt.log.error('Error initiating watch:', watchError);
814 if ('warning' in watchResponse) {
815 grunt.log.error('warning: ', watchResponse.warning);
818 var watch = watchResponse.watch;
819 var relativePath = watchResponse.relative_path;
820 watchInitialised = true;
822 watchmanClient.command(['clock', watch], function(clockError, clockResponse) {
824 grunt.log.error('Failed to query clock:', clockError);
829 // Generate the expression query used by watchman.
830 // Documentation is limited, but see https://facebook.github.io/watchman/docs/expr/allof.html for examples.
831 // We generate an expression to match any value in the files list of all of our tasks, but excluding
832 // all value in the excludes list of that task.
837 // ['match', validPath, 'wholename'],
838 // ['match', validPath, 'wholename'],
842 // ['match', invalidPath, 'wholename'],
843 // ['match', invalidPath, 'wholename'],
847 var matchWholeName = fileGlob => ['match', fileGlob, 'wholename'];
848 var matches = Object.keys(watchConfig).map(function(task) {
850 matchAll.push(['anyof'].concat(watchConfig[task].files.map(matchWholeName)));
852 if (watchConfig[task].excludes.length) {
853 matchAll.push(['not', ['anyof'].concat(watchConfig[task].excludes.map(matchWholeName))]);
856 return ['allof'].concat(matchAll);
859 matches = ['anyof'].concat(matches);
863 // Which fields we're interested in.
864 fields: ["name", "size", "type"],
865 // Add our time constraint.
866 since: clockResponse.clock
870 /* eslint-disable camelcase */
871 sub.relative_root = relativePath;
874 watchmanClient.command(['subscribe', watch, 'grunt-watch', sub], function(subscribeError) {
875 if (subscribeError) {
876 // Probably an error in the subscription criteria.
877 grunt.log.error('failed to subscribe: ', subscribeError);
882 grunt.log.ok('Listening for changes to files in ' + fullRunDir);
888 // On watch, we dynamically modify config to build only affected files. This
889 // method is slightly complicated to deal with multiple changed files at once (copied
890 // from the grunt-contrib-watch readme).
891 var changedFiles = Object.create(null);
892 var onChange = grunt.util._.debounce(function() {
893 var files = Object.keys(changedFiles);
894 grunt.config('eslint.amd.src', files);
895 grunt.config('eslint.yui.src', files);
896 grunt.config('shifter.options.paths', files);
897 grunt.config('gherkinlint.options.files', files);
898 grunt.config('babel.dist.files', [{expand: true, src: files, rename: babelRename}]);
899 changedFiles = Object.create(null);
902 grunt.event.on('watch', function(action, filepath) {
903 changedFiles[filepath] = action;
907 // Register NPM tasks.
908 grunt.loadNpmTasks('grunt-contrib-uglify');
909 grunt.loadNpmTasks('grunt-contrib-watch');
910 grunt.loadNpmTasks('grunt-sass');
911 grunt.loadNpmTasks('grunt-eslint');
912 grunt.loadNpmTasks('grunt-stylelint');
913 grunt.loadNpmTasks('grunt-rollup');
916 // Rename the grunt-contrib-watch "watch" task because we're going to wrap it.
917 grunt.renameTask('watch', 'watch-grunt');
919 // Register JS tasks.
920 grunt.registerTask('shifter', 'Run Shifter against the current directory', tasks.shifter);
921 grunt.registerTask('gherkinlint', 'Run gherkinlint against the current directory', tasks.gherkinlint);
922 grunt.registerTask('ignorefiles', 'Generate ignore files for linters', tasks.ignorefiles);
923 grunt.registerTask('watch', 'Run tasks on file changes', tasks.watch);
924 grunt.registerTask('yui', ['eslint:yui', 'shifter']);
925 grunt.registerTask('amd', ['eslint:amd', 'rollup']);
926 grunt.registerTask('js', ['amd', 'yui']);
928 // Register CSS tasks.
929 registerStyleLintTasks(grunt, files, fullRunDir);
931 // Register the startup task.
932 grunt.registerTask('startup', 'Run the correct tasks for the current directory', tasks.startup);
934 // Register the default task.
935 grunt.registerTask('default', ['startup']);