1441 |
ariadna |
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 |
/* jshint node: true, browser: false */
|
|
|
16 |
/* eslint-env node */
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* Component Library build tasks.
|
|
|
20 |
*
|
|
|
21 |
* @copyright 2021 Andrew Nicols
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
module.exports = grunt => {
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* Get a child path of the component library.
|
|
|
29 |
*
|
|
|
30 |
* @param {string} path
|
|
|
31 |
* @returns {string}
|
|
|
32 |
*/
|
|
|
33 |
const getCLPath = path => `admin/tool/componentlibrary/${path}`;
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Get a spawn handler.
|
|
|
37 |
*
|
|
|
38 |
* This is a generic function to write the spawn output, and then to exit if required and mark the async task as
|
|
|
39 |
* complete.
|
|
|
40 |
*
|
|
|
41 |
* @param {Promise} done
|
|
|
42 |
* @returns {function}
|
|
|
43 |
*/
|
|
|
44 |
const getSpawnHandler = done => (error, result, code) => {
|
|
|
45 |
grunt.log.write(result);
|
|
|
46 |
|
|
|
47 |
if (error) {
|
|
|
48 |
grunt.log.error(result.stdout);
|
|
|
49 |
process.exit(code);
|
|
|
50 |
}
|
|
|
51 |
done();
|
|
|
52 |
};
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* Spawn a function against Node with the provided args.
|
|
|
56 |
*
|
|
|
57 |
* @param {array} args
|
|
|
58 |
* @returns {object}
|
|
|
59 |
*/
|
|
|
60 |
const spawnNodeCall = (args) => grunt.util.spawn({
|
|
|
61 |
cmd: 'node',
|
|
|
62 |
args,
|
|
|
63 |
}, getSpawnHandler(grunt.task.current.async()));
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* Build the docs using Hugo.
|
|
|
67 |
*
|
|
|
68 |
* @returns {Object} Reference to the spawned task
|
|
|
69 |
*/
|
|
|
70 |
const docsBuild = () => spawnNodeCall([
|
|
|
71 |
'node_modules/hugo-bin/cli.js',
|
|
|
72 |
'--config', getCLPath('config.yml'),
|
|
|
73 |
'--cleanDestinationDir',
|
|
|
74 |
]);
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* Build the docs index using the hugo-lunr-indexer.
|
|
|
78 |
*
|
|
|
79 |
* @returns {Object} Reference to the spawned task
|
|
|
80 |
*/
|
|
|
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'),
|
|
|
85 |
'-l', 'yaml',
|
|
|
86 |
'-d', '---',
|
|
|
87 |
]);
|
|
|
88 |
|
|
|
89 |
/**
|
|
|
90 |
* Build the hugo CSS.
|
|
|
91 |
*
|
|
|
92 |
* @returns {Object} Reference to the spawned task
|
|
|
93 |
*/
|
|
|
94 |
const cssBuild = () => spawnNodeCall([
|
|
|
95 |
'node_modules/sass/sass.js',
|
|
|
96 |
'--style', 'expanded',
|
|
|
97 |
'--source-map',
|
|
|
98 |
'--embed-sources',
|
|
|
99 |
'--precision', 6,
|
|
|
100 |
'--load-path', process.cwd(),
|
|
|
101 |
getCLPath('hugo/scss/docs.scss'),
|
|
|
102 |
getCLPath('hugo/dist/css/docs.css'),
|
|
|
103 |
]);
|
|
|
104 |
|
|
|
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',
|
|
|
113 |
]);
|
|
|
114 |
|
|
|
115 |
grunt.config.merge({
|
|
|
116 |
watch: {
|
|
|
117 |
componentLibraryDocs: {
|
|
|
118 |
files: [
|
|
|
119 |
getCLPath('content/**/*.md'),
|
|
|
120 |
getCLPath('hugo'),
|
|
|
121 |
],
|
|
|
122 |
tasks: ['componentlibrary:docsBuild', 'componentlibrary:indexBuild'],
|
|
|
123 |
},
|
|
|
124 |
componentLibraryCSS: {
|
|
|
125 |
files: [
|
|
|
126 |
getCLPath('hugo/scss/**/*.scss'),
|
|
|
127 |
'hugo',
|
|
|
128 |
],
|
|
|
129 |
tasks: ['componentlibrary:cssBuild'],
|
|
|
130 |
},
|
|
|
131 |
},
|
|
|
132 |
});
|
|
|
133 |
|
|
|
134 |
// Add the 'componentlibrary' task as a startup task.
|
|
|
135 |
grunt.moodleEnv.startupTasks.push('componentlibrary');
|
|
|
136 |
|
|
|
137 |
return {
|
|
|
138 |
docsBuild,
|
|
|
139 |
cssBuild,
|
|
|
140 |
indexBuild,
|
|
|
141 |
};
|
|
|
142 |
};
|