nebulosa-css/build.js

66 lines
1.8 KiB
JavaScript
Raw Normal View History

2023-03-01 22:48:48 +01:00
import sass from "sass"
import fs from "fs"
2023-08-04 19:51:08 +02:00
import { log } from "console"
import YAML from "yaml"
import parseTemplate from "./parseTemplate.js"
2023-03-01 22:48:48 +01:00
2023-08-04 19:51:08 +02:00
export default function buildNebulosa(debug) {
log("Generating Nebulosa CSS...")
2023-03-01 22:48:48 +01:00
2023-08-04 19:51:08 +02:00
log("Reading config...")
if (!fs.existsSync("./config.yml")) {
log("No config file found!")
log("Copying 'example-config.yml' to 'config.yml'...")
fs.copyFileSync("./example-config.yml", "config.yml")
}
const config = YAML.parse(fs.readFileSync("./config.yml").toString())
2023-03-01 22:48:48 +01:00
2023-08-04 19:51:08 +02:00
log("Reading templates...")
const templateFiles = fs.readdirSync("./src/templates")
const templates = templateFiles.map(
fileName => {
return {
fileName,
content: fs.readFileSync(`./src/templates/${fileName}`).toString()
}
}
)
2023-03-01 22:48:48 +01:00
2023-08-04 19:51:08 +02:00
log("Executing templates...")
templates.forEach(
template => {
const compiledTemplate = parseTemplate(template.content, config)
fs.writeFileSync(`./src/${template.fileName}`, compiledTemplate)
}
)
log("Compiling SCSS...")
const { css } = sass.compile(
"./src/main.scss",
{ style: "compressed" }
)
const now = new Date()
.toISOString()
.replace(/\.\S+/g, "")
.replace(/:/g, ".")
.replace(/T/g, "_")
const outFileName = debug ? "./out/main.css" : `./out/${now}.css`
if (!fs.existsSync("./out"))
fs.mkdirSync("./out")
log("Writing file...")
fs.writeFileSync(
outFileName,
"/* Generated using Nebulosa CSS. https://git.qwik.space/BurnyLlama/nebulosa-css */\n" + css
)
log(`Saved config file to ./out/${now}.css`)
}
// If the file is run directly from the commandline, build Nebulosa.
if (import.meta.url === `file://${process.argv[0]}`) {
buildNebulosa(false)
}