Improve prestart and tests (#1610)

* screenshotter: replace promisify(fs) and mkdirp with fs-extra

* screenshotter: replace selenium.promise with builtin Promise

* Lint all JavaScript files in the root

* unicodeMake: replace console.log with writeFile(string)

* unicodeMake: check timestamp, do not build if up-to-date

* Replace check-dependencies with yarn check

* Exclude src/unicodeMake.js from coverage

* Add (missing) dependencies

* Skip CircleCI if only unrelated has been changed

* Fix commit range

* Use fs-extra in update-sri
This commit is contained in:
ylemkimon
2018-08-17 16:29:27 +09:00
committed by GitHub
parent e8f2ecdd39
commit f829ebc692
7 changed files with 120 additions and 197 deletions

View File

@@ -1,31 +1,14 @@
const fs = require("fs");
const path = require("path");
const fs = require("fs-extra");
const sriToolbox = require("sri-toolbox");
const version = process.argv[2];
function read(file, encoding) {
return new Promise((resolve, reject) =>
fs.readFile(file, encoding, (err, body) =>
err ? reject(err) : resolve(body)));
}
function write(file, data) {
return new Promise((resolve, reject) =>
fs.writeFile(file, data, (err) =>
err ? reject(err) : resolve()));
}
Promise.all(process.argv.slice(3).map(file =>
read(file, "utf8")
fs.readFile(file, "utf8")
.then(body => {
// Replace size badge url
// 1 - url prefix: https://img.badgesize.io/Khan/KaTeX/
// 2 - url suffix: /dist/katex.min.js?compression=gzip
const badgeRe = /(https:\/\/img\.badgesize\.io\/Khan\/KaTeX\/v)(?:.+)(\/dist\/katex\.min\.js\?compression=gzip)/g;
body = body.replace(badgeRe, (m, pre, post) => {
return pre + version + post;
});
// eslint-disable-next-line max-len
body = body.replace(/(https:\/\/img\.badgesize\.io\/Khan\/KaTeX\/v)(?:.+)(\/dist\/katex\.min\.js\?compression=gzip)/g, `$1${version}$2`);
// Replace CDN urls
// 1 - url prefix: "http…/KaTeX/
@@ -35,24 +18,26 @@ Promise.all(process.argv.slice(3).map(file =>
// 5 - integrity opening quote: "
// 6 - old hash: sha384-…
// 7 - integrity hash algorithm: sha384
const cdnRe = /((["'])https?:\/\/cdn\.jsdelivr\.net\/npm\/katex@)[^\/"']+(\/([^"']+)\2(?:\s+integrity=(["'])(([^-]+)-[^"']+)\5)?)/g;
// eslint-disable-next-line max-len
const cdnRe = /((["'])https?:\/\/cdn\.jsdelivr\.net\/npm\/katex@)[^/"']+(\/([^"']+)\2(?:\s+integrity=(["'])(([^-]+)-[^"']+)\5)?)/g;
const hashes = {};
body = body.replace(cdnRe, (m, pre, oq1, post, file, oq2, old, algo) => {
if (old) {
hashes[old] = { file, algo };
hashes[old] = {file, algo};
}
return pre + version + post;
});
return Promise.all(Object.keys(hashes).map(hash =>
read(hashes[hash].file, null)
fs.readFile(hashes[hash].file, null)
.then(data => {
body = body.replace(hash, sriToolbox.generate({
algorithms: [hashes[hash].algo],
}, data));
})
)).then(() => write(file, body));
)).then(() => fs.writeFile(file, body));
})
)).then(() => process.exit(0), err => {
// eslint-disable-next-line no-console
console.error(err.stack);
process.exit(1);
});