From 9cc39a8aa76fadcb482e18aa377201ee6c4b1903 Mon Sep 17 00:00:00 2001 From: Claudia Meadows Date: Sun, 1 Sep 2024 06:35:08 -0700 Subject: [PATCH] Fuse the command boilerplate into the relative subcommands --- scripts/_command.js | 37 ------------------- scripts/generate-docs.js | 80 ++++++++++++++++++++-------------------- scripts/minify-stream.js | 7 ++-- scripts/update-docs.js | 8 ++-- 4 files changed, 47 insertions(+), 85 deletions(-) delete mode 100644 scripts/_command.js diff --git a/scripts/_command.js b/scripts/_command.js deleted file mode 100644 index 2ccdafe64..000000000 --- a/scripts/_command.js +++ /dev/null @@ -1,37 +0,0 @@ -"use strict" - -process.on("unhandledRejection", function (e) { - process.exitCode = 1 - if (!e.stdout || !e.stderr) throw e - console.error(e.stack) - - if (e.stdout && e.stdout.length) { - console.error(e.stdout.toString("utf-8")) - } - if (e.stderr && e.stderr.length) { - console.error(e.stderr.toString("utf-8")) - } - - // eslint-disable-next-line no-process-exit - process.exit() -}) - -module.exports = ({exec, watch}) => { - const index = process.argv.indexOf("--watch") - const useCache = process.argv.indexOf("--cache") >= 0 - if (index >= 0) { - process.argv.splice(index, 1) - - if (watch == null) { - console.error("Watching this script is not supported!") - // eslint-disable-next-line no-process-exit - process.exit(1) - } - - watch() - } else { - Promise.resolve(exec({useCache})).then((code) => { - if (code != null) process.exitCode = code - }) - } -} diff --git a/scripts/generate-docs.js b/scripts/generate-docs.js index df7d7c862..741605aa8 100644 --- a/scripts/generate-docs.js +++ b/scripts/generate-docs.js @@ -1,5 +1,7 @@ "use strict" +require("./_improve-rejection-crashing.js") + const {promises: fs} = require("fs") const path = require("path") const {promisify} = require("util") @@ -38,7 +40,6 @@ const htmlMinifierConfig = { useShortDoctype: true, } -module.exports = generate async function generate() { return (await makeGenerator()).generate() } @@ -176,7 +177,7 @@ class Generator { // insert parsed HTML result = result.replace(/\[body\]/, markedHtml) - + // insert meta description result = result.replace(/\[metaDescription\]/, metaDescription) @@ -261,45 +262,46 @@ class Generator { } } -/* eslint-disable global-require */ -if (require.main === module) { - require("./_command")({ - exec: generate, - async watch() { - let timeout, genPromise - function updateGenerator() { - if (timeout == null) return - clearTimeout(timeout) - genPromise = new Promise((resolve) => { - timeout = setTimeout(function() { - timeout = null - resolve(makeGenerator().then((g) => g.generate())) - }, 100) - }) - } +function watch() { + let timeout, genPromise + function updateGenerator() { + if (timeout == null) return + clearTimeout(timeout) + genPromise = new Promise((resolve) => { + timeout = setTimeout(function() { + timeout = null + resolve(makeGenerator().then((g) => g.generate())) + }, 100) + }) + } - async function updateFile(file) { - if ((/^layout\.html$|^archive$|^nav-/).test(file)) { - updateGenerator() - } - (await genPromise).generateSingle(file) - } + async function updateFile(file) { + if ((/^layout\.html$|^archive$|^nav-/).test(file)) { + updateGenerator() + } + (await genPromise).generateSingle(file) + } - async function removeFile(file) { - (await genPromise).eachTarget(file, (dest) => fs.unlink(dest)) - } + async function removeFile(file) { + (await genPromise).eachTarget(file, (dest) => fs.unlink(dest)) + } - require("chokidar").watch(r("docs"), { - ignored: ["archive/**", /(^|\\|\/)\../], - // This depends on `layout`/etc. existing first. - ignoreInitial: true, - awaitWriteFinish: true, - }) - .on("ready", updateGenerator) - .on("add", updateFile) - .on("change", updateFile) - .on("unlink", removeFile) - .on("unlinkDir", removeFile) - }, + // eslint-disable-next-line global-require + require("chokidar").watch(r("docs"), { + ignored: ["archive/**", /(^|\\|\/)\../], + // This depends on `layout`/etc. existing first. + ignoreInitial: true, + awaitWriteFinish: true, }) + .on("ready", updateGenerator) + .on("add", updateFile) + .on("change", updateFile) + .on("unlink", removeFile) + .on("unlinkDir", removeFile) +} + +if (process.argv.includes("--watch", 2)) { + watch() +} else { + generate() } diff --git a/scripts/minify-stream.js b/scripts/minify-stream.js index 8048e43a7..7a6a4c169 100644 --- a/scripts/minify-stream.js +++ b/scripts/minify-stream.js @@ -7,6 +7,8 @@ // - https://github.com/MithrilJS/mithril.js/issues/2417 // - https://github.com/MithrilJS/mithril.js/pull/2422 +require("./_improve-rejection-crashing.js") + const {promises: fs} = require("fs") const path = require("path") const zlib = require("zlib") @@ -33,7 +35,4 @@ async function minify() { console.log("Compiled size: " + format(compressedGzipSize) + " bytes gzipped (" + format(compressedSize) + " bytes uncompressed)") } -/* eslint-disable global-require */ -if (require.main === module) { - require("./_command")({exec: minify}) -} +minify() diff --git a/scripts/update-docs.js b/scripts/update-docs.js index 138432eb0..c5894eb62 100644 --- a/scripts/update-docs.js +++ b/scripts/update-docs.js @@ -7,13 +7,14 @@ // - https://github.com/MithrilJS/mithril.js/issues/2417 // - https://github.com/MithrilJS/mithril.js/pull/2422 +require("./_improve-rejection-crashing.js") + const path = require("path") const {execFileSync} = require("child_process") const ghPages = require("gh-pages") const upstream = require("./_upstream") const generate = require("./generate-docs") -module.exports = update async function update() { await generate() const commit = execFileSync("git", ["rev-parse", "--verify", "HEAD"], { @@ -38,7 +39,4 @@ async function update() { console.log("Published!") } -/* eslint-disable global-require */ -if (require.main === module) { - require("./_command")({exec: update}) -} +update()