buildThemes.ts

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