Skip to content

Commit 21c0524

Browse files
committed
修复重复生成问题
1 parent 3f69b8f commit 21c0524

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "op-scaffold",
33
"productName": "op-scaffold",
4-
"version": "0.1.2",
4+
"version": "0.1.3",
55
"author": "ZhangBohan <[email protected]>",
66
"description": "An Electron scaffold project",
77
"license": null,

src/renderer/utils/d2Crud.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,13 @@ export default (sourceDir, dataConfig, fields) => {
4444
const routes = []
4545

4646
const addRouters = template => {
47-
routes.push(` {
47+
routes.push({ name: `${name}-${template.type}`, template })
48+
}
49+
50+
let routesLines = []
51+
52+
const addRouterLines = template => {
53+
routesLines.push(` {
4854
path: '${template.path}',
4955
name: '${name}-${template.type}',
5056
component: () => import('@/pages/${name}/${template.type}'),
@@ -90,9 +96,36 @@ export default (sourceDir, dataConfig, fields) => {
9096
if (err) {}
9197

9298
const lines = data.split('\n')
93-
console.log('lines', lines)
9499
let count = 0
95100
let codeIndex = 0
101+
102+
console.log('routes', routes)
103+
104+
// 删除无需重复生成的route
105+
lines.forEach(line => {
106+
if (line) {
107+
const trimedLine = line.replace(/\s/g, '')
108+
if (trimedLine.startsWith('name:\'')) {
109+
// 取得route name
110+
const routeName = trimedLine.substring('name:\''.length, trimedLine.lastIndexOf('\''))
111+
// 查找待生成的route列表中是否存在该route,存在则删除列表中的对应route
112+
const index = routes.map(route => route.name).indexOf(routeName)
113+
if (index !== -1) {
114+
routes.splice(index, 1)
115+
}
116+
}
117+
}
118+
})
119+
console.log('routes', routes)
120+
if (routes.length === 0) {
121+
return
122+
}
123+
124+
routes.forEach(route => {
125+
addRouterLines(route.template)
126+
})
127+
128+
// 倒查']',以便找到写入新代码地点
96129
for (let i = lines.length; i > 0; i--) {
97130
const line = lines[i]
98131
if (line && line.endsWith(']')) {
@@ -108,7 +141,7 @@ export default (sourceDir, dataConfig, fields) => {
108141
if (!line.endsWith(',')) {
109142
lines[codeIndex] = line + ','
110143
}
111-
const newLines = lines.slice(0, codeIndex + 1).concat(routes).concat(lines.slice(codeIndex + 1))
144+
const newLines = lines.slice(0, codeIndex + 1).concat(routesLines).concat(lines.slice(codeIndex + 1))
112145
fs.writeFile(routesPath, newLines.join('\n'), (err) => {
113146
console.log(`write ${routesPath}. error: ${err}`)
114147
})

0 commit comments

Comments
 (0)