nebulosa-css/build.js

77 lines
2.2 KiB
JavaScript

import sass from "sass"
import fs from "fs"
import { log } from "console"
import YAML from "yaml"
import parseTemplate from "./parseTemplate.js"
export default function buildNebulosa(predictableOutputName) {
log("Generating Nebulosa CSS...")
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())
log("Reading templates...")
const templateFiles = fs.readdirSync("./src/templates")
const templates = templateFiles.map(
fileName => {
return {
fileName,
content: fs.readFileSync(`./src/templates/${fileName}`).toString()
}
}
)
log("Executing templates...")
templates.forEach(
template => {
const compiledTemplate = parseTemplate(template.content, config)
fs.writeFileSync(`./src/${template.fileName}`, compiledTemplate)
}
)
try {
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 = predictableOutputName ? "./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 ${outFileName}`)
} catch (err) {
console.error("Encountered an error while building Nebulosa:")
console.error(err)
}
}
// If the file is run directly from the commandline, build Nebulosa.
if (import.meta.url === `file://${process.argv[1]}`) {
log("Building Nebulosa...")
try {
buildNebulosa(false)
} catch (err) {
console.error(err)
process.exit(1)
}
}