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 * Component Library build tasks.
21 * @copyright 2021 Andrew Nicols
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 module.exports = grunt => {
28 * Get a child path of the component library.
30 * @param {string} path
33 const getCLPath = path => `admin/tool/componentlibrary/${path}`;
36 * Get a spawn handler.
38 * This is a generic function to write the spawn output, and then to exit if required and mark the async task as
41 * @param {Promise} done
44 const getSpawnHandler = done => (error, result, code) => {
45 grunt.log.write(result);
48 grunt.log.error(result.stdout);
55 * Spawn a function against Node with the provided args.
60 const spawnNodeCall = (args) => grunt.util.spawn({
63 }, getSpawnHandler(grunt.task.current.async()));
66 * Build the docs using Hugo.
68 * @returns {Object} Reference to the spawned task
70 const docsBuild = () => spawnNodeCall([
71 'node_modules/hugo-bin/cli.js',
72 '--config', getCLPath('config.yml'),
73 '--cleanDestinationDir',
77 * Build the docs index using the hugo-lunr-indexer.
79 * @returns {Object} Reference to the spawned task
81 const indexBuild = () => spawnNodeCall([
82 'node_modules/hugo-lunr-indexer/bin/hli.js',
83 '-i', getCLPath('content/**'),
84 '-o', getCLPath('hugo/site/data/my-index.json'),
92 * @returns {Object} Reference to the spawned task
94 const cssBuild = () => spawnNodeCall([
95 'node_modules/.bin/sass',
96 '--style', 'expanded',
100 '--load-path', process.cwd(),
101 getCLPath('hugo/scss/docs.scss'),
102 getCLPath('hugo/dist/css/docs.css'),
105 // Register the various component library tasks.
106 grunt.registerTask('componentlibrary:docsBuild', 'Build the component library', docsBuild);
107 grunt.registerTask('componentlibrary:cssBuild', 'Build the component library', cssBuild);
108 grunt.registerTask('componentlibrary:indexBuild', 'Build the component library', indexBuild);
109 grunt.registerTask('componentlibrary', 'Build the component library', [
110 'componentlibrary:docsBuild',
111 'componentlibrary:cssBuild',
112 'componentlibrary:indexBuild',
117 componentLibraryDocs: {
119 getCLPath('content/**/*.md'),
122 tasks: ['componentlibrary:docsBuild', 'componentlibrary:indexBuild'],
124 componentLibraryCSS: {
126 getCLPath('hugo/scss/**/*.scss'),
129 tasks: ['componentlibrary:cssBuild'],
134 // Add the 'componentlibrary' task as a startup task.
135 grunt.moodleEnv.startupTasks.push('componentlibrary');