-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.ts
78 lines (69 loc) · 1.83 KB
/
generate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import path from 'node:path'
import fsp from 'node:fs/promises'
import { ShikiMap } from '../src/types'
const entries = (obj: any) => Object.entries(obj ?? {})
const values = (obj: any) => Object.values(obj ?? {})
// Modify this line
const PREFIX = 's'
async function generateOne(themePath: string) {
const shikiMap: ShikiMap = {}
let count = 0
const getClass = () => {
const className = PREFIX + count
count++
return className
}
function addValue(color: string) {
if (typeof color !== 'string') {
return
}
color = color.toLowerCase()
if (shikiMap[color]) {
return
}
shikiMap[color] = getClass()
}
const text = await fsp.readFile(themePath, 'utf-8')
const json = JSON.parse(text)
for (const token of json.tokenColors) {
const { foreground } = token.settings ?? {}
if (foreground) {
addValue(foreground as string)
}
}
for (const value of values(json.semanticTokenColors)) {
addValue(value as string)
}
return shikiMap
}
function generateCss(obj: ShikiMap) {
let style = ''
for (const [value, key] of entries(obj)) {
style += `.${key} {
color: ${value};
}
`
}
return style
}
async function generate() {
const themeDirPath = path.join(
process.cwd(),
'node_modules',
'tm-themes',
'themes',
)
const themeNames = await fsp.readdir(themeDirPath)
await Promise.all(
themeNames.map(async (name) => {
const themePath = path.join(themeDirPath, name)
const map = await generateOne(themePath)
const baseName = name.replace('.json', '')
const css = generateCss(map)
const writeDirPath = path.join(process.cwd(), 'src', 'themes')
await fsp.writeFile(path.join(writeDirPath, name), JSON.stringify(map))
await fsp.writeFile(path.join(writeDirPath, `${baseName}.css`), css)
}),
)
}
generate()