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")
9
10function clearTokens(tokensDirectory: string) {
11 if (!fs.existsSync(tokensDirectory)) {
12 fs.mkdirSync(tokensDirectory, { recursive: true })
13 } else {
14 for (const file of fs.readdirSync(tokensDirectory)) {
15 if (file.endsWith(".json")) {
16 fs.unlinkSync(path.join(tokensDirectory, file))
17 }
18 }
19 }
20}
21
22function writeTokens(colorSchemes: ColorScheme[], tokensDirectory: string) {
23 clearTokens(tokensDirectory)
24
25 for (const colorScheme of colorSchemes) {
26 const fileName = slugify(colorScheme.name)
27 const tokens = colorSchemeTokens(colorScheme)
28 const tokensJSON = JSON.stringify(tokens, null, 2)
29 const outPath = path.join(tokensDirectory, `${fileName}.json`)
30 fs.writeFileSync(outPath, tokensJSON)
31 console.log(`- ${outPath} created`)
32 }
33}
34
35const colorSchemes: ColorScheme[] = themes.map((theme) =>
36 createColorScheme(theme)
37)
38
39writeTokens(colorSchemes, TOKENS_DIRECTORY)