buildThemes.ts

 1import * as fs from "fs";
 2import * as path from "path";
 3import { tmpdir } from "os";
 4import app from "./styleTree/app";
 5import themes from "./themes";
 6import snakeCase from "./utils/snakeCase";
 7
 8const themeDirectory = `${__dirname}/../../assets/themes/`;
 9const tempDirectory = fs.mkdtempSync(path.join(tmpdir(), "build-themes"));
10
11// Clear existing themes
12for (const file of fs.readdirSync(themeDirectory)) {
13  if (file.endsWith(".json")) {
14    const name = file.replace(/\.json$/, "");
15    if (!themes.find((theme) => theme.name === name)) {
16      fs.unlinkSync(path.join(themeDirectory, file));
17    }
18  }
19}
20
21// Write new themes to theme directory
22for (let theme of themes) {
23  let styleTree = snakeCase(app(theme));
24  let styleTreeJSON = JSON.stringify(styleTree, null, 2);
25  let tempPath = path.join(tempDirectory, `${theme.name}.json`);
26  let outPath = path.join(themeDirectory, `${theme.name}.json`);
27  fs.writeFileSync(tempPath, styleTreeJSON);
28  fs.renameSync(tempPath, outPath);
29  console.log(`- ${outPath} created`);
30}