gulp: Use `async` functions & drop testing on Node v6 (#1783) This refactors the `gulpfile.js` to use async/await for clearer code. It also drops testing for all NodeJS versions before NodeJS v8 because they don't support async/await.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
diff --git a/.travis.yml b/.travis.yml
index 47adfc5..8dbeab5 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,6 +1,5 @@
language: node_js
node_js:
-- '6'
- '8'
- '10'
- 'node'
diff --git a/gulpfile.js b/gulpfile.js
index 0c6f8a2..9c09a76 100644
--- a/gulpfile.js
+++ b/gulpfile.js
@@ -6,6 +6,7 @@ const header = require('gulp-header');
const concat = require('gulp-concat');
const replace = require('gulp-replace');
const pump = require('pump');
+const util = require('util');
const fs = require('fs');
const simpleGit = require('simple-git');
const shelljs = require('shelljs');
@@ -79,12 +80,11 @@ function build(cb) {
`), concat('prism.js'), dest('./')], cb);
}
-function componentsJsonToJs(cb) {
- componentsPromise.then(data => {
- const js = `var components = ${JSON.stringify(data)};
+async function componentsJsonToJs() {
+ const data = await componentsPromise;
+ const js = `var components = ${JSON.stringify(data)};
if (typeof module !== 'undefined' && module.exports) { module.exports = components; }`;
- fs.writeFile(paths.componentsFileJS, js, cb);
- });
+ return util.promisify(fs.writeFile)(paths.componentsFileJS, js);
}
function watchComponentsAndPlugins() {
@@ -92,102 +92,105 @@ function watchComponentsAndPlugins() {
watch(paths.plugins, parallel(minifyPlugins, build));
}
-function languagePlugins(cb) {
- componentsPromise.then(data => {
- const languagesMap = {};
- const dependenciesMap = {};
- const aliasMap = {};
-
- /**
- * Tries to guess the name of a language given its id.
- *
- * From `prism-show-language.js`.
- *
- * @param {string} id The language id.
- * @returns {string}
- */
- function guessTitle(id) {
- if (!id) {
- return id;
- }
- return (id.substring(0, 1).toUpperCase() + id.substring(1)).replace(/s(?=cript)/, 'S');
+async function languagePlugins() {
+ const data = await componentsPromise;
+ const languagesMap = {};
+ const dependenciesMap = {};
+ const aliasMap = {};
+
+ /**
+ * Tries to guess the name of a language given its id.
+ *
+ * From `prism-show-language.js`.
+ *
+ * @param {string} id The language id.
+ * @returns {string}
+ */
+ function guessTitle(id) {
+ if (!id) {
+ return id;
}
-
- function addLanguageTitle(key, title) {
- if (!languagesMap[key] && guessTitle(key) !== title) {
- languagesMap[key] = title;
- }
+ return (id.substring(0, 1).toUpperCase() + id.substring(1)).replace(/s(?=cript)/, 'S');
+ }
+
+ /**
+ * @param {string} key
+ * @param {string} title
+ */
+ function addLanguageTitle(key, title) {
+ if (!languagesMap[key] && guessTitle(key) !== title) {
+ languagesMap[key] = title;
}
+ }
- for (const id in data.languages) {
- if (id !== 'meta') {
- const language = data.languages[id];
- const title = language.displayTitle || language.title;
+ for (const id in data.languages) {
+ if (id !== 'meta') {
+ const language = data.languages[id];
+ const title = language.displayTitle || language.title;
- addLanguageTitle(id, title);
+ addLanguageTitle(id, title);
- for (const name in language.aliasTitles) {
- addLanguageTitle(name, language.aliasTitles[name]);
- }
+ for (const name in language.aliasTitles) {
+ addLanguageTitle(name, language.aliasTitles[name]);
+ }
- if (language.alias) {
- if (typeof language.alias === 'string') {
- aliasMap[language.alias] = id;
- addLanguageTitle(language.alias, title);
- } else {
- language.alias.forEach(function (alias) {
- aliasMap[alias] = id;
- addLanguageTitle(alias, title);
- });
- }
+ if (language.alias) {
+ if (typeof language.alias === 'string') {
+ aliasMap[language.alias] = id;
+ addLanguageTitle(language.alias, title);
+ } else {
+ language.alias.forEach(function (alias) {
+ aliasMap[alias] = id;
+ addLanguageTitle(alias, title);
+ });
}
+ }
- if (language.require) {
- dependenciesMap[id] = language.require;
- }
+ if (language.require) {
+ dependenciesMap[id] = language.require;
}
}
-
- function formattedStringify(json) {
- return JSON.stringify(json, null, '\t').replace(/\n/g, '\n\t');
+ }
+
+ function formattedStringify(json) {
+ return JSON.stringify(json, null, '\t').replace(/\n/g, '\n\t');
+ }
+
+ const jsonLanguagesMap = formattedStringify(languagesMap);
+ const jsonDependenciesMap = formattedStringify(dependenciesMap);
+ const jsonAliasMap = formattedStringify(aliasMap);
+
+ const tasks = [
+ {
+ plugin: paths.showLanguagePlugin,
+ maps: { languages: jsonLanguagesMap}
+ },
+ {
+ plugin: paths.autoloaderPlugin,
+ maps: { aliases: jsonAliasMap, dependencies: jsonDependenciesMap }
}
+ ];
- const jsonLanguagesMap = formattedStringify(languagesMap);
- const jsonDependenciesMap = formattedStringify(dependenciesMap);
- const jsonAliasMap = formattedStringify(aliasMap);
-
- const tasks = [
- {
- plugin: paths.showLanguagePlugin,
- maps: { languages: jsonLanguagesMap}
- },
- {
- plugin: paths.autoloaderPlugin,
- maps: { aliases: jsonAliasMap, dependencies: jsonDependenciesMap }
- }
- ];
-
- let cpt = 0;
- const l = tasks.length;
- const done = () => {
- cpt++;
- if (cpt === l) {
- cb && cb();
- }
- };
-
- for (const task of tasks) {
- const stream = src(task.plugin)
- .pipe(replace(
- /\/\*(\w+)_placeholder\[\*\/[\s\S]*?\/\*\]\*\//g,
- (m, mapName) => `/*${mapName}_placeholder[*/${task.maps[mapName]}/*]*/`
- ))
- .pipe(dest(task.plugin.substring(0, task.plugin.lastIndexOf('/'))));
-
- stream.on('error', done);
- stream.on('end', done);
- }
- });
+ // TODO: Use `Promise.allSettled` (https://github.com/tc39/proposal-promise-allSettled)
+ const taskResults = await Promise.all(tasks.map(task => new Promise((resolve, reject) => {
+ const stream = src(task.plugin)
+ .pipe(replace(
+ /\/\*(\w+)_placeholder\[\*\/[\s\S]*?\/\*\]\*\//g,
+ (m, mapName) => `/*${mapName}_placeholder[*/${task.maps[mapName]}/*]*/`
+ ))
+ .pipe(dest(task.plugin.substring(0, task.plugin.lastIndexOf('/'))));
+
+ stream.on('error', reject);
+ stream.on('end', resolve);
+ }).then(
+ /** @type {<T>(value: T) => {status: 'fulfilled', value: T}} */ value => ({status: 'fulfilled', value}),
+ /** @type {<T>(error: T) => {status: 'rejected', reason: T}} */ error => ({status: 'rejected', reason: error}),
+ )));
+
+ const rejectedTasks = taskResults.filter(/** @return {r is {status: 'rejected', reason: any}} */ r => r.status === 'rejected');
+ if (rejectedTasks.length > 0) {
+ throw rejectedTasks.map(r => r.reason);
+ }
}
const ISSUE_RE = /#(\d+)(?![\d\]])/g;