nebulosa-css/dev.js
2023-08-04 19:52:57 +02:00

57 lines
1.4 KiB
JavaScript

import chokidar from "chokidar"
import express from "express"
import fs from "fs"
import njk from "nunjucks"
import buildNebulosa from "./build.js"
const server = express()
njk.configure(
"./web",
{
autoescape: true,
express: server,
lstripBlocks: true,
trimBlocks: true,
watch: true
}
)
// Do an initial build of Nebulosa, then watch for changes...
buildNebulosa(true)
chokidar
.watch(["./src", "./config.yml", "./example-config.yml"])
.on(
"change",
() => {
console.log("Detected changes in SCSS or config! Recompiling...")
try {
buildNebulosa(true)
console.log("Saved!")
} catch (error) {
console.log("Error while compiling!")
console.error(error)
}
}
)
server.use("/assets", express.static("./out"))
server.get(
"*",
(req, res) => {
const path = req.path
.replace(/^\/|\.\.+|\/$/g, "")
if (fs.existsSync(`web/pages/${path}.njk`))
return res.render(`pages/${path}.njk`)
if (fs.existsSync(`web/pages/${path}/index.njk`))
return res.render(`pages/${path}/index.njk`)
res.status(404).send("Not Found")
}
)
server.listen(12345, () => console.log("Dev server started and listening on port 12345!"))