nebulosa-css/dev.js

60 lines
1.4 KiB
JavaScript
Raw Permalink Normal View History

2023-03-01 23:27:34 +01:00
import chokidar from "chokidar"
import express from "express"
import fs from "fs"
import njk from "nunjucks"
2023-08-04 19:51:08 +02:00
import buildNebulosa from "./build.js"
2023-03-01 23:27:34 +01:00
const server = express()
njk.configure(
"./web",
{
autoescape: true,
express: server,
lstripBlocks: true,
trimBlocks: true,
watch: true
}
)
2023-08-04 19:51:08 +02:00
// Do an initial build of Nebulosa, then watch for changes...
buildNebulosa(true)
chokidar
2023-08-05 01:59:08 +02:00
.watch(
["./src", "./config.yml", "./example-config.yml"],
{ ignored: ["./src/main.scss", "./src/_settings.scss"] }
)
2023-08-04 19:51:08 +02:00
.on(
"change",
() => {
console.log("Detected changes in SCSS or config! Recompiling...")
try {
buildNebulosa(true)
console.log("Saved!")
2023-08-05 01:59:08 +02:00
} catch (err) {
2023-08-04 19:51:08 +02:00
console.log("Error while compiling!")
2023-08-05 01:59:08 +02:00
console.error(err)
2023-03-06 10:20:47 +01:00
}
2023-08-04 19:51:08 +02:00
}
)
2023-03-01 23:27:34 +01:00
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!"))