nebulosa-css/build.js

77 lines
2.2 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-05 01:59:08 +02:00
export default function buildNebulosa(predictableOutputName) {
2023-08-04 19:51:08 +02:00
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)
}
)
2023-08-05 01:59:08 +02:00
try {
log("Compiling SCSS...")
const { css } = sass.compile(
"./src/main.scss",
{ style: "compressed" }
)
2023-08-04 19:51:08 +02:00
2023-08-05 01:59:08 +02:00
const now = new Date()
.toISOString()
.replace(/\.\S+/g, "")
.replace(/:/g, ".")
.replace(/T/g, "_")
2023-08-04 19:51:08 +02:00
2023-08-05 01:59:08 +02:00
const outFileName = predictableOutputName ? "./out/main.css" : `./out/${now}.css`
2023-08-04 19:51:08 +02:00
2023-08-05 01:59:08 +02:00
if (!fs.existsSync("./out"))
fs.mkdirSync("./out")
2023-08-04 19:51:08 +02:00
2023-08-05 01:59:08 +02:00
log("Writing file...")
fs.writeFileSync(
outFileName,
"/* Generated using Nebulosa CSS. https://git.qwik.space/BurnyLlama/nebulosa-css */\n" + css
)
2023-08-04 19:51:08 +02:00
2023-08-05 01:59:08 +02:00
log(`Saved config file to ${outFileName}`)
} catch (err) {
console.error("Encountered an error while building Nebulosa:")
console.error(err)
}
2023-08-04 19:51:08 +02:00
}
// If the file is run directly from the commandline, build Nebulosa.
2023-08-04 20:14:57 +02:00
if (import.meta.url === `file://${process.argv[1]}`) {
log("Building Nebulosa...")
2023-08-05 01:59:08 +02:00
try {
buildNebulosa(false)
} catch (err) {
console.error(err)
process.exit(1)
}
2023-08-04 19:51:08 +02:00
}