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"
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()
}

View file

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

View file

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