zed/styles/src/build_themes.ts

48 lines
1.5 KiB
TypeScript
Raw Normal View History

import * as fs from "fs"
import { tmpdir } from "os"
import * as path from "path"
2023-06-28 22:20:43 +00:00
import app from "./style_tree/app"
2023-07-04 05:20:56 +00:00
import { Theme, create_theme } from "./theme/create_theme"
2023-06-07 15:10:02 +00:00
import { themes } from "./themes"
import { useThemeStore } from "./theme"
2023-06-29 05:48:40 +00:00
const assets_directory = `${__dirname}/../../assets`
const temp_directory = fs.mkdtempSync(path.join(tmpdir(), "build-themes"))
2023-06-29 05:48:40 +00:00
function clear_themes(theme_directory: string) {
if (!fs.existsSync(theme_directory)) {
fs.mkdirSync(theme_directory, { recursive: true })
} else {
2023-06-29 05:48:40 +00:00
for (const file of fs.readdirSync(theme_directory)) {
if (file.endsWith(".json")) {
2023-06-29 05:48:40 +00:00
fs.unlinkSync(path.join(theme_directory, file))
}
}
}
}
const all_themes: Theme[] = themes.map((theme) =>
create_theme(theme)
)
2023-07-04 05:20:56 +00:00
function write_themes(themes: Theme[], output_directory: string) {
2023-06-29 05:48:40 +00:00
clear_themes(output_directory)
2023-07-04 05:20:56 +00:00
for (const theme of themes) {
const { setTheme } = useThemeStore.getState()
2023-07-04 05:20:56 +00:00
setTheme(theme)
const style_tree = app()
2023-06-29 05:48:40 +00:00
const style_tree_json = JSON.stringify(style_tree, null, 2)
2023-07-04 05:20:56 +00:00
const temp_path = path.join(temp_directory, `${theme.name}.json`)
const out_path = path.join(
output_directory,
`${theme.name}.json`
)
2023-06-29 05:48:40 +00:00
fs.writeFileSync(temp_path, style_tree_json)
fs.renameSync(temp_path, out_path)
console.log(`- ${out_path} created`)
}
}
2023-06-29 15:41:51 +00:00
write_themes(all_themes, `${assets_directory}/themes`)