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[]): { tokenSetOrder: string[] } {
31    const tokenSetOrder: string[] = colorSchemes.map(
32        (scheme) => scheme.name.toLowerCase().replace(/\s+/g, "_")
33    );
34    return { tokenSetOrder };
35}
36
37function buildThemesIndex(colorSchemes: ColorScheme[]): TokenSet[] {
38    const themesIndex: TokenSet[] = colorSchemes.map((scheme, index) => {
39        const id = `${scheme.isLight ? "light" : "dark"}_${scheme.name
40            .toLowerCase()
41            .replace(/\s+/g, "_")}_${index}`;
42        const selectedTokenSets: { [key: string]: "enabled" } = {};
43        const tokenSet = scheme.name.toLowerCase().replace(/\s+/g, "_");
44        selectedTokenSets[tokenSet] = "enabled";
45
46        return {
47            id,
48            name: `${scheme.name} - ${scheme.isLight ? "Light" : "Dark"}`,
49            selectedTokenSets,
50        };
51    });
52
53    return themesIndex;
54}
55
56function writeTokens(colorSchemes: ColorScheme[], tokensDirectory: string) {
57    clearTokens(tokensDirectory);
58
59    for (const colorScheme of colorSchemes) {
60        const fileName = slugify(colorScheme.name) + ".json";
61        const tokens = colorSchemeTokens(colorScheme);
62        const tokensJSON = JSON.stringify(tokens, null, 2);
63        const outPath = path.join(tokensDirectory, fileName);
64        fs.writeFileSync(outPath, tokensJSON, { mode: 0o644 });
65        console.log(`- ${outPath} created`);
66    }
67
68    const themeIndexData = buildThemesIndex(colorSchemes);
69
70    const themesJSON = JSON.stringify(themeIndexData, null, 2);
71    fs.writeFileSync(TOKENS_FILE, themesJSON, { mode: 0o644 });
72    console.log(`- ${TOKENS_FILE} created`);
73
74    const tokenSetOrderData = buildTokenSetOrder(colorSchemes);
75
76    const metadataJSON = JSON.stringify(tokenSetOrderData, null, 2);
77    fs.writeFileSync(METADATA_FILE, metadataJSON, { mode: 0o644 });
78    console.log(`- ${METADATA_FILE} created`);
79}
80
81const colorSchemes: ColorScheme[] = themes.map((theme) =>
82    createColorScheme(theme)
83);
84
85writeTokens(colorSchemes, TOKENS_DIRECTORY);