forked from opentiny/tiny-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplitMaterials.mjs
More file actions
60 lines (46 loc) · 1.78 KB
/
Copy pathsplitMaterials.mjs
File metadata and controls
60 lines (46 loc) · 1.78 KB
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
import fs from 'fs-extra'
import path from 'node:path'
import Logger from './logger.mjs'
const logger = new Logger('splitMaterials')
// 物料资产包mock数据路径
const bundlePath = path.join(process.cwd(), '/packages/design-core/public/mock/bundle.json')
// 物料文件存放文件夹名称
const materialsDir = 'materials'
const bundle = fs.readJSONSync(bundlePath)
const { components, snippets, blocks } = bundle.data.materials
const capitalize = (str) => `${str.charAt(0).toUpperCase()}${str.slice(1)}`
const toPascalCase = (str) => str.split('-').map(capitalize).join('')
/**
* 将物料资产包拆分为单个组件
*/
const splitMaterials = () => {
try {
components.forEach((comp) => {
snippets.some((child) => {
const snippet = child.children.find((item) => {
if (Array.isArray(comp.component)) {
return toPascalCase(comp.component[0]) === toPascalCase(item.snippetName)
}
return toPascalCase(comp.component) === toPascalCase(item.snippetName)
})
if (snippet) {
comp.snippets = [snippet]
comp.category = child.group
return true
}
return false
})
const fileName = Array.isArray(comp.component) ? comp.component[0] : comp.component
const componentPath = path.join(process.cwd(), materialsDir, 'components', `${toPascalCase(fileName)}.json`)
fs.outputJsonSync(componentPath, comp, { spaces: 2 })
})
blocks.forEach((block) => {
const blockPath = path.join(process.cwd(), materialsDir, 'blocks', `${block.label}.json`)
fs.outputJsonSync(blockPath, block, { spaces: 2 })
})
logger.success('拆分物料资产包完成')
} catch (error) {
logger.error(`拆分物料资产包失败: ${error}`)
}
}
splitMaterials()