zed/styles/src/buildThemes.ts

44 lines
1.5 KiB
TypeScript
Raw Normal View History

import * as fs from "fs"
import { tmpdir } from "os"
import * as path from "path"
import app from "./styleTree/app"
import { ColorScheme, createColorScheme } from "./theme/colorScheme"
import snakeCase from "./utils/snakeCase"
2023-06-07 15:10:02 +00:00
import { themes } from "./themes"
const assetsDirectory = `${__dirname}/../../assets`
const tempDirectory = fs.mkdtempSync(path.join(tmpdir(), "build-themes"))
// Clear existing themes
function clearThemes(themeDirectory: string) {
if (!fs.existsSync(themeDirectory)) {
fs.mkdirSync(themeDirectory, { recursive: true })
} else {
for (const file of fs.readdirSync(themeDirectory)) {
if (file.endsWith(".json")) {
2023-06-07 15:10:02 +00:00
fs.unlinkSync(path.join(themeDirectory, file))
}
}
}
}
function writeThemes(colorSchemes: ColorScheme[], outputDirectory: string) {
2023-06-07 15:10:02 +00:00
clearThemes(outputDirectory)
2023-06-28 20:44:18 +00:00
for (const colorScheme of colorSchemes) {
const styleTree = snakeCase(app(colorScheme))
const styleTreeJSON = JSON.stringify(styleTree, null, 2)
const tempPath = path.join(tempDirectory, `${colorScheme.name}.json`)
const outPath = path.join(outputDirectory, `${colorScheme.name}.json`)
fs.writeFileSync(tempPath, styleTreeJSON)
fs.renameSync(tempPath, outPath)
console.log(`- ${outPath} created`)
}
}
const colorSchemes: ColorScheme[] = themes.map((theme) =>
createColorScheme(theme)
)
2023-06-07 16:40:49 +00:00
// Write new themes to theme directory
2023-06-07 16:40:49 +00:00
writeThemes(colorSchemes, `${assetsDirectory}/themes`)