Fuse the command boilerplate into the relative subcommands

This commit is contained in:
Claudia Meadows 2024-09-01 06:35:08 -07:00
parent 0d095d1373
commit 5af0693fea
No known key found for this signature in database
GPG key ID: C86B594396786760
4 changed files with 47 additions and 85 deletions

View file

@ -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
})
}
}

View file

@ -1,5 +1,7 @@
"use strict" "use strict"
require("./_improve-rejection-crashing.js")
const {promises: fs} = require("fs") const {promises: fs} = require("fs")
const path = require("path") const path = require("path")
const {promisify} = require("util") const {promisify} = require("util")
@ -38,7 +40,6 @@ const htmlMinifierConfig = {
useShortDoctype: true, useShortDoctype: true,
} }
module.exports = generate
async function generate() { async function generate() {
return (await makeGenerator()).generate() return (await makeGenerator()).generate()
} }
@ -261,11 +262,7 @@ class Generator {
} }
} }
/* eslint-disable global-require */ function watch() {
if (require.main === module) {
require("./_command")({
exec: generate,
async watch() {
let timeout, genPromise let timeout, genPromise
function updateGenerator() { function updateGenerator() {
if (timeout == null) return if (timeout == null) return
@ -289,6 +286,7 @@ if (require.main === module) {
(await genPromise).eachTarget(file, (dest) => fs.unlink(dest)) (await genPromise).eachTarget(file, (dest) => fs.unlink(dest))
} }
// eslint-disable-next-line global-require
require("chokidar").watch(r("docs"), { require("chokidar").watch(r("docs"), {
ignored: ["archive/**", /(^|\\|\/)\../], ignored: ["archive/**", /(^|\\|\/)\../],
// This depends on `layout`/etc. existing first. // This depends on `layout`/etc. existing first.
@ -300,6 +298,10 @@ if (require.main === module) {
.on("change", updateFile) .on("change", updateFile)
.on("unlink", removeFile) .on("unlink", removeFile)
.on("unlinkDir", removeFile) .on("unlinkDir", removeFile)
}, }
})
if (process.argv.includes("--watch", 2)) {
watch()
} else {
generate()
} }

View file

@ -7,6 +7,8 @@
// - https://github.com/MithrilJS/mithril.js/issues/2417 // - https://github.com/MithrilJS/mithril.js/issues/2417
// - https://github.com/MithrilJS/mithril.js/pull/2422 // - https://github.com/MithrilJS/mithril.js/pull/2422
require("./_improve-rejection-crashing.js")
const {promises: fs} = require("fs") const {promises: fs} = require("fs")
const path = require("path") const path = require("path")
const zlib = require("zlib") const zlib = require("zlib")
@ -33,7 +35,4 @@ async function minify() {
console.log("Compiled size: " + format(compressedGzipSize) + " bytes gzipped (" + format(compressedSize) + " bytes uncompressed)") console.log("Compiled size: " + format(compressedGzipSize) + " bytes gzipped (" + format(compressedSize) + " bytes uncompressed)")
} }
/* eslint-disable global-require */ minify()
if (require.main === module) {
require("./_command")({exec: minify})
}

View file

@ -7,13 +7,14 @@
// - https://github.com/MithrilJS/mithril.js/issues/2417 // - https://github.com/MithrilJS/mithril.js/issues/2417
// - https://github.com/MithrilJS/mithril.js/pull/2422 // - https://github.com/MithrilJS/mithril.js/pull/2422
require("./_improve-rejection-crashing.js")
const path = require("path") const path = require("path")
const {execFileSync} = require("child_process") const {execFileSync} = require("child_process")
const ghPages = require("gh-pages") const ghPages = require("gh-pages")
const upstream = require("./_upstream") const upstream = require("./_upstream")
const generate = require("./generate-docs") const generate = require("./generate-docs")
module.exports = update
async function update() { async function update() {
await generate() await generate()
const commit = execFileSync("git", ["rev-parse", "--verify", "HEAD"], { const commit = execFileSync("git", ["rev-parse", "--verify", "HEAD"], {
@ -38,7 +39,4 @@ async function update() {
console.log("Published!") console.log("Published!")
} }
/* eslint-disable global-require */ update()
if (require.main === module) {
require("./_command")({exec: update})
}