1import * as fs from "fs"
2import * as path from "path"
3import { ColorScheme, create_color_scheme } from "./common"
4import { themes } from "./themes"
5import { slugify } from "./utils/slugify"
6import { theme_tokens } from "./theme/tokens/color_scheme"
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: ColorScheme[]): {
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: ColorScheme[]): 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: ColorScheme[], tokens_directory: string) {
59 clear_tokens(tokens_directory)
60
61 for (const theme of themes) {
62 const file_name = slugify(theme.name) + ".json"
63 const tokens = theme_tokens(theme)
64 const tokens_json = JSON.stringify(tokens, null, 2)
65 const out_path = path.join(tokens_directory, file_name)
66 fs.writeFileSync(out_path, tokens_json, { mode: 0o644 })
67 console.log(`- ${out_path} created`)
68 }
69
70 const theme_index_data = build_themes_index(themes)
71
72 const themes_json = JSON.stringify(theme_index_data, null, 2)
73 fs.writeFileSync(TOKENS_FILE, themes_json, { mode: 0o644 })
74 console.log(`- ${TOKENS_FILE} created`)
75
76 const token_set_order_data = build_token_set_order(themes)
77
78 const metadata_json = JSON.stringify(token_set_order_data, null, 2)
79 fs.writeFileSync(METADATA_FILE, metadata_json, { mode: 0o644 })
80 console.log(`- ${METADATA_FILE} created`)
81}
82
83const all_themes: ColorScheme[] = themes.map((theme) =>
84 create_color_scheme(theme)
85)
86
87write_tokens(all_themes, TOKENS_DIRECTORY)