-
Notifications
You must be signed in to change notification settings - Fork 474
Expand file tree
/
Copy pathindex.ts
More file actions
128 lines (107 loc) · 3.25 KB
/
index.ts
File metadata and controls
128 lines (107 loc) · 3.25 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
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
import { generateApp, type IAppSchema } from '@opentiny/tiny-engine-dsl-vue'
import * as dslVue from '@opentiny/tiny-engine-dsl-vue'
import { getMergeMeta } from '@opentiny/tiny-engine-meta-register'
import { formatString } from '../../js/ast'
import defaultPrettierConfig from '../../js/config-files/prettierrc'
// 应用出码默认配置
const defaultOptions = {
pluginConfig: {
template: {},
block: {},
page: {},
dataSource: {},
dependencies: {},
globalState: {},
i18n: {},
router: {},
utils: {},
formatCode: {
// 默认格式化配置
...defaultPrettierConfig
},
parseSchema: {}
}
}
// 应用出码
const generateAppCode = async (appSchema: IAppSchema, options = {}) => {
const enableTailwindCSS = getMergeMeta('engine.config')?.enableTailwindCSS
const instance = generateApp({
...defaultOptions,
pluginConfig: {
...defaultOptions.pluginConfig,
template: { ...defaultOptions.pluginConfig.template, enableTailwindCSS }
},
...options
})
return instance.generate(appSchema)
}
// 页面出码
const { parseRequiredBlocks, genSFCWithDefaultPlugin } = dslVue as any
const generatePageCode = (...args: any[]) => {
return formatString(genSFCWithDefaultPlugin(...args), 'vue')
}
/**
* 获取所有嵌套的区块 schema
* @param {*} pageSchema 页面 schema
* @param {*} fetchBlockSchemaApi 获取 blockSchema 的 api
* @param {*} blockSet 已经获取的区块 set,默认不需要传
* @returns
*/
const getAllNestedBlocksSchema = async (pageSchema: any, fetchBlockSchemaApi: any, blockSet = new Set<string>()) => {
const res: any[] = []
const blockNames = parseRequiredBlocks(pageSchema)
const promiseList = blockNames
.filter((name: string) => {
if (blockSet.has(name)) {
return false
}
blockSet.add(name)
return true
})
.map((name: any) => fetchBlockSchemaApi(name))
const schemaList = await Promise.allSettled(promiseList)
const extraList: any[] = []
schemaList.forEach((item) => {
if (item.status !== 'fulfilled') {
return
}
const blockItem = (item.value as any[])?.[0]
if (!blockItem) {
return
}
const historyId = blockItem?.current_history
const historySchema = blockItem?.histories?.find?.((historyItem: any) => historyItem?.id === historyId)
let schemaContent = null
if (historyId && historySchema?.content) {
schemaContent = historySchema.content
} else {
schemaContent = blockItem?.content
}
if (!schemaContent) {
return
}
// 区块 schema 中加上当前版本号,让后续数据支持缓存或更丰富的操作
schemaContent.version = historyId || ''
// 区块子依赖
schemaContent.subBlockDeps = blockNames
res.push(schemaContent)
extraList.push(getAllNestedBlocksSchema(schemaContent, fetchBlockSchemaApi, blockSet))
})
;(await Promise.allSettled(extraList)).forEach((item) => {
if (item.status === 'fulfilled' && item.value) {
res.push(...item.value)
}
})
return res
}
export const GenerateCodeService = {
id: 'engine.service.generateCode',
type: 'MetaService',
options: {},
apis: {
parseRequiredBlocks,
getAllNestedBlocksSchema,
generatePageCode,
generateAppCode
}
}