summaryrefslogtreecommitdiff
path: root/web/gotosocial-styling/index.js
blob: 88d9398b2d4b10dce5f0fc00821dd2cb99890452 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"use strict";

const Promise = require("bluebird");
const fs = require("fs").promises;
const postcss = require('postcss');
const {parse} = require("postcss-scss");

/*
	Bundle all postCSS files under the `templates/` directory separately, each prepended with the (variable) contents of ./colors.css
	Outputs in plain CSS are in `build/`, split by template
*/

const postcssPlugins = ["postcss-strip-inline-comments", "postcss-nested", "postcss-simple-vars", "postcss-color-function"].map((plugin) => require(plugin)());

function getTemplates() {
	return fs.readdir(`${__dirname}/templates`).then((templates) => {
		return templates.map((a) => {
			return [a, `${__dirname}/templates/${a}`];
		});
	});
}

getTemplates();

function bundle([template, path]) {
	return Promise.try(() => {
		return Promise.all([
			fs.readFile(`${__dirname}/colors.css`, "utf-8"),
			fs.readFile(path, "utf-8")
		]);
	}).then(([colors, style]) => {
		return parse(colors + "\n" + style);
	}).then((ast) => {
		return postcss(postcssPlugins).process(ast, {
			from: template,
			to: template
		});
	}).then((bundle) => {
		return fs.writeFile(`${buildDir}/${template}`, bundle.css);
	}).then(() => {
		console.log(`Finished writing CSS to ${buildDir}/${template}`);
	});
}

let buildDir = process.env.BUILD_DIR;
if (buildDir == undefined) {
	buildDir = `${__dirname}/build`;
}
console.log("bundling to", buildDir);

function bundleAll() {
	return getTemplates().then((templates) => {
		return Promise.map(templates, bundle);
	});
}

if (process.env.NODE_ENV != "development") {
	bundleAll();
} else {
	const chokidar = require("chokidar");
	console.log("Watching for changes");
	chokidar.watch(`${__dirname}/templates`).on("all", (_, path) => {
		if (path.endsWith(".css")) {
			bundle([path.split("/").slice(-1)[0], path]);
		}
	});
	chokidar.watch(`${__dirname}/colors.css`).on("all", () => {
		console.log("colors.css updated, rebuilding all templates");
		bundleAll();
	});
}