zed/styles/src/build_tokens.ts

88 lines
2.9 KiB
TypeScript
Raw Normal View History

2023-06-21 17:58:54 +00:00
import * as fs from "fs"
import * as path from "path"
2023-06-29 06:16:21 +00:00
import { ColorScheme, create_color_scheme } from "./common"
2023-06-21 17:58:54 +00:00
import { themes } from "./themes"
import { slugify } from "./utils/slugify"
2023-06-29 15:41:51 +00:00
import { theme_tokens } from "./theme/tokens/color_scheme"
2023-06-08 04:37:00 +00:00
2023-06-21 17:58:54 +00:00
const TOKENS_DIRECTORY = path.join(__dirname, "..", "target", "tokens")
const TOKENS_FILE = path.join(TOKENS_DIRECTORY, "$themes.json")
const METADATA_FILE = path.join(TOKENS_DIRECTORY, "$metadata.json")
2023-06-08 04:37:00 +00:00
2023-06-29 05:48:40 +00:00
function clear_tokens(tokens_directory: string) {
if (!fs.existsSync(tokens_directory)) {
fs.mkdirSync(tokens_directory, { recursive: true })
2023-06-08 04:37:00 +00:00
} else {
2023-06-29 05:48:40 +00:00
for (const file of fs.readdirSync(tokens_directory)) {
2023-06-08 04:37:00 +00:00
if (file.endsWith(".json")) {
2023-06-29 05:48:40 +00:00
fs.unlinkSync(path.join(tokens_directory, file))
2023-06-08 04:37:00 +00:00
}
}
}
}
type TokenSet = {
2023-06-21 17:58:54 +00:00
id: string
name: string
2023-06-29 05:48:40 +00:00
selected_token_sets: { [key: string]: "enabled" }
2023-06-21 17:58:54 +00:00
}
2023-06-29 05:48:40 +00:00
function build_token_set_order(theme: ColorScheme[]): {
token_set_order: string[]
2023-06-21 17:58:54 +00:00
} {
2023-06-29 05:48:40 +00:00
const token_set_order: string[] = theme.map((scheme) =>
2023-06-21 17:58:54 +00:00
scheme.name.toLowerCase().replace(/\s+/g, "_")
)
2023-06-29 05:48:40 +00:00
return { token_set_order }
}
2023-06-29 05:48:40 +00:00
function build_themes_index(theme: ColorScheme[]): TokenSet[] {
const themes_index: TokenSet[] = theme.map((scheme, index) => {
2023-06-28 22:20:43 +00:00
const id = `${scheme.is_light ? "light" : "dark"}_${scheme.name
.toLowerCase()
2023-06-21 17:58:54 +00:00
.replace(/\s+/g, "_")}_${index}`
2023-06-29 05:48:40 +00:00
const selected_token_sets: { [key: string]: "enabled" } = {}
const token_set = scheme.name.toLowerCase().replace(/\s+/g, "_")
selected_token_sets[token_set] = "enabled"
return {
id,
2023-06-28 22:20:43 +00:00
name: `${scheme.name} - ${scheme.is_light ? "Light" : "Dark"}`,
2023-06-29 05:48:40 +00:00
selected_token_sets,
2023-06-21 17:58:54 +00:00
}
})
2023-06-29 05:48:40 +00:00
return themes_index
}
2023-06-29 05:48:40 +00:00
function write_tokens(themes: ColorScheme[], tokens_directory: string) {
clear_tokens(tokens_directory)
2023-06-08 04:37:00 +00:00
2023-06-29 05:48:40 +00:00
for (const theme of themes) {
const file_name = slugify(theme.name) + ".json"
2023-07-04 04:32:27 +00:00
const tokens = theme_tokens()
2023-06-29 05:48:40 +00:00
const tokens_json = JSON.stringify(tokens, null, 2)
const out_path = path.join(tokens_directory, file_name)
fs.writeFileSync(out_path, tokens_json, { mode: 0o644 })
console.log(`- ${out_path} created`)
2023-06-08 04:37:00 +00:00
}
2023-06-29 05:48:40 +00:00
const theme_index_data = build_themes_index(themes)
2023-06-29 05:48:40 +00:00
const themes_json = JSON.stringify(theme_index_data, null, 2)
fs.writeFileSync(TOKENS_FILE, themes_json, { mode: 0o644 })
2023-06-21 17:58:54 +00:00
console.log(`- ${TOKENS_FILE} created`)
2023-06-29 05:48:40 +00:00
const token_set_order_data = build_token_set_order(themes)
2023-06-29 05:48:40 +00:00
const metadata_json = JSON.stringify(token_set_order_data, null, 2)
fs.writeFileSync(METADATA_FILE, metadata_json, { mode: 0o644 })
2023-06-21 17:58:54 +00:00
console.log(`- ${METADATA_FILE} created`)
2023-06-08 04:37:00 +00:00
}
2023-06-29 05:48:40 +00:00
const all_themes: ColorScheme[] = themes.map((theme) =>
2023-06-29 06:16:21 +00:00
create_color_scheme(theme)
2023-06-21 17:58:54 +00:00
)
2023-06-08 04:37:00 +00:00
2023-06-29 05:48:40 +00:00
write_tokens(all_themes, TOKENS_DIRECTORY)