-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
271 lines (242 loc) · 8.21 KB
/
index.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
const fs = require('fs');
const path = require('path');
const token = require('./token');
const commonKey = ['background', 'font-color', 'border']
const Theme = token["cabin x theme"]
const commonTemplate = (currentKey) => {
return `\n/* ----------- ${currentKey} -----------*/\n`
}
const valueReg = 'cabin x theme.cabin x.'
const prefix = 'dm'
const PaletteTokenPrefix = `--${prefix}-token`
const CommonVarPrefix = `--${prefix}-common`
const ComponentPrefix = `--${prefix}-component`
/**
* 扁平化对象
* @param obj
* @param targetKey
* @param parentKey
* @param separator
* @returns {{}}
*/
const flattenObject = (obj, targetKey = 'value', parentKey = '', separator = '-') => {
const result = {};
let newKey = parentKey
if (obj[targetKey] !== undefined) {
result[newKey] = obj[targetKey];
} else {
for (const key in obj) {
newKey = parentKey ? parentKey + separator + key : key;
if (typeof obj[key] === 'object') {
const nestedObject = flattenObject(obj[key], targetKey, newKey, separator);
Object.assign(result, nestedObject);
}
}
}
return result;
}
/**
* 写文件
* @param filePath
* @param content
* @returns {Promise<unknown>}
*/
const writeFile = (filePath, content) => {
return new Promise((resolve, reject) => {
try {
const directoryPath = path.dirname(filePath)
if (!fs.existsSync(directoryPath)) {
// 如果目录不存在,使用 fs.mkdirSync 创建它
fs.mkdirSync(directoryPath, {recursive: true}); // 使用 { recursive: true } 可以创建多层目录
}
fs.writeFile(filePath, content, (err) => {
if (err) {
reject(false)
}
resolve(true)
})
} catch (e) {
reject(false)
}
})
}
/**
* 格式化 色板
* @param theme
* @returns {{}}
*/
const formatPaletteJsonToCssVarJson = (theme) => {
const palette = theme.palette;
return flattenObject(palette, 'value', `${PaletteTokenPrefix}-palette`);
}
/**
* 格式化 公共变量
* @param theme
* @returns {{}}
*/
const formatCommonVarJsonToCssVarJson = (theme) => {
let comData = {};
commonKey.forEach((key) => {
Object.keys(theme[key]).forEach((key2) => {
comData = {...comData, ...flattenObject(theme[key][key2], 'value', `${CommonVarPrefix}-${key}-${key2}`)}
})
})
return comData;
}
/**
* 格式化 组件变量
* @param theme
* @returns {{}}
*/
const formatComponentVarJsonToCssVarJson = (theme) => {
let components = theme.components;
return flattenObject(components, 'value', `${ComponentPrefix}`);
}
/**
* 获取字体大小和行高
* 默认使用默认值即可
* @returns {{"--cx-font-size-l-default": string, "--cx-font-size-m-default": string, "--cx-font-size-l-default-line-height": string, "--cx-font-size-s-default": string, "--cx-font-size-m-default-line-height": string, "--cx-font-size-s-default-line-height": string}}
*/
const getFontSizeAndLineHeight = ({
fontSizeS = 12,
fontSizeM = 14,
fontSizeL = 16,
lineHeightS = 18,
lineHeightM = 22,
lineHeightL = 24
}) => {
return {
//font-size
"--cx-font-size-s-default": `${fontSizeS}`, //small尺寸(三个尺寸中)默认字号
"--cx-font-size-m-default": `${fontSizeM}`, //normal尺寸(三个尺寸中)默认字号
"--cx-font-size-l-default": `${fontSizeL}`, //large尺寸(三个尺寸中)默认字号
//line-height
"--cx-font-size-s-default-line-height": `${lineHeightS}`,
"--cx-font-size-m-default-line-height": `${lineHeightM}`,
"--cx-font-size-l-default-line-height": `${lineHeightL}`
}
}
const writeComponentFile = async (name, data) => {
await writeFile(`./components/${name}.scss`, data.str)
await writeFile(`./components/${name}.json`, JSON.stringify(data.data))
}
const writePaletteFile = async (name, data) => {
await writeFile(`./palette/${name}.scss`, data.str)
await writeFile(`./palette/${name}.json`, JSON.stringify(data.data))
}
/**
* json 转 css var
* @param json
* @param prefixType
* @returns {{str: string, data: {}}}
*/
const jsonToCssVar = (json, prefixType = '',) => {
let str = '';
let lastKey = '';
let lastComponentName = '';
let prefixMap = {
palette: PaletteTokenPrefix,
common: CommonVarPrefix,
both: ComponentPrefix
}
let keyPrefix = prefixMap[prefixType];
let data = {};
Object.keys(json).forEach((key) => {
let currentKey = key.replace(keyPrefix, '');
/* -type-name-step */
currentKey = currentKey.split('-')[1];
// 组件
if (prefixType === 'both' && currentKey !== lastComponentName) {
str += commonTemplate(currentKey);
lastComponentName = currentKey;
}
// 普通
currentKey = key.replace(keyPrefix, '').split('-')[2];
if (currentKey !== lastKey && keyPrefix) {
str += commonTemplate(currentKey);
lastKey = currentKey;
}
// 如果是number 则+px
if (typeof json[key] === 'number') {
json[key] = `${json[key]}px`;
data[key] = `${json[key]}px`;
}
// {cabin x theme.cabin x.palette.gray.20}
if (json[key].includes(valueReg)) {
const reg = new RegExp(valueReg, 'g');
let valueKey = json[key].replace(/{|}/g, '').replace(reg, '').replace(/\./g, '-');
let prefix = valueKey.startsWith('palette') ? prefixMap.palette : prefixMap.common;
if (prefixType !== 'both' || valueKey.split('-')[0] === 'palette') {
prefix = prefixMap.palette;
}
json[key] = `var(${prefix}-${valueKey})`;
data[key] = `${prefix}-${valueKey}`;
}else {
data[key] = `${json[key]}`;
}
str += `${key}: ${json[key]};\n`
})
return {str, data};
}
/**
* 组合公共变量
* @param palette
* @param common
* @param fontSize
* @returns
*/
const comboCommonCssVarContent = (palette, common, fontSize) => {
const paletteData = jsonToCssVar(palette, 'palette');
const commonData = jsonToCssVar(palette, 'palette');
const fontSizeData = jsonToCssVar(palette, 'palette');
return {
str: `
:root,:host{
/***************** 色板 *************************/
${paletteData.str}
/***************** 通用变量 **********************/
${commonData.str}
/***************** 字体 **********************/
${fontSizeData.str}
}
`,
data: {
...paletteData.data,
...commonData.data,
...fontSizeData.data
}
}
}
/**
* 组合组件变量
* @param components
* @returns
*/
const comboComponentCssVarContent = (components) => {
let componentData = jsonToCssVar(components, 'both')
return {
str: `
:root,:host{
/***************** 组件 *************************/
${componentData.str}
}
`,
data: {
...componentData.data
}
}
}
const genThemeFile = async (Theme) => {
const ThemeNames = Object.keys(Theme);
ThemeNames.forEach((name) => {
const themeData = Theme[name]
const ThemeName = name;
const paletteJson = formatPaletteJsonToCssVarJson(themeData);
const commonJson = formatCommonVarJsonToCssVarJson(themeData);
const fontSizeJson = getFontSizeAndLineHeight(themeData)
writePaletteFile(ThemeName, comboCommonCssVarContent(paletteJson, commonJson, fontSizeJson));
const componentsJson = formatComponentVarJsonToCssVarJson(themeData);
writeComponentFile(ThemeName, comboComponentCssVarContent(componentsJson));
})
}
genThemeFile(Theme)