Skip to content

Commit

Permalink
Fuse the command boilerplate into the relative subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-claudia committed Sep 2, 2024
1 parent 053a9d7 commit 9cc39a8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 85 deletions.
37 changes: 0 additions & 37 deletions scripts/_command.js

This file was deleted.

80 changes: 41 additions & 39 deletions scripts/generate-docs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"use strict"

require("./_improve-rejection-crashing.js")

const {promises: fs} = require("fs")
const path = require("path")
const {promisify} = require("util")
Expand Down Expand Up @@ -38,7 +40,6 @@ const htmlMinifierConfig = {
useShortDoctype: true,
}

module.exports = generate
async function generate() {
return (await makeGenerator()).generate()
}
Expand Down Expand Up @@ -176,7 +177,7 @@ class Generator {

// insert parsed HTML
result = result.replace(/\[body\]/, markedHtml)

// insert meta description
result = result.replace(/\[metaDescription\]/, metaDescription)

Expand Down Expand Up @@ -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()
}
7 changes: 3 additions & 4 deletions scripts/minify-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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()
8 changes: 3 additions & 5 deletions scripts/update-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"], {
Expand All @@ -38,7 +39,4 @@ async function update() {
console.log("Published!")
}

/* eslint-disable global-require */
if (require.main === module) {
require("./_command")({exec: update})
}
update()

0 comments on commit 9cc39a8

Please sign in to comment.