2023-06-21 17:58:54 +00:00
|
|
|
import * as fs from "fs"
|
|
|
|
import * as path from "path"
|
|
|
|
import { ColorScheme, createColorScheme } from "./common"
|
|
|
|
import { themes } from "./themes"
|
|
|
|
import { slugify } from "./utils/slugify"
|
|
|
|
import { colorSchemeTokens } from "./theme/tokens/colorScheme"
|
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
|
|
|
|
|
|
|
function clearTokens(tokensDirectory: string) {
|
|
|
|
if (!fs.existsSync(tokensDirectory)) {
|
|
|
|
fs.mkdirSync(tokensDirectory, { recursive: true })
|
|
|
|
} else {
|
|
|
|
for (const file of fs.readdirSync(tokensDirectory)) {
|
|
|
|
if (file.endsWith(".json")) {
|
|
|
|
fs.unlinkSync(path.join(tokensDirectory, file))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-08 20:19:28 +00:00
|
|
|
type TokenSet = {
|
2023-06-21 17:58:54 +00:00
|
|
|
id: string
|
|
|
|
name: string
|
|
|
|
selectedTokenSets: { [key: string]: "enabled" }
|
|
|
|
}
|
2023-06-08 20:19:28 +00:00
|
|
|
|
2023-06-21 17:58:54 +00:00
|
|
|
function buildTokenSetOrder(colorSchemes: ColorScheme[]): {
|
|
|
|
tokenSetOrder: string[]
|
|
|
|
} {
|
|
|
|
const tokenSetOrder: string[] = colorSchemes.map((scheme) =>
|
|
|
|
scheme.name.toLowerCase().replace(/\s+/g, "_")
|
|
|
|
)
|
|
|
|
return { tokenSetOrder }
|
2023-06-08 20:19:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function buildThemesIndex(colorSchemes: ColorScheme[]): TokenSet[] {
|
|
|
|
const themesIndex: TokenSet[] = colorSchemes.map((scheme, index) => {
|
|
|
|
const id = `${scheme.isLight ? "light" : "dark"}_${scheme.name
|
|
|
|
.toLowerCase()
|
2023-06-21 17:58:54 +00:00
|
|
|
.replace(/\s+/g, "_")}_${index}`
|
|
|
|
const selectedTokenSets: { [key: string]: "enabled" } = {}
|
|
|
|
const tokenSet = scheme.name.toLowerCase().replace(/\s+/g, "_")
|
|
|
|
selectedTokenSets[tokenSet] = "enabled"
|
2023-06-08 20:19:28 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
id,
|
|
|
|
name: `${scheme.name} - ${scheme.isLight ? "Light" : "Dark"}`,
|
|
|
|
selectedTokenSets,
|
2023-06-21 17:58:54 +00:00
|
|
|
}
|
|
|
|
})
|
2023-06-08 20:19:28 +00:00
|
|
|
|
2023-06-21 17:58:54 +00:00
|
|
|
return themesIndex
|
2023-06-08 20:19:28 +00:00
|
|
|
}
|
|
|
|
|
2023-06-08 04:37:00 +00:00
|
|
|
function writeTokens(colorSchemes: ColorScheme[], tokensDirectory: string) {
|
2023-06-21 17:58:54 +00:00
|
|
|
clearTokens(tokensDirectory)
|
2023-06-08 04:37:00 +00:00
|
|
|
|
|
|
|
for (const colorScheme of colorSchemes) {
|
2023-06-21 17:58:54 +00:00
|
|
|
const fileName = slugify(colorScheme.name) + ".json"
|
|
|
|
const tokens = colorSchemeTokens(colorScheme)
|
|
|
|
const tokensJSON = JSON.stringify(tokens, null, 2)
|
|
|
|
const outPath = path.join(tokensDirectory, fileName)
|
|
|
|
fs.writeFileSync(outPath, tokensJSON, { mode: 0o644 })
|
|
|
|
console.log(`- ${outPath} created`)
|
2023-06-08 04:37:00 +00:00
|
|
|
}
|
2023-06-08 20:19:28 +00:00
|
|
|
|
2023-06-21 17:58:54 +00:00
|
|
|
const themeIndexData = buildThemesIndex(colorSchemes)
|
2023-06-08 20:19:28 +00:00
|
|
|
|
2023-06-21 17:58:54 +00:00
|
|
|
const themesJSON = JSON.stringify(themeIndexData, null, 2)
|
|
|
|
fs.writeFileSync(TOKENS_FILE, themesJSON, { mode: 0o644 })
|
|
|
|
console.log(`- ${TOKENS_FILE} created`)
|
2023-06-08 20:19:28 +00:00
|
|
|
|
2023-06-21 17:58:54 +00:00
|
|
|
const tokenSetOrderData = buildTokenSetOrder(colorSchemes)
|
2023-06-08 20:19:28 +00:00
|
|
|
|
2023-06-21 17:58:54 +00:00
|
|
|
const metadataJSON = JSON.stringify(tokenSetOrderData, null, 2)
|
|
|
|
fs.writeFileSync(METADATA_FILE, metadataJSON, { mode: 0o644 })
|
|
|
|
console.log(`- ${METADATA_FILE} created`)
|
2023-06-08 04:37:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const colorSchemes: ColorScheme[] = themes.map((theme) =>
|
|
|
|
createColorScheme(theme)
|
2023-06-21 17:58:54 +00:00
|
|
|
)
|
2023-06-08 04:37:00 +00:00
|
|
|
|
2023-06-21 17:58:54 +00:00
|
|
|
writeTokens(colorSchemes, TOKENS_DIRECTORY)
|