-
Notifications
You must be signed in to change notification settings - Fork 1
/
genTheme.js
104 lines (87 loc) · 2.06 KB
/
genTheme.js
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const fs = require('licia/fs')
const each = require('licia/each')
const startWith = require('licia/startWith')
const endWith = require('licia/endWith')
const contain = require('licia/contain')
const kebabCase = require('licia/kebabCase')
const camelCase = require('licia/camelCase')
const { theme } = require('antd')
module.exports = async function (options) {
const { input, output } = options
const config = JSON.parse(await fs.readFile(input, 'utf8'))
let result = ''
if (endWith(output, '.ts')) {
result = await outputTs(config)
} else {
result = await outputScss(config, output)
}
await fs.writeFile(output, result, 'utf8')
}
async function outputScss(config, output) {
let scss = '// light\n'
each(theme.getDesignToken(config), (val, key) => {
if (filter(key)) {
return
}
scss += `$${kebabCase(key)}: ${val};\n`
})
scss += '\n// dark\n'
config.algorithm = theme.darkAlgorithm
each(theme.getDesignToken(config), (val, key) => {
if (filter(key) || filterDark(key)) {
return
}
scss += `$${kebabCase(key)}-dark: ${val};\n`
})
return scss
}
function outputTs(config) {
let ts = '// light\n'
each(theme.getDesignToken(config), (val, key) => {
if (filter(key)) {
return
}
ts += `export const ${camelCase(key)} = \`${val}\`\n`
})
ts += '\n// dark\n'
config.algorithm = theme.darkAlgorithm
each(theme.getDesignToken(config), (val, key) => {
if (filter(key) || filterDark(key)) {
return
}
ts += `export const ${camelCase(key)}Dark = \`${val}\`\n`
})
return ts
}
const colors = [
'blue',
'purple',
'cyan',
'green',
'magenta',
'pink',
'red',
'orange',
'yellow',
'volcano',
'geekblue',
'gold',
'lime',
]
function filter(key) {
if (contain(colors, key)) {
return true
}
return !contain(key, '-') && /\d/.test(key)
}
function filterDark(key) {
if (startWith(key, 'color') || startWith(key, 'boxShadow')) {
return false
}
for (const color of colors) {
if (startWith(key, color)) {
return false
}
}
return true
}