buildThemes.ts

 1import * as fs from "fs";
 2import * as path from "path";
 3import { tmpdir } from "os";
 4import app from "./styleTree/app";
 5import themes, { internalThemes, experimentalThemes } from "./themes";
 6import snakeCase from "./utils/snakeCase";
 7import Theme from "./themes/common/theme";
 8
 9const themeDirectory = `${__dirname}/../../assets/themes`;
10const internalDirectory = `${themeDirectory}/internal`;
11const experimentsDirectory = `${themeDirectory}/experiments`;
12const tempDirectory = fs.mkdtempSync(path.join(tmpdir(), "build-themes"));
13
14// Clear existing themes
15function clearThemes(themeDirectory: string) {
16  for (const file of fs.readdirSync(themeDirectory)) {
17    if (file.endsWith(".json")) {
18      const name = file.replace(/\.json$/, "");
19      if (!themes.find((theme) => theme.name === name)) {
20        fs.unlinkSync(path.join(themeDirectory, file));
21      }
22    }
23  }
24}
25
26clearThemes(themeDirectory);
27clearThemes(internalDirectory);
28clearThemes(experimentsDirectory);
29
30function writeThemes(themes: Theme[], outputDirectory: string) {
31  for (let theme of themes) {
32    let styleTree = snakeCase(app(theme));
33    let styleTreeJSON = JSON.stringify(styleTree, null, 2);
34    let tempPath = path.join(tempDirectory, `${theme.name}.json`);
35    let outPath = path.join(outputDirectory, `${theme.name}.json`);
36    fs.writeFileSync(tempPath, styleTreeJSON);
37    fs.renameSync(tempPath, outPath);
38    console.log(`- ${outPath} created`);
39  }
40}
41
42// Write new themes to theme directory
43writeThemes(themes, themeDirectory);
44writeThemes(internalThemes, internalDirectory);
45writeThemes(experimentalThemes, experimentsDirectory);