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 |
* @copyright 2021 Andrew Nicols
|
|
|
20 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
21 |
*/
|
|
|
22 |
|
|
|
23 |
module.exports = grunt => {
|
|
|
24 |
|
|
|
25 |
const getCssConfigForFiles = files => {
|
|
|
26 |
return {
|
|
|
27 |
stylelint: {
|
|
|
28 |
css: {
|
|
|
29 |
// Use a fully-qualified path.
|
|
|
30 |
src: files,
|
|
|
31 |
options: {
|
|
|
32 |
quietDeprecationWarnings: true,
|
|
|
33 |
configOverrides: {
|
|
|
34 |
rules: {
|
|
|
35 |
// These rules have to be disabled in .stylelintrc for scss compat.
|
|
|
36 |
"at-rule-no-unknown": true,
|
|
|
37 |
}
|
|
|
38 |
}
|
|
|
39 |
}
|
|
|
40 |
},
|
|
|
41 |
},
|
|
|
42 |
};
|
|
|
43 |
};
|
|
|
44 |
|
|
|
45 |
const getScssConfigForFiles = files => {
|
|
|
46 |
return {
|
|
|
47 |
stylelint: {
|
|
|
48 |
scss: {
|
|
|
49 |
options: {
|
|
|
50 |
quietDeprecationWarnings: true,
|
|
|
51 |
customSyntax: 'postcss-scss',
|
|
|
52 |
},
|
|
|
53 |
src: files,
|
|
|
54 |
},
|
|
|
55 |
},
|
|
|
56 |
};
|
|
|
57 |
};
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
* Register any stylelint tasks.
|
|
|
61 |
*
|
|
|
62 |
* @param {Object} grunt
|
|
|
63 |
* @param {Array} files
|
|
|
64 |
* @param {String} fullRunDir
|
|
|
65 |
*/
|
|
|
66 |
const registerStyleLintTasks = () => {
|
|
|
67 |
const glob = require('glob');
|
|
|
68 |
|
|
|
69 |
// The stylelinters do not handle the case where a configuration was provided but no files were included.
|
|
|
70 |
// Keep track of whether any files were found.
|
|
|
71 |
let hasCss = false;
|
|
|
72 |
let hasScss = false;
|
|
|
73 |
|
|
|
74 |
// The stylelint processors do not take a path argument. They always check all provided values.
|
|
|
75 |
// As a result we must check through each glob and determine if any files match the current directory.
|
|
|
76 |
const scssFiles = [];
|
|
|
77 |
const cssFiles = [];
|
|
|
78 |
|
|
|
79 |
const requestedFiles = grunt.moodleEnv.files;
|
|
|
80 |
if (requestedFiles) {
|
|
|
81 |
// Grunt was called with a files argument.
|
|
|
82 |
// Check whether each of the requested files matches either the CSS or SCSS source file list.
|
|
|
83 |
|
|
|
84 |
requestedFiles.forEach(changedFilePath => {
|
|
|
85 |
let matchesGlob;
|
|
|
86 |
|
|
|
87 |
// Check whether this watched path matches any watched SCSS file.
|
|
|
88 |
matchesGlob = grunt.moodleEnv.scssSrc.some(watchedPathGlob => {
|
|
|
89 |
return glob.sync(watchedPathGlob).indexOf(changedFilePath) !== -1;
|
|
|
90 |
});
|
|
|
91 |
if (matchesGlob) {
|
|
|
92 |
scssFiles.push(changedFilePath);
|
|
|
93 |
hasScss = true;
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
// Check whether this watched path matches any watched CSS file.
|
|
|
97 |
matchesGlob = grunt.moodleEnv.cssSrc.some(watchedPathGlob => {
|
|
|
98 |
return glob.sync(watchedPathGlob).indexOf(changedFilePath) !== -1;
|
|
|
99 |
});
|
|
|
100 |
if (matchesGlob) {
|
|
|
101 |
cssFiles.push(changedFilePath);
|
|
|
102 |
hasCss = true;
|
|
|
103 |
}
|
|
|
104 |
});
|
|
|
105 |
} else {
|
|
|
106 |
// Grunt was called without a list of files.
|
|
|
107 |
// The start directory (runDir) may be a child dir of the project.
|
|
|
108 |
// Check each scssSrc file to see if it's in the start directory.
|
|
|
109 |
// This means that we can lint just mod/*/styles.css if started in the mod directory.
|
|
|
110 |
|
|
|
111 |
grunt.moodleEnv.scssSrc.forEach(path => {
|
|
|
112 |
if (path.startsWith(grunt.moodleEnv.runDir)) {
|
|
|
113 |
scssFiles.push(path);
|
|
|
114 |
hasScss = true;
|
|
|
115 |
}
|
|
|
116 |
});
|
|
|
117 |
|
|
|
118 |
grunt.moodleEnv.cssSrc.forEach(path => {
|
|
|
119 |
if (path.startsWith(grunt.moodleEnv.runDir)) {
|
|
|
120 |
cssFiles.push(path);
|
|
|
121 |
hasCss = true;
|
|
|
122 |
}
|
|
|
123 |
});
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
// Register the tasks.
|
|
|
127 |
const scssTasks = ['sass'];
|
|
|
128 |
if (hasScss) {
|
|
|
129 |
grunt.config.merge(getScssConfigForFiles(scssFiles));
|
|
|
130 |
scssTasks.unshift('stylelint:scss');
|
|
|
131 |
}
|
|
|
132 |
scssTasks.unshift('ignorefiles');
|
|
|
133 |
|
|
|
134 |
const cssTasks = ['ignorefiles'];
|
|
|
135 |
if (hasCss) {
|
|
|
136 |
grunt.config.merge(getCssConfigForFiles(cssFiles));
|
|
|
137 |
cssTasks.push('stylelint:css');
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
// The tasks must be registered, even if empty to ensure a consistent command list.
|
|
|
141 |
// They jsut won't run anything.
|
|
|
142 |
grunt.registerTask('scss', scssTasks);
|
|
|
143 |
grunt.registerTask('rawcss', cssTasks);
|
|
|
144 |
};
|
|
|
145 |
|
|
|
146 |
// Register CSS tasks.
|
|
|
147 |
grunt.loadNpmTasks('grunt-stylelint');
|
|
|
148 |
|
|
|
149 |
// Register the style lint tasks.
|
|
|
150 |
registerStyleLintTasks();
|
|
|
151 |
grunt.registerTask('css', ['scss', 'rawcss']);
|
|
|
152 |
|
|
|
153 |
const getCoreThemeMatches = () => {
|
|
|
154 |
const scssMatch = 'scss/**/*.scss';
|
|
|
155 |
|
|
|
156 |
if (grunt.moodleEnv.inTheme) {
|
|
|
157 |
return [scssMatch];
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
if (grunt.moodleEnv.runDir.startsWith('theme')) {
|
|
|
161 |
return [`*/${scssMatch}`];
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
return [`theme/*/${scssMatch}`];
|
|
|
165 |
};
|
|
|
166 |
|
|
|
167 |
// Add the watch configuration for rawcss, and scss.
|
|
|
168 |
grunt.config.merge({
|
|
|
169 |
watch: {
|
|
|
170 |
rawcss: {
|
|
|
171 |
files: [
|
|
|
172 |
'**/*.css',
|
|
|
173 |
],
|
|
|
174 |
excludes: [
|
|
|
175 |
'**/moodle.css',
|
|
|
176 |
'**/editor.css',
|
|
|
177 |
'jsdoc/styles/*.css',
|
|
|
178 |
],
|
|
|
179 |
tasks: ['rawcss']
|
|
|
180 |
},
|
|
|
181 |
scss: {
|
|
|
182 |
files: getCoreThemeMatches(),
|
|
|
183 |
tasks: ['scss']
|
|
|
184 |
},
|
|
|
185 |
},
|
|
|
186 |
});
|
|
|
187 |
};
|