Files
KaTeX/update-sri.js
ylemkimon f829ebc692 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
2018-08-17 16:29:27 +09:00

44 lines
1.6 KiB
JavaScript

const fs = require("fs-extra");
const sriToolbox = require("sri-toolbox");
const version = process.argv[2];
Promise.all(process.argv.slice(3).map(file =>
fs.readFile(file, "utf8")
.then(body => {
// Replace size badge url
// 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/
// 2 - opening quote: "
// 3 - preserved suffix: /katex.min.js" integrity="…"
// 4 - file name: katex.min.js
// 5 - integrity opening quote: "
// 6 - old hash: sha384-…
// 7 - integrity hash algorithm: sha384
// 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};
}
return pre + version + post;
});
return Promise.all(Object.keys(hashes).map(hash =>
fs.readFile(hashes[hash].file, null)
.then(data => {
body = body.replace(hash, sriToolbox.generate({
algorithms: [hashes[hash].algo],
}, data));
})
)).then(() => fs.writeFile(file, body));
})
)).then(() => process.exit(0), err => {
// eslint-disable-next-line no-console
console.error(err.stack);
process.exit(1);
});