buildTokens.ts

 1import * as fs from "fs"
 2import * as path from "path"
 3import { ColorScheme, createColorScheme } from "./common"
 4import { themes } from "./themes"
 5import { slugify } from "./utils/slugify"
 6import { colorSchemeTokens } from "./theme/tokens/colorScheme"
 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 clearTokens(tokensDirectory: string) {
13    if (!fs.existsSync(tokensDirectory)) {
14        fs.mkdirSync(tokensDirectory, { recursive: true })
15    } else {
16        for (const file of fs.readdirSync(tokensDirectory)) {
17            if (file.endsWith(".json")) {
18                fs.unlinkSync(path.join(tokensDirectory, file))
19            }
20        }
21    }
22}
23
24type TokenSet = {
25    id: string
26    name: string
27    selectedTokenSets: { [key: string]: "enabled" }
28}
29
30function buildTokenSetOrder(colorSchemes: ColorScheme[]): {
31    tokenSetOrder: string[]
32} {
33    const tokenSetOrder: string[] = colorSchemes.map((scheme) =>
34        scheme.name.toLowerCase().replace(/\s+/g, "_")
35    )
36    return { tokenSetOrder }
37}
38
39function buildThemesIndex(colorSchemes: ColorScheme[]): TokenSet[] {
40    const themesIndex: TokenSet[] = colorSchemes.map((scheme, index) => {
41        const id = `${scheme.isLight ? "light" : "dark"}_${scheme.name
42            .toLowerCase()
43            .replace(/\s+/g, "_")}_${index}`
44        const selectedTokenSets: { [key: string]: "enabled" } = {}
45        const tokenSet = scheme.name.toLowerCase().replace(/\s+/g, "_")
46        selectedTokenSets[tokenSet] = "enabled"
47
48        return {
49            id,
50            name: `${scheme.name} - ${scheme.isLight ? "Light" : "Dark"}`,
51            selectedTokenSets,
52        }
53    })
54
55    return themesIndex
56}
57
58function writeTokens(colorSchemes: ColorScheme[], tokensDirectory: string) {
59    clearTokens(tokensDirectory)
60
61    for (const colorScheme of colorSchemes) {
62        const fileName = slugify(colorScheme.name) + ".json"
63        const tokens = colorSchemeTokens(colorScheme)
64        const tokensJSON = JSON.stringify(tokens, null, 2)
65        const outPath = path.join(tokensDirectory, fileName)
66        fs.writeFileSync(outPath, tokensJSON, { mode: 0o644 })
67        console.log(`- ${outPath} created`)
68    }
69
70    const themeIndexData = buildThemesIndex(colorSchemes)
71
72    const themesJSON = JSON.stringify(themeIndexData, null, 2)
73    fs.writeFileSync(TOKENS_FILE, themesJSON, { mode: 0o644 })
74    console.log(`- ${TOKENS_FILE} created`)
75
76    const tokenSetOrderData = buildTokenSetOrder(colorSchemes)
77
78    const metadataJSON = JSON.stringify(tokenSetOrderData, null, 2)
79    fs.writeFileSync(METADATA_FILE, metadataJSON, { mode: 0o644 })
80    console.log(`- ${METADATA_FILE} created`)
81}
82
83const colorSchemes: ColorScheme[] = themes.map((theme) =>
84    createColorScheme(theme)
85)
86
87writeTokens(colorSchemes, TOKENS_DIRECTORY)