build_tokens.ts

 1import * as fs from "fs"
 2import * as path from "path"
 3import { Theme, create_theme, useThemeStore } from "./common"
 4import { themes } from "./themes"
 5import { slugify } from "./utils/slugify"
 6import { theme_tokens } from "./theme/tokens/theme"
 7
 8const TOKENS_DIRECTORY = path.join(__dirname, "..", "target", "tokens")
 9const TOKENS_FILE = path.join(TOKENS_DIRECTORY, "$themes.json")
10const METADATA_FILE = path.join(TOKENS_DIRECTORY, "$metadata.json")
11
12function clear_tokens(tokens_directory: string) {
13    if (!fs.existsSync(tokens_directory)) {
14        fs.mkdirSync(tokens_directory, { recursive: true })
15    } else {
16        for (const file of fs.readdirSync(tokens_directory)) {
17            if (file.endsWith(".json")) {
18                fs.unlinkSync(path.join(tokens_directory, file))
19            }
20        }
21    }
22}
23
24type TokenSet = {
25    id: string
26    name: string
27    selected_token_sets: { [key: string]: "enabled" }
28}
29
30function build_token_set_order(theme: Theme[]): {
31    token_set_order: string[]
32} {
33    const token_set_order: string[] = theme.map((scheme) =>
34        scheme.name.toLowerCase().replace(/\s+/g, "_")
35    )
36    return { token_set_order }
37}
38
39function build_themes_index(theme: Theme[]): TokenSet[] {
40    const themes_index: TokenSet[] = theme.map((scheme, index) => {
41        const id = `${scheme.is_light ? "light" : "dark"}_${scheme.name
42            .toLowerCase()
43            .replace(/\s+/g, "_")}_${index}`
44        const selected_token_sets: { [key: string]: "enabled" } = {}
45        const token_set = scheme.name.toLowerCase().replace(/\s+/g, "_")
46        selected_token_sets[token_set] = "enabled"
47
48        return {
49            id,
50            name: `${scheme.name} - ${scheme.is_light ? "Light" : "Dark"}`,
51            selected_token_sets,
52        }
53    })
54
55    return themes_index
56}
57
58function write_tokens(themes: Theme[], tokens_directory: string) {
59    clear_tokens(tokens_directory)
60
61    for (const theme of themes) {
62        const { setTheme } = useThemeStore.getState()
63        setTheme(theme)
64
65        const file_name = slugify(theme.name) + ".json"
66        const tokens = theme_tokens()
67        const tokens_json = JSON.stringify(tokens, null, 2)
68        const out_path = path.join(tokens_directory, file_name)
69        fs.writeFileSync(out_path, tokens_json, { mode: 0o644 })
70        console.log(`- ${out_path} created`)
71    }
72
73    const theme_index_data = build_themes_index(themes)
74
75    const themes_json = JSON.stringify(theme_index_data, null, 2)
76    fs.writeFileSync(TOKENS_FILE, themes_json, { mode: 0o644 })
77    console.log(`- ${TOKENS_FILE} created`)
78
79    const token_set_order_data = build_token_set_order(themes)
80
81    const metadata_json = JSON.stringify(token_set_order_data, null, 2)
82    fs.writeFileSync(METADATA_FILE, metadata_json, { mode: 0o644 })
83    console.log(`- ${METADATA_FILE} created`)
84}
85
86const all_themes: Theme[] = themes.map((theme) => create_theme(theme))
87
88write_tokens(all_themes, TOKENS_DIRECTORY)