Skip to content

Instantly share code, notes, and snippets.

@jrc03c
Last active October 7, 2023 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrc03c/70b1aea9df095c3776d9cc10ac2c9c68 to your computer and use it in GitHub Desktop.
Save jrc03c/70b1aea9df095c3776d9cc10ac2c9c68 to your computer and use it in GitHub Desktop.
an updated default build script with more bells & whistles
const { execSync } = require("node:child_process")
const { fg, fx } = require("@jrc03c/bash-colors")
const { indent, unindent, wrap } = require("@jrc03c/js-text-tools")
const express = require("express")
const fs = require("node:fs")
const path = require("node:path")
const process = require("node:process")
const watch = require("@jrc03c/watch")
const OUT_DIR = "_site"
let isRebuilding = false
function rebuild(basePath) {
isRebuilding = true
console.log("-----\n")
console.log(`Rebuilding... (${new Date().toLocaleString()})`)
console.log("Base path:", basePath)
const fullPath = path.join(OUT_DIR, basePath, "/")
try {
if (fs.existsSync(fullPath)) {
fs.rmSync(fullPath, { recursive: true, force: true })
}
fs.mkdirSync(fullPath, { recursive: true })
execSync(
`npx esbuild res/js/src/main.js --bundle --outfile=res/js/bundle.js`,
{ encoding: "utf8" },
)
execSync(`cp index.html "${fullPath}"`, { encoding: "utf8" })
execSync(`cp -r res "${fullPath}"`, { encoding: "utf8" })
console.log("\nDone! 🎉\n")
} catch (e) {
console.error(e)
}
isRebuilding = false
}
const shouldHelp =
process.argv.indexOf("--help") > -1 ||
process.argv.indexOf("help") > -1 ||
process.argv.indexOf("-h") > -1
if (shouldHelp) {
const message = wrap(
indent(
unindent(`
${fx.underscore(fx.bright("Options:"))}
${fg.yellow(
"--base-path, -b",
)} = the path at which the app should be served
${fg.yellow("--help, -h")} = show this message again
${fg.yellow("--watch, -w")} = watch for changes
Note that watching for changes also launches a web server to serve the files, so you don't need to start one manually.
`),
" ",
),
)
console.log(message)
process.exit()
}
const shouldWatch =
process.argv.indexOf("-w") > -1 || process.argv.indexOf("--watch") > -1
const basePath = (() => {
const longIndex = process.argv.findIndex(a => a.includes("--base-path"))
const shortIndex = process.argv.indexOf("-b")
let out = ""
if (longIndex > -1) {
out = process.argv[longIndex].split("--base-path=").at(-1).trim()
} else if (shortIndex > -1) {
out = process.argv[shortIndex + 1].trim()
}
return path.join("/", out, "/")
})()
if (shouldWatch) {
watch({
target: ".",
exclude: ["build.js", "bundle.js", "node_modules", OUT_DIR],
created: () => rebuild(basePath),
modified: () => rebuild(basePath),
deleted: () => rebuild(basePath),
})
const server = express()
const statick = express.static(path.join(OUT_DIR, basePath), {
extensions: ["html"],
})
server.use(basePath, (request, response, next) => {
const interval = setInterval(() => {
if (isRebuilding) return
clearInterval(interval)
return statick(request, response, next)
}, 10)
})
server.listen(3000, () => {
const url = `http://localhost:3000${basePath}`
console.log("-----")
console.log(`Listening at ${url}...`)
console.log("-----\n")
execSync(`xdg-open ${url}`, { encoding: "utf8" })
})
}
rebuild(basePath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment