forked from opentiny/tiny-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththemeConcat.mjs
73 lines (65 loc) · 2.27 KB
/
themeConcat.mjs
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
import fs from 'node:fs'
import fg from 'fast-glob'
import gulp from 'gulp'
import minimist from 'minimist'
themeConcat.description = '将 theme/src中所有的smb-theme.js aurora-theme.js合并到一起,放到theme/src/theme对应目录中去'
themeConcat.flags = {
'--watch -w': '开发时,进入watch模式,监听变化并立即合并 *-theme.js'
}
export default function themeConcat(cb) {
const argv = minimist(process.argv.slice(2))
if (argv.w || argv.watch) {
gulp.watch('packages/theme/**/*-theme.js', concat)
} else {
concat(cb)
}
}
function concat(cb) {
fg(['packages/theme/**/*-theme.js']).then((files) => {
const ignoreNames = ['base', 'theme']
// 1、遍历,返回全部 [{ name:'button', theme:'smb-theme'} ......]
const components = files
.map((file) => {
const arr = file.split('/')
const themeFile = arr.slice(-1)[0]
const content = fs.readFileSync(file, { encoding: 'utf8' })
return {
name: arr.slice(-2)[0],
theme: themeFile.substring(0, themeFile.indexOf('.')),
content
}
})
.filter((item) => !ignoreNames.includes(item.name))
// 2、把混在一起的主题js文件分开,返回 {'aurora-theme':[], 'smb-theme':[]}
const themes = {}
components.forEach((comp) => {
const key = comp.theme
if (!themes[key]) {
themes[key] = [comp]
} else {
themes[key].push(comp)
}
})
// 3、写入各自的component.js 文件
Object.entries(themes).forEach(([themeKey, components]) => {
// 抓取每一个 xxx-theme.js文件,每个文件都是一个对象体
let themeObjs = components.map((component) => {
let src = component.content
src = src.replace(/export[\s\S]*\{/, '{')
src = `(${src})`
// eslint-disable-next-line no-eval
return eval(src)
})
// 把对象体的所有键值组合为一个对象
let contents = 'export const concatTheme = {\n'
themeObjs.forEach((obj) => {
Object.keys(obj).forEach((token) => {
contents += ` '${token}': '${obj[token]}',\n`
})
})
contents += '}\n'
fs.writeFileSync(`packages/theme/src/theme/${themeKey}/component.js`, contents)
})
cb()
})
}