shadowfacts.net/lib/generate/css.ts

41 lines
1.1 KiB
TypeScript
Raw Normal View History

2019-01-04 18:14:53 +00:00
import { promises as fs } from "fs";
import * as util from "../util";
import sass, { Result as SassResult } from "node-sass";
import ejs from "ejs";
function renderSass(data: string): Promise<SassResult> {
2019-01-04 18:14:53 +00:00
return new Promise((resolve, reject) => {
sass.render({
data: data,
2019-09-17 21:02:31 +00:00
outputStyle: process.env.NODE_ENV === "development" ? "expanded" : "compressed"
2019-01-04 18:14:53 +00:00
}, (error, result) => {
if (error) reject(error);
else resolve(result);
});
});
}
async function generate(theme: string) {
const filename = `site/css/${theme}.scss`;
let sass = (await fs.readFile(filename)).toString();
2019-01-05 02:59:52 +00:00
sass = util.render(sass, {}, filename);
2019-06-28 19:51:26 +00:00
let css = (await renderSass(sass)).css.toString();
css += "\n";
let mainSass = (await fs.readFile("site/css/main.scss")).toString();
mainSass = util.render(mainSass, {}, "site/css/main.scss");
css += (await renderSass(mainSass)).css.toString();
2019-01-04 18:14:53 +00:00
2019-06-28 19:51:26 +00:00
util.write(`css/${theme}.css`, css);
2019-01-04 18:14:53 +00:00
}
export default async function css() {
await generate("light");
await generate("dark");
2019-06-28 19:51:26 +00:00
await generate("auto");
2019-06-28 19:28:00 +00:00
if (process.env.NODE_ENV === "development") {
require("fs").watch("site/css/", css);
}
}