1import * as fs from "fs"
2import { tmpdir } from "os"
3import * as path from "path"
4import app from "./style_tree/app"
5import { Theme, create_theme } from "./theme/create_theme"
6import { themes } from "./themes"
7import { useThemeStore } from "./theme"
8
9const assets_directory = `${__dirname}/../../assets`
10const temp_directory = fs.mkdtempSync(path.join(tmpdir(), "build-themes"))
11
12function clear_themes(theme_directory: string) {
13 if (!fs.existsSync(theme_directory)) {
14 fs.mkdirSync(theme_directory, { recursive: true })
15 } else {
16 for (const file of fs.readdirSync(theme_directory)) {
17 if (file.endsWith(".json")) {
18 fs.unlinkSync(path.join(theme_directory, file))
19 }
20 }
21 }
22}
23
24const all_themes: Theme[] = themes.map((theme) => create_theme(theme))
25
26function write_themes(themes: Theme[], output_directory: string) {
27 clear_themes(output_directory)
28 for (const theme of themes) {
29 const { setTheme } = useThemeStore.getState()
30 setTheme(theme)
31
32 const style_tree = app()
33 // Nathan: New elements will read directly from the theme colors.
34 // Adding this during the transition. Afterwards, we can port all themes to Rust.
35 style_tree.base_theme = theme
36 const style_tree_json = JSON.stringify(style_tree, null, 2)
37 const temp_path = path.join(temp_directory, `${theme.name}.json`)
38 const out_path = path.join(output_directory, `${theme.name}.json`)
39 fs.writeFileSync(temp_path, style_tree_json)
40 fs.renameSync(temp_path, out_path)
41 console.log(`- ${out_path} created`)
42 }
43}
44
45write_themes(all_themes, `${assets_directory}/themes`)