MDL-74116 behat: Unrelated, add the @skip_interim tag
[moodle.git] / Gruntfile.js
bloba9b7120edf8e86cec52562492258b2a51619bb0c
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.
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 */
16 /* eslint-env node */
18 /**
19  * @copyright  2014 Andrew Nicols
20  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
21  */
23 /* eslint-env node */
25 /**
26  * Calculate the cwd, taking into consideration the `root` option (for Windows).
27  *
28  * @param {Object} grunt
29  * @returns {String} The current directory as best we can determine
30  */
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);
44         } else {
45             grunt.fail.fatal('Setting root to ' + root + ' failed - path does not exist');
46         }
47     }
49     return cwd;
52 /**
53  * Register any stylelint tasks.
54  *
55  * @param {Object} grunt
56  * @param {Array} files
57  * @param {String} fullRunDir
58  */
59 const registerStyleLintTasks = (grunt, files, fullRunDir) => {
60     const getCssConfigForFiles = files => {
61         return {
62             stylelint: {
63                 css: {
64                     // Use a fully-qualified path.
65                     src: files,
66                     options: {
67                         configOverrides: {
68                             rules: {
69                                 // These rules have to be disabled in .stylelintrc for scss compat.
70                                 "at-rule-no-unknown": true,
71                             }
72                         }
73                     }
74                 },
75             },
76         };
77     };
79     const getScssConfigForFiles = files => {
80         return {
81             stylelint: {
82                 scss: {
83                     options: {syntax: 'scss'},
84                     src: files,
85                 },
86             },
87         };
88     };
90     let hasCss = true;
91     let hasScss = true;
93     if (files) {
94         // Specific files were passed. Just set them up.
95         grunt.config.merge(getCssConfigForFiles(files));
96         grunt.config.merge(getScssConfigForFiles(files));
97     } else {
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');
102         const scssSrc = [];
103         glob.sync(`${fullRunDir}/**/*.scss`).forEach(path => scssSrc.push(path));
105         if (scssSrc.length) {
106             grunt.config.merge(getScssConfigForFiles(scssSrc));
107         } else {
108             hasScss = false;
109         }
111         const cssSrc = [];
112         glob.sync(`${fullRunDir}/**/*.css`).forEach(path => cssSrc.push(path));
114         if (cssSrc.length) {
115             grunt.config.merge(getCssConfigForFiles(cssSrc));
116         } else {
117             hasCss = false;
118         }
119     }
121     const scssTasks = ['sass'];
122     if (hasScss) {
123         scssTasks.unshift('stylelint:scss');
124     }
125     grunt.registerTask('scss', scssTasks);
127     const cssTasks = [];
128     if (hasCss) {
129         cssTasks.push('stylelint:css');
130     }
131     grunt.registerTask('rawcss', cssTasks);
133     grunt.registerTask('css', ['scss', 'rawcss']);
137  * Grunt configuration.
139  * @param {Object} grunt
140  */
141 module.exports = function(grunt) {
142     const path = require('path');
143     const tasks = {};
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);
159     }
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('============================================================================');
191     if (inComponent) {
192         grunt.log.ok(`Running tasks for component directory ${componentDirectory}`);
193     }
195     let files = null;
196     if (grunt.option('files')) {
197         // Accept a comma separated list of files to process.
198         files = grunt.option('files').split(',');
199     }
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.
206     let amdSrc = [];
207     if (inComponent) {
208         amdSrc.push(componentDirectory + "/amd/src/*.js");
209         amdSrc.push(componentDirectory + "/amd/src/**/*.js");
210     } else {
211         amdSrc = ComponentList.getAmdSrcGlobList();
212     }
214     let yuiSrc = [];
215     if (inComponent) {
216         yuiSrc.push(componentDirectory + "/yui/src/**/*.js");
217     } else {
218         yuiSrc = ComponentList.getYuiSrcGlobList(gruntFilePath + '/');
219     }
221     /**
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
226      *
227      * @param {String} destPath the current destination
228      * @param {String} srcPath the  matched src path
229      * @return {String} The rewritten destination path.
230      */
231     var babelRename = function(destPath, srcPath) {
232         destPath = srcPath.replace('src', 'build');
233         destPath = destPath.replace('.js', '.min.js');
234         return destPath;
235     };
237     /**
238      * Find thirdpartylibs.xml and generate an array of paths contained within
239      * them (used to generate ignore files and so on).
240      *
241      * @return {array} The list of thirdparty paths.
242      */
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(/\/?$/, '/');
258                 }
260                 // Look for duplicate paths before adding to array.
261                 if (libs.indexOf(lib) === -1) {
262                     libs.push(lib);
263                 }
264             });
265         });
267         return libs;
268     };
270     /**
271      * Get the list of feature files to pass to the gherkin linter.
272      *
273      * @returns {Array}
274      */
275     const getGherkinLintTargets = () => {
276         if (files) {
277             // Specific files were requested. Only check these.
278             return files;
279         }
281         if (inComponent) {
282             return [`${runDir}/tests/behat/*.feature`];
283         }
285         return ['**/tests/behat/*.feature'];
286     };
288     const babelTransform = require('@babel/core').transform;
289     const babel = (options = {}) => {
290         return {
291             name: 'babel',
293             transform: (code, id) => {
294                 grunt.log.debug(`Transforming ${id}`);
295                 options.filename = id;
296                 const transformed = babelTransform(code, options);
298                 return {
299                     code: transformed.code,
300                     map: transformed.map
301                 };
302             }
303         };
304     };
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 = () => {
310         const queue = [];
311         let queueRunner;
313         const startQueue = () => {
314             if (queueRunner) {
315                 return;
316             }
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}`);
321                 runQueue(limit);
322             }, 100);
323         };
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 => {
328                 resolve();
329             });
330         };
332         return {
333             name: 'ratelimit',
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 => {
339                     queue.push(resolve);
340                     startQueue();
341                 });
342             },
344             // When an item in the queue completes, start the next item in the queue.
345             buildEnd: () => {
346                 runQueue();
347             },
348         };
349     };
351     const terser = require('rollup-plugin-terser').terser;
353     // Project configuration.
354     grunt.initConfig({
355         eslint: {
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.
361             options: {
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)
364             },
365             amd: {src: files ? files : amdSrc},
366             // Check YUI module source files.
367             yui: {src: files ? files : yuiSrc},
368         },
369         rollup: {
370             dist: {
371                 options: {
372                     format: 'esm',
373                     dir: 'output',
374                     sourcemap: true,
375                     treeshake: false,
376                     context: 'window',
377                     plugins: [
378                         rateLimit({initialDelay: 0}),
379                         babel({
380                             sourceMaps: true,
381                             comments: false,
382                             compact: false,
383                             plugins: [
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.
389                                 //
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'
397                             ],
398                             presets: [
399                                 ['@babel/preset-env', {
400                                     targets: {
401                                         browsers: [
402                                             ">0.25%",
403                                             "last 2 versions",
404                                             "not ie <= 10",
405                                             "not op_mini all",
406                                             "not Opera > 0",
407                                             "not dead"
408                                         ]
409                                     },
410                                     modules: false,
411                                     useBuiltIns: false
412                                 }]
413                             ]
414                         }),
416                         terser({
417                             // Do not mangle variables.
418                             // Makes debugging easier.
419                             mangle: false,
420                         }),
421                     ],
422                 },
423                 files: [{
424                     expand: true,
425                     src: files ? files : amdSrc,
426                     rename: babelRename
427                 }],
428             },
429         },
430         sass: {
431             dist: {
432                 files: {
433                     "theme/boost/style/moodle.css": "theme/boost/scss/preset/default.scss",
434                     "theme/classic/style/moodle.css": "theme/classic/scss/classicgrunt.scss"
435                 }
436             },
437             options: {
438                 implementation: sass,
439                 includePaths: ["theme/boost/scss/", "theme/classic/scss/"]
440             }
441         },
442         watch: {
443             options: {
444                 nospawn: true // We need not to spawn so config can be changed dynamically.
445             },
446             amd: {
447                 files: inComponent
448                     ? ['amd/src/*.js', 'amd/src/**/*.js']
449                     : ['**/amd/src/**/*.js'],
450                 tasks: ['amd']
451             },
452             boost: {
453                 files: [inComponent ? 'scss/**/*.scss' : 'theme/boost/scss/**/*.scss'],
454                 tasks: ['scss']
455             },
456             rawcss: {
457                 files: [
458                     '**/*.css',
459                 ],
460                 excludes: [
461                     '**/moodle.css',
462                     '**/editor.css',
463                 ],
464                 tasks: ['rawcss']
465             },
466             yui: {
467                 files: inComponent
468                     ? ['yui/src/*.json', 'yui/src/**/*.js']
469                     : ['**/yui/src/**/*.js'],
470                 tasks: ['yui']
471             },
472             gherkinlint: {
473                 files: [inComponent ? 'tests/behat/*.feature' : '**/tests/behat/*.feature'],
474                 tasks: ['gherkinlint']
475             }
476         },
477         shifter: {
478             options: {
479                 recursive: true,
480                 // Shifter takes a relative path.
481                 paths: files ? files : [runDir]
482             }
483         },
484         gherkinlint: {
485             options: {
486                 files: getGherkinLintTargets(),
487             }
488         },
489     });
491     /**
492      * Generate ignore files (utilising thirdpartylibs.xml data)
493      */
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/',
501             '*/**/build/',
502         ].concat(thirdPartyPaths);
503         grunt.file.write('.eslintignore', eslintIgnores.join('\n') + '\n');
505         // Generate .stylelintignore.
506         const stylelintIgnores = [
507             '# Generated by "grunt ignorefiles"',
508             '**/yui/build/*',
509             'theme/boost/style/moodle.css',
510             'theme/classic/style/moodle.css',
511         ].concat(thirdPartyPaths);
512         grunt.file.write('.stylelintignore', stylelintIgnores.join('\n') + '\n');
513     };
515     /**
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.
518      *
519      * Note that this task runs the invidiaul shifter jobs async (becase it spawns
520      * so be careful to to call done().
521      */
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) {
528             var args = [];
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
537                 // watch).
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');
544                 args.push('--walk');
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;
555             }
557             if (grunt.option('watch')) {
558                 grunt.fail.fatal('The --watch option has been removed, please use `grunt watch` instead');
559             }
561             // Add the stderr option if appropriate
562             if (grunt.option('verbose')) {
563                 args.push('--lint-stderr');
564             }
566             if (grunt.option('no-color')) {
567                 args.push('--color=false');
568             }
570             var execShifter = function() {
572                 grunt.log.ok("Running shifter on " + src);
573                 grunt.util.spawn({
574                     cmd: "node",
575                     args: args,
576                     opts: {cwd: src, stdio: 'inherit', env: process.env}
577                 }, function(error, result, code) {
578                     if (code) {
579                         grunt.fail.fatal('Shifter failed with code: ' + code);
580                     } else {
581                         grunt.log.ok('Shifter build complete.');
582                         filedone();
583                     }
584                 });
585             };
587             // Actually run shifter.
588             if (!options.recursive) {
589                 execShifter();
590             } else {
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');
594                     execShifter();
595                 } else {
596                     grunt.log.ok('No YUI modules to build.');
597                     filedone();
598                 }
599             }
600         }, done);
601     };
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');
613         // Run the linter.
614         return linter.lint(
615             featureFinder.getFeatureFiles(grunt.file.expand(options.files)),
616             configParser.getConfiguration(configParser.defaultConfigFileName)
617         )
618         .then(results => {
619             // Print the results out uncondtionally.
620             formatter.printResults(results);
622             return results;
623         })
624         .then(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);
628         })
629         .then(done); // eslint-disable-line promise/no-callback-in-promise
630     };
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?
637         } else if (inAMD) {
638             grunt.task.run('amd');
639         } else {
640             // Run them all!.
641             grunt.task.run('css');
642             grunt.task.run('js');
643             grunt.task.run('gherkinlint');
644         }
645     };
647     /**
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.
650      *
651      * If Watchman isn't installed then it falls back to the grunt-contrib-watch file
652      * watcher for backwards compatibility.
653      */
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.
664                 return;
665             }
667             processingQueue = true;
669             // Grab all tasks currently in the queue.
670             var queueToProcess = watchTaskQueue;
671             // Reset the queue.
672             watchTaskQueue = {};
674             async.forEachSeries(
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
682                     // if it failed.
683                     grunt.util.spawn(
684                         {
685                             // Spawn with the grunt bin.
686                             grunt: true,
687                             // Run from current working dir and inherit stdio from process.
688                             opts: {
689                                 cwd: fullRunDir,
690                                 stdio: 'inherit'
691                             },
692                             args: [task, filesOption]
693                         },
694                         function(err, res, code) {
695                             if (code !== 0) {
696                                 // The grunt task failed.
697                                 grunt.log.error(err);
698                             }
700                             // Move on to the next task.
701                             next();
702                         }
703                     );
704                 },
705                 function() {
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();
711                 }
712             );
713         };
715         const originalWatchConfig = grunt.config.get(['watch']);
716         const watchConfig = Object.keys(originalWatchConfig).reduce(function(carry, key) {
717             if (key == 'options') {
718                 return carry;
719             }
721             const value = originalWatchConfig[key];
723             const taskNames = value.tasks;
724             const files = value.files;
725             let excludes = [];
726             if (value.excludes) {
727                 excludes = value.excludes;
728             }
730             taskNames.forEach(function(taskName) {
731                 carry[taskName] = {
732                     files,
733                     excludes,
734                 };
735             });
737             return carry;
738         }, {});
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.
752                 watchTaskDone(0);
753             } else {
754                 grunt.log.error(error);
755                 // Fatal error.
756                 watchTaskDone(1);
757             }
758         });
760         watchmanClient.on('subscription', function(resp) {
761             if (resp.subscription !== 'grunt-watch') {
762                 return;
763             }
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);
774                     });
776                     if (match) {
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);
785                             }
786                         } else {
787                             watchTaskQueue[task] = [relativePath];
788                         }
789                     }
790                 });
791             });
793             processWatchTaskQueue();
794         });
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.');
801             }
803             process.exit();
804         });
806         // Initiate the watch on the current directory.
807         watchmanClient.command(['watch-project', fullRunDir], function(watchError, watchResponse) {
808             if (watchError) {
809                 grunt.log.error('Error initiating watch:', watchError);
810                 watchTaskDone(1);
811                 return;
812             }
814             if ('warning' in watchResponse) {
815                 grunt.log.error('warning: ', watchResponse.warning);
816             }
818             var watch = watchResponse.watch;
819             var relativePath = watchResponse.relative_path;
820             watchInitialised = true;
822             watchmanClient.command(['clock', watch], function(clockError, clockResponse) {
823                 if (clockError) {
824                     grunt.log.error('Failed to query clock:', clockError);
825                     watchTaskDone(1);
826                     return;
827                 }
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.
833                 //
834                 // [anyof, [
835                 //      [allof, [
836                 //          [anyof, [
837                 //              ['match', validPath, 'wholename'],
838                 //              ['match', validPath, 'wholename'],
839                 //          ],
840                 //          [not,
841                 //              [anyof, [
842                 //                  ['match', invalidPath, 'wholename'],
843                 //                  ['match', invalidPath, 'wholename'],
844                 //              ],
845                 //          ],
846                 //      ],
847                 var matchWholeName = fileGlob => ['match', fileGlob, 'wholename'];
848                 var matches = Object.keys(watchConfig).map(function(task) {
849                     const matchAll = [];
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))]);
854                     }
856                     return ['allof'].concat(matchAll);
857                 });
859                 matches = ['anyof'].concat(matches);
861                 var sub = {
862                     expression: matches,
863                     // Which fields we're interested in.
864                     fields: ["name", "size", "type"],
865                     // Add our time constraint.
866                     since: clockResponse.clock
867                 };
869                 if (relativePath) {
870                     /* eslint-disable camelcase */
871                     sub.relative_root = relativePath;
872                 }
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);
878                         watchTaskDone(1);
879                         return;
880                     }
882                     grunt.log.ok('Listening for changes to files in ' + fullRunDir);
883                 });
884             });
885         });
886     };
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);
900     }, 200);
902     grunt.event.on('watch', function(action, filepath) {
903         changedFiles[filepath] = action;
904         onChange();
905     });
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']);