-
Notifications
You must be signed in to change notification settings - Fork 0
/
writeTailwindThemeConf.ts
47 lines (38 loc) · 1.21 KB
/
writeTailwindThemeConf.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
import { writeFileSync } from 'fs'
import { join } from 'path'
import { themes } from './src/themes/index'
import type { Theme } from './src/themes/types'
function objectEntries<T extends Record<string | number, unknown>>(
obj: T
): [keyof T, T[keyof T]][] {
return Object.entries(obj) as [keyof T, T[keyof T]][]
}
function toTailwindConfig(theme: Theme): Theme {
const themeCpy: Theme = JSON.parse(JSON.stringify(theme))
function flatten(obj: Theme, path?: string) {
objectEntries(obj).forEach(([key, value]) => {
if (value !== null && typeof value === 'object') {
const formattedPath = path ? `--${path}-${key}` : `--${key}`
flatten(value as unknown as Theme, formattedPath.replace(/-{3,}/, '--'))
return
}
/* eslint-disable */
if (typeof value === 'string') {
/* @ts-ignore */
obj[key as any] = `var(${path}-${key})`
/* eslint-enable */
}
})
}
flatten(themeCpy)
return themeCpy
}
function syncWriteFile(filename: string, data: string) {
writeFileSync(join(__dirname, filename), data, {
flag: 'w',
})
}
syncWriteFile(
'./tailwind.theme.conf.js',
`module.exports = ${JSON.stringify(toTailwindConfig(themes.default))} `
)