ohos-router 1.0.7 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/router.ts +426 -428
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ohos-router",
3
- "version": "1.0.7",
3
+ "version": "1.0.8",
4
4
  "description": "鸿蒙路由管理插件",
5
5
  "main": "main.js",
6
6
  "scripts": {
package/router.ts CHANGED
@@ -1,487 +1,485 @@
1
- import { HvigorNode } from '@ohos/hvigor';
2
- import { readFileSync, writeFileSync,existsSync } from 'fs'
1
+ import {HvigorNode} from '@ohos/hvigor';
2
+ import {readFileSync, writeFileSync, existsSync} from 'fs'
3
3
 
4
4
  let entryModulePath = ""
5
- let entryUpperCaseNameArray:string[] = []
6
- let entryIsHsp:boolean[] = []
5
+ let entryUpperCaseNameArray: string[] = []
6
+ let entryIsHsp: boolean[] = []
7
7
 
8
8
  export function pluginStart(node: HvigorNode, isExecute: boolean) {
9
- if (!isExecute) {
10
- return
11
- }
9
+ if (!isExecute) {
10
+ return
11
+ }
12
12
 
13
- entryUpperCaseNameArray = []
14
- entryIsHsp = []
15
- node.subNodes((childNode: HvigorNode) => {
16
- try {
17
- let hvigorfilePath = childNode.getNodePath() + "\\hvigorfile.ts"
18
- let hvigorfileData = readFileSync(hvigorfilePath, 'utf-8');
19
- let isHsp = hvigorfileData.indexOf("hspTasks") != -1
20
- let isHap = hvigorfileData.indexOf("hapTasks") != -1
21
- let nodeName = childNode.getNodeName()
22
- let upperCaseName = nodeName.substring(0, 1).toUpperCase() + nodeName.substring(1, nodeName.length)
23
- //默认Entry不追加
24
- if ("Entry" != upperCaseName) {
25
- entryUpperCaseNameArray.push(upperCaseName)
26
- entryIsHsp.push(isHsp)
27
- }
28
- let configPath = childNode.getNodePath() + "\\" + upperCaseName + "RouterConfig.ets"
29
-
30
- if (!existsSync(configPath)) {
13
+ entryUpperCaseNameArray = []
14
+ entryIsHsp = []
15
+ node.subNodes((childNode: HvigorNode) => {
31
16
  try {
32
- writeFileSync(configPath, getRouterConfig(upperCaseName, nodeName, isHsp));
33
- } catch (error) {
34
- log(JSON.stringify(error))
35
- }
36
- if (!isHap) {
37
- let indexFilePath = childNode.getNodePath() + "\\" + "Index.ets"
38
- let indexFileData = readFileSync(indexFilePath, 'utf-8');
39
- if (indexFileData.indexOf(upperCaseName) == -1) {
40
- indexFileData = "export { " + upperCaseName +
41
- "RouterConfig } from './" + upperCaseName +
42
- "RouterConfig'\n" + indexFileData
43
- writeFileSync(indexFilePath, indexFileData);
44
- }
45
- }
46
- }
47
- if (isHap) {
48
- entryModulePath = childNode.getNodePath()
49
- writeBuildProfile(childNode)
50
- }
51
- let fileList = childNode.getNodeDir()
52
- .asFileList()
53
- .filter(item => {
54
- return item.getPath().indexOf("build") == -1
55
- && item.getPath().indexOf("oh_modules") == -1
56
- && item.getPath().indexOf("src\\main") != -1
57
- && item.getPath().endsWith(".ets")
58
- })
59
-
60
- fileList.forEach((file) => {
61
- let data = readFileSync(file.filePath, 'utf-8');
62
- if (data.indexOf("@RouterPath") != -1) {
63
- let routerPathEnd = data.split("@RouterPath")[1]
64
- let routerName = ""
65
- if (routerPathEnd.startsWith("(\"")) {
66
- routerName = routerPathEnd.substring(2, routerPathEnd.indexOf(")") - 1)
67
- } else {
68
- routerName = routerPathEnd.substring(1, routerPathEnd.indexOf(")"))
69
- }
70
- var importPath = "./src" + file.filePath.split("src")[1]
71
- importPath = importPath.replace(".ets", "").replaceAll("\\", "/")
72
- let viewName = importPath.substring(importPath.lastIndexOf("/") + 1, importPath.length)
73
- let fileData = readFileSync(configPath, 'utf-8')
74
- if (fileData.indexOf(routerName) == -1) {
75
- let switchTag = "switch (builderName) {"
76
- let switchPosition = fileData.indexOf(switchTag) + switchTag.length
77
-
78
- let switchBuilder = ""
79
-
80
- if (routerPathEnd.startsWith("(\"")) {
81
- switchBuilder = "\n case \"" + routerName + "\":\n" +
82
- " import(\"" + importPath + "\")\n" +
83
- " break"
84
- } else {
85
- switchBuilder = "\n case " + routerName + ":\n" +
86
- " import(\"" + importPath + "\")\n" +
87
- " break"
17
+ let hvigorfilePath = childNode.getNodePath() + "\\hvigorfile.ts"
18
+ let hvigorfileData = readFileSync(hvigorfilePath, 'utf-8');
19
+ let isHsp = hvigorfileData.indexOf("hspTasks") != -1
20
+ let isHap = hvigorfileData.indexOf("hapTasks") != -1
21
+ let nodeName = childNode.getNodeName()
22
+ let upperCaseName = nodeName.substring(0, 1).toUpperCase() + nodeName.substring(1, nodeName.length)
23
+ //默认Entry不追加
24
+ if ("Entry" != upperCaseName) {
25
+ entryUpperCaseNameArray.push(upperCaseName)
26
+ entryIsHsp.push(isHsp)
88
27
  }
89
-
90
- let configEnd = insertString(fileData, switchPosition, switchBuilder)
91
-
92
- let destinationEnd = ""
93
- let DestinationTag = "NavDestination() {"
94
- let destinationPosition = configEnd.indexOf(DestinationTag) + DestinationTag.length
95
-
96
- let destinationBuilder = ""
97
- if (routerPathEnd.startsWith("(\"")) {
98
- destinationBuilder = "\n if (routerGetBuilderType() == \"" + routerName + "\") {\n" +
99
- " " + viewName + "()\n" +
100
- " } "
101
- } else {
102
- destinationBuilder = "\n if (routerGetBuilderType() == " + routerName + ") {\n" +
103
- " " + viewName + "()\n" +
104
- " } "
28
+ let configPath = childNode.getNodePath() + "\\" + upperCaseName + "RouterConfig.ets"
29
+
30
+ if (!existsSync(configPath)) {
31
+ try {
32
+ writeFileSync(configPath, getRouterConfig(upperCaseName, nodeName, isHsp));
33
+ } catch (error) {
34
+ log(JSON.stringify(error))
35
+ }
36
+ if (!isHap) {
37
+ let indexFilePath = childNode.getNodePath() + "\\" + "Index.ets"
38
+ let indexFileData = readFileSync(indexFilePath, 'utf-8');
39
+ if (indexFileData.indexOf(upperCaseName) == -1) {
40
+ indexFileData = "export { " + upperCaseName +
41
+ "RouterConfig } from './" + upperCaseName +
42
+ "RouterConfig'\n" + indexFileData
43
+ writeFileSync(indexFilePath, indexFileData);
44
+ }
45
+ }
105
46
  }
106
-
107
- if (configEnd.indexOf("routerGetBuilderType()") != -1) {
108
- destinationBuilder = destinationBuilder + "else "
47
+ if (isHap) {
48
+ entryModulePath = childNode.getNodePath()
49
+ writeBuildProfile(childNode)
109
50
  }
110
- destinationEnd = insertString(configEnd, destinationPosition, destinationBuilder)
51
+ let fileList = childNode.getNodeDir()
52
+ .asFileList()
53
+ .filter(item => {
54
+ return item.getPath().indexOf("build") == -1
55
+ && item.getPath().indexOf("oh_modules") == -1
56
+ && item.getPath().indexOf("src\\main") != -1
57
+ && item.getPath().endsWith(".ets")
58
+ })
59
+
60
+ fileList.forEach((file) => {
61
+ let data = readFileSync(file.filePath, 'utf-8');
62
+ if (data.indexOf("@RouterPath") != -1) {
63
+ let routerPathEnd = data.split("@RouterPath")[1]
64
+ let routerName = ""
65
+ if (routerPathEnd.startsWith("(\"")) {
66
+ routerName = routerPathEnd.substring(2, routerPathEnd.indexOf(")") - 1)
67
+ } else {
68
+ routerName = routerPathEnd.substring(1, routerPathEnd.indexOf(")"))
69
+ }
70
+ var importPath = "./src" + file.filePath.split("src")[1]
71
+ importPath = importPath.replace(".ets", "").replaceAll("\\", "/")
72
+ let viewName = importPath.substring(importPath.lastIndexOf("/") + 1, importPath.length)
73
+ let fileData = readFileSync(configPath, 'utf-8')
74
+ if (fileData.indexOf(routerName) == -1) {
75
+ let switchTag = "switch (builderName) {"
76
+ let switchPosition = fileData.indexOf(switchTag) + switchTag.length
77
+
78
+ let switchBuilder = ""
79
+
80
+ if (routerPathEnd.startsWith("(\"")) {
81
+ switchBuilder = "\n case \"" + routerName + "\":\n" +
82
+ " import(\"" + importPath + "\")\n" +
83
+ " break"
84
+ } else {
85
+ switchBuilder = "\n case " + routerName + ":\n" +
86
+ " import(\"" + importPath + "\")\n" +
87
+ " break"
88
+ }
89
+
90
+ let configEnd = insertString(fileData, switchPosition, switchBuilder)
91
+
92
+ let destinationEnd = ""
93
+ let DestinationTag = "NavDestinationView() {"
94
+ let destinationPosition = configEnd.indexOf(DestinationTag) + DestinationTag.length
95
+
96
+ let destinationBuilder = ""
97
+ if (routerPathEnd.startsWith("(\"")) {
98
+ destinationBuilder = "\n if (routerGetBuilderType() == \"" + routerName + "\") {\n" +
99
+ " " + viewName + "()\n" +
100
+ " } "
101
+ } else {
102
+ destinationBuilder = "\n if (routerGetBuilderType() == " + routerName + ") {\n" +
103
+ " " + viewName + "()\n" +
104
+ " } "
105
+ }
106
+
107
+ if (configEnd.indexOf("routerGetBuilderType()") != -1) {
108
+ destinationBuilder = destinationBuilder + "else "
109
+ }
110
+ destinationEnd = insertString(configEnd, destinationPosition, destinationBuilder)
111
+
112
+ let importContent = ""
113
+ if (fileData.indexOf(importPath) == -1) {
114
+ importContent = "import { " + viewName +
115
+ " } from '" + importPath + "';\n"
116
+ }
117
+ let className = routerName.split(".")[0]
118
+ if (!routerPathEnd.startsWith("(\"") && fileData.indexOf("./" + className) == -1) {
119
+ importContent = importContent + "import { " + className +
120
+ " } from './" + className + "';\n"
121
+ }
122
+ destinationEnd = importContent + destinationEnd
123
+
124
+ writeFileSync(configPath, destinationEnd);
125
+ }
126
+ }
127
+ })
111
128
 
112
- let importContent = ""
113
- if (fileData.indexOf(importPath) == -1) {
114
- importContent = "import { " + viewName +
115
- " } from '" + importPath + "';\n"
116
- }
117
- let className = routerName.split(".")[0]
118
- if (!routerPathEnd.startsWith("(\"") && fileData.indexOf("./" + className) == -1) {
119
- importContent = importContent + "import { " + className +
120
- " } from './" + className + "';\n"
121
- }
122
- destinationEnd = importContent + destinationEnd
129
+ } catch (error) {
130
+ console.log(error)
131
+ }
132
+ })
123
133
 
124
- writeFileSync(configPath, destinationEnd);
125
- }
134
+ try {
135
+ let moduleJson5Path = entryModulePath + "\\src\\main\\module.json5"
136
+ let moduleJson5Data = readFileSync(moduleJson5Path, 'utf-8');
137
+ moduleJson5JSONParse(moduleJson5Data)
138
+ let appPath = entryModulePath + "\\src\\main\\ets\\App.ets"
139
+ if (moduleSrcEntry == undefined) {
140
+ writeFileSync(appPath, getApp());
141
+ let moduleTag = "\"module\": {"
142
+ let modulePosition = moduleJson5Data.indexOf(moduleTag) + moduleTag.length
143
+ let mJson = "\n \"srcEntry\": \"./ets/App.ets\","
144
+ let moduleContent = insertString(moduleJson5Data, modulePosition, mJson)
145
+ writeFileSync(moduleJson5Path, moduleContent);
146
+ } else {
147
+ let endSrc = moduleSrcEntry.substring(1, moduleSrcEntry.length).replaceAll("/", "\\")
148
+ let endAppPath = entryModulePath + "\\src\\main" + endSrc
149
+ let appFileData = readFileSync(endAppPath, 'utf-8');
150
+ if (appFileData.indexOf("routerInitConfig([") != -1) {
151
+ let initHaveConfig = ""
152
+ let importHaveConfig = ""
153
+ let isHaveConfig = false;
154
+ entryUpperCaseNameArray.forEach((name, index) => {
155
+ let isHsp = entryIsHsp[index]
156
+ let endHsp = ""
157
+ if (isHsp) {
158
+ endHsp = "getARouter()"
159
+ }
160
+
161
+ if (appFileData.indexOf(name) == -1) {
162
+ isHaveConfig = true;
163
+ initHaveConfig = initHaveConfig + (" new " + name + "RouterConfig(" + endHsp + "),\n")
164
+ dependenciesKey.forEach((item, index) => {
165
+ if (item.indexOf(name.toLowerCase()) != -1) {
166
+ importHaveConfig = importHaveConfig + "\nimport { " + name +
167
+ "RouterConfig } from '" + item + "';\n"
168
+ }
169
+ })
170
+ }
171
+ })
172
+
173
+ if (isHaveConfig) {
174
+ let routerInitHaveTag = "routerInitConfig(["
175
+ let routerInitHavePosition = appFileData.indexOf(routerInitHaveTag) + routerInitHaveTag.length
176
+ let routerHaveJson = "\n" + initHaveConfig
177
+ let routerInitHaveContent = insertString(appFileData, routerInitHavePosition, routerHaveJson)
178
+ routerInitHaveContent = importHaveConfig + routerInitHaveContent
179
+ writeFileSync(endAppPath, routerInitHaveContent);
180
+ }
181
+ } else {
182
+ let initNewConfig = ""
183
+ let importNewConfig = ""
184
+ entryUpperCaseNameArray.forEach((name, index) => {
185
+ let isHsp = entryIsHsp[index]
186
+ let endHsp = ""
187
+ if (isHsp) {
188
+ endHsp = "getARouter()"
189
+ }
190
+ initNewConfig = initNewConfig + (" new " + name + "RouterConfig(" + endHsp + "),\n")
191
+ dependenciesKey.forEach((item, index) => {
192
+ if (item.indexOf(name.toLowerCase()) != -1) {
193
+ importNewConfig = importNewConfig + "\nimport { " + name +
194
+ "RouterConfig } from '" + item + "';\n"
195
+ }
196
+ })
197
+ })
198
+
199
+ let routerInitTag = "onCreate(): void {"
200
+ let routerInitPosition = appFileData.indexOf(routerInitTag) + routerInitTag.length
201
+ let routerJson = "\n routerInitConfig([\n" +
202
+ initNewConfig +
203
+ " new EntryRouterConfig()\n" +
204
+ " ])\n"
205
+ let routerInitContent = insertString(appFileData, routerInitPosition, routerJson)
206
+ routerInitContent = "import { getARouter,routerInitConfig } from '@abner/router';\n" +
207
+ "import { EntryRouterConfig } from '../../../EntryRouterConfig';\n"
208
+ + importNewConfig
209
+ + routerInitContent
210
+ writeFileSync(endAppPath, routerInitContent);
211
+ }
126
212
  }
127
- })
128
213
 
129
214
  } catch (error) {
130
- console.log(error)
131
- }
132
- })
133
-
134
- try {
135
- let moduleJson5Path = entryModulePath + "\\src\\main\\module.json5"
136
- let moduleJson5Data = readFileSync(moduleJson5Path, 'utf-8');
137
- moduleJson5JSONParse(moduleJson5Data)
138
- let appPath = entryModulePath + "\\src\\main\\ets\\App.ets"
139
- if (moduleSrcEntry == undefined) {
140
- writeFileSync(appPath, getApp());
141
- let moduleTag = "\"module\": {"
142
- let modulePosition = moduleJson5Data.indexOf(moduleTag) + moduleTag.length
143
- let mJson = "\n \"srcEntry\": \"./ets/App.ets\","
144
- let moduleContent = insertString(moduleJson5Data, modulePosition, mJson)
145
- writeFileSync(moduleJson5Path, moduleContent);
146
- } else {
147
- let endSrc = moduleSrcEntry.substring(1, moduleSrcEntry.length).replaceAll("/", "\\")
148
- let endAppPath = entryModulePath + "\\src\\main" + endSrc
149
- let appFileData = readFileSync(endAppPath, 'utf-8');
150
- if (appFileData.indexOf("routerInitConfig([") != -1) {
151
- let initHaveConfig = ""
152
- let importHaveConfig = ""
153
- let isHaveConfig = false;
154
- entryUpperCaseNameArray.forEach((name, index) => {
155
- let isHsp = entryIsHsp[index]
156
- let endHsp = ""
157
- if (isHsp) {
158
- endHsp = "getARouter()"
159
- }
160
-
161
- if (appFileData.indexOf(name) == -1) {
162
- isHaveConfig = true;
163
- initHaveConfig = initHaveConfig + (" new " + name + "RouterConfig(" + endHsp + "),\n")
164
- dependenciesKey.forEach((item, index) => {
165
- if (item.indexOf(name.toLowerCase()) != -1) {
166
- importHaveConfig = importHaveConfig + "\nimport { " + name +
167
- "RouterConfig } from '" + item + "';\n"
168
- }
169
- })
170
- }
171
- })
172
215
 
173
- if (isHaveConfig) {
174
- let routerInitHaveTag = "routerInitConfig(["
175
- let routerInitHavePosition = appFileData.indexOf(routerInitHaveTag) + routerInitHaveTag.length
176
- let routerHaveJson = "\n" + initHaveConfig
177
- let routerInitHaveContent = insertString(appFileData, routerInitHavePosition, routerHaveJson)
178
- routerInitHaveContent = importHaveConfig + routerInitHaveContent
179
- writeFileSync(endAppPath, routerInitHaveContent);
180
- }
181
- } else {
182
- let initNewConfig = ""
183
- let importNewConfig = ""
184
- entryUpperCaseNameArray.forEach((name, index) => {
185
- let isHsp = entryIsHsp[index]
186
- let endHsp = ""
187
- if (isHsp) {
188
- endHsp = "getARouter()"
189
- }
190
- initNewConfig = initNewConfig + (" new " + name + "RouterConfig(" + endHsp + "),\n")
191
- dependenciesKey.forEach((item, index) => {
192
- if (item.indexOf(name.toLowerCase()) != -1) {
193
- importNewConfig = importNewConfig + "\nimport { " + name +
194
- "RouterConfig } from '" + item + "';\n"
195
- }
196
- })
197
- })
198
-
199
- let routerInitTag = "onCreate(): void {"
200
- let routerInitPosition = appFileData.indexOf(routerInitTag) + routerInitTag.length
201
- let routerJson = "\n routerInitConfig([\n" +
202
- initNewConfig +
203
- " new EntryRouterConfig()\n" +
204
- " ])\n"
205
- let routerInitContent = insertString(appFileData, routerInitPosition, routerJson)
206
- routerInitContent = "import { getARouter,routerInitConfig } from '@abner/router';\n" +
207
- "import { EntryRouterConfig } from '../../../EntryRouterConfig';\n"
208
- + importNewConfig
209
- + routerInitContent
210
- writeFileSync(endAppPath, routerInitContent);
211
- }
212
216
  }
213
217
 
214
- } catch (error) {
215
-
216
- }
217
-
218
218
  }
219
219
 
220
- let moduleSrcEntry:string
221
-
222
- function moduleJson5JSONParse(moduleJson5Data:string) {
223
- try {
224
- let moduleJsonBean = JSON.parse(moduleJson5Data)
225
- let module = moduleJsonBean.module
226
- moduleSrcEntry = module.srcEntry
227
- } catch (error) {
228
- let errorArray = error.message.split(" ")
229
- let position = Number(errorArray[errorArray.length-1])
230
- let endString = moduleJson5Data.substring(0, position)
231
- const lastIndex = endString.lastIndexOf(",")
232
- let replacedStr = replaceCharUsingSubstring(endString, lastIndex, '');
233
- moduleJson5JSONParse(replacedStr + moduleJson5Data.substring(position, moduleJson5Data.length))
234
- }
220
+ let moduleSrcEntry: string
221
+
222
+ function moduleJson5JSONParse(moduleJson5Data: string) {
223
+ try {
224
+ let moduleJsonBean = JSON.parse(moduleJson5Data)
225
+ let module = moduleJsonBean.module
226
+ moduleSrcEntry = module.srcEntry
227
+ } catch (error) {
228
+ let errorArray = error.message.split(" ")
229
+ let position = Number(errorArray[errorArray.length - 1])
230
+ let endString = moduleJson5Data.substring(0, position)
231
+ const lastIndex = endString.lastIndexOf(",")
232
+ let replacedStr = replaceCharUsingSubstring(endString, lastIndex, '');
233
+ moduleJson5JSONParse(replacedStr + moduleJson5Data.substring(position, moduleJson5Data.length))
234
+ }
235
235
 
236
236
  }
237
237
 
238
238
  function insertString(src: string, pos: number, val: string): string {
239
- if (pos < -src.length - 1 || pos > src.length) {
240
- throw "insert position is error";
241
- }
242
- if (pos >= 0) {
243
- return src.slice(0, pos) + val + src.slice(pos);
244
- } else {
245
- return src.slice(0, src.length + 1 + pos) + val + src.slice(src.length + 1 + pos);
246
- }
239
+ if (pos < -src.length - 1 || pos > src.length) {
240
+ throw "insert position is error";
241
+ }
242
+ if (pos >= 0) {
243
+ return src.slice(0, pos) + val + src.slice(pos);
244
+ } else {
245
+ return src.slice(0, src.length + 1 + pos) + val + src.slice(src.length + 1 + pos);
246
+ }
247
247
  }
248
248
 
249
249
  function getRouterConfig(upperCaseName: string, nodeName: string, isHsp: boolean) {
250
- var configText = "import { ";
251
- if (isHsp) {
252
- configText = configText + " ARouter, setARouter, "
253
- }
254
- configText = configText + "RouterConfig,routerGetBuilderType, routerInitBuilder } from '@abner/router';\n" +
255
- "\n" +
256
- "/**\n" +
257
- " * AUTHOR:AbnerMing\n" +
258
- " * INTRODUCE:动态配置路径\n" +
259
- " * */\n" +
260
- "function importPath(builderName: string) {\n" +
261
- " switch (builderName) {\n" +
262
- "\n" +
263
- " }\n" +
264
- "}\n" +
265
- "\n" +
266
- "/**\n" +
267
- " * AUTHOR:AbnerMing\n" +
268
- " * INTRODUCE:动态配置组件\n" +
269
- " * */\n" +
270
- "@Builder\n" +
271
- "export function viewBuilder() {\n" +
272
- " NavDestination() {\n" +
273
- "\n" +
274
- " }\n" +
275
- " .hideTitleBar(true)\n" +
276
- " .height('100%')\n" +
277
- "\n" +
278
- "}\n" +
279
- "\n" +
280
- "\n" +
281
- "/**\n" +
282
- " * AUTHOR:AbnerMing\n" +
283
- " * DATE:" + getNowTime() + "\n" +
284
- " * INTRODUCE:路由配置,此路由配置文件为自动生成,无特殊情况下请无须改动\n" +
285
- " * */\n" +
286
- "export class " + upperCaseName + "RouterConfig implements RouterConfig {\n";
287
-
288
- if (isHsp) {
289
- //是动态包
290
- configText = configText + " constructor(router: ARouter) {\n" +
291
- " setARouter(router)\n" +
292
- " }\n"
293
- }
294
-
295
- configText = configText + " getModuleName(): string {\n" +
296
- " return \"" + nodeName + "\"\n" +
297
- " }\n" +
298
- "\n" +
299
- " async initRouter(builderName: string): Promise<void> {\n" +
300
- " return await new Promise((resolve: Function) => {\n" +
301
- " importPath(builderName)\n" +
302
- " routerInitBuilder(builderName, wrapBuilder(viewBuilder))\n" +
303
- " resolve()\n" +
304
- " })\n" +
305
- " }\n" +
306
- "}"
307
-
308
-
309
- return configText
250
+ var configText = "import { ";
251
+ if (isHsp) {
252
+ configText = configText + " ARouter, setARouter, "
253
+ }
254
+ configText = configText + "RouterConfig,routerGetBuilderType, routerInitBuilder,NavDestinationView } from '@abner/router';\n" +
255
+ "\n" +
256
+ "/**\n" +
257
+ " * AUTHOR:AbnerMing\n" +
258
+ " * INTRODUCE:动态配置路径\n" +
259
+ " * */\n" +
260
+ "function importPath(builderName: string) {\n" +
261
+ " switch (builderName) {\n" +
262
+ "\n" +
263
+ " }\n" +
264
+ "}\n" +
265
+ "\n" +
266
+ "/**\n" +
267
+ " * AUTHOR:AbnerMing\n" +
268
+ " * INTRODUCE:动态配置组件\n" +
269
+ " * */\n" +
270
+ "@Builder\n" +
271
+ "export function viewBuilder() {\n" +
272
+ " NavDestinationView() {\n" +
273
+ "\n" +
274
+ " }\n" +
275
+ "\n" +
276
+ "}\n" +
277
+ "\n" +
278
+ "\n" +
279
+ "/**\n" +
280
+ " * AUTHOR:AbnerMing\n" +
281
+ " * DATE:" + getNowTime() + "\n" +
282
+ " * INTRODUCE:路由配置,此路由配置文件为自动生成,无特殊情况下请无须改动\n" +
283
+ " * */\n" +
284
+ "export class " + upperCaseName + "RouterConfig implements RouterConfig {\n";
285
+
286
+ if (isHsp) {
287
+ //是动态包
288
+ configText = configText + " constructor(router: ARouter) {\n" +
289
+ " setARouter(router)\n" +
290
+ " }\n"
291
+ }
292
+
293
+ configText = configText + " getModuleName(): string {\n" +
294
+ " return \"" + nodeName + "\"\n" +
295
+ " }\n" +
296
+ "\n" +
297
+ " async initRouter(builderName: string): Promise<void> {\n" +
298
+ " return await new Promise((resolve: Function) => {\n" +
299
+ " importPath(builderName)\n" +
300
+ " routerInitBuilder(builderName, wrapBuilder(viewBuilder))\n" +
301
+ " resolve()\n" +
302
+ " })\n" +
303
+ " }\n" +
304
+ "}"
305
+
306
+
307
+ return configText
310
308
  }
311
309
 
312
310
  var buildProfilePath = ""
313
311
 
314
312
  function writeBuildProfile(childNode: HvigorNode) {
315
- let ohPackagePath = childNode.getNodePath() + "\\oh-package.json5"
316
- let ohPackageData = readFileSync(ohPackagePath, 'utf-8');
317
- dependenciesJSONParse(ohPackageData)
318
- buildProfilePath = childNode.getNodePath() + "\\build-profile.json5"
319
- let buildProfileData = readFileSync(buildProfilePath, 'utf-8');
320
- buildProfileJSONParse(buildProfileData)
313
+ let ohPackagePath = childNode.getNodePath() + "\\oh-package.json5"
314
+ let ohPackageData = readFileSync(ohPackagePath, 'utf-8');
315
+ dependenciesJSONParse(ohPackageData)
316
+ buildProfilePath = childNode.getNodePath() + "\\build-profile.json5"
317
+ let buildProfileData = readFileSync(buildProfilePath, 'utf-8');
318
+ buildProfileJSONParse(buildProfileData)
321
319
  }
322
320
 
323
321
  function getNowTime() {
324
- let d = new Date();
325
- return d.getFullYear() + "年" + (d.getMonth() + 1) + "月" + d.getDate() + "日 " + d.getHours() + "时" +
326
- d.getMinutes() +
327
- "分" + d.getSeconds() + "秒"
322
+ let d = new Date();
323
+ return d.getFullYear() + "年" + (d.getMonth() + 1) + "月" + d.getDate() + "日 " + d.getHours() + "时" +
324
+ d.getMinutes() +
325
+ "分" + d.getSeconds() + "秒"
328
326
  }
329
327
 
330
- function log(params:string) {
331
- console.error(params);
328
+ function log(params: string) {
329
+ console.error(params);
332
330
  }
333
331
 
334
332
  let dependenciesKey: string[]
335
333
 
336
334
  function dependenciesJSONParse(ohPackageData: string) {
337
- try {
338
- dependenciesKey = []
339
- let ohPackAgeBean = JSON.parse(ohPackageData)
340
- let dependencies = ohPackAgeBean.dependencies
341
- for (var key in dependencies) {
342
- if (dependencies[key].indexOf("file:..") != -1) {
343
- dependenciesKey.push(key)
344
- }
335
+ try {
336
+ dependenciesKey = []
337
+ let ohPackAgeBean = JSON.parse(ohPackageData)
338
+ let dependencies = ohPackAgeBean.dependencies
339
+ for (var key in dependencies) {
340
+ if (dependencies[key].indexOf("file:..") != -1) {
341
+ dependenciesKey.push(key)
342
+ }
343
+ }
344
+ } catch (error) {
345
+ let errorArray = error.message.split(" ")
346
+ let position = Number(errorArray[errorArray.length - 1])
347
+ let endString = ohPackageData.substring(0, position)
348
+ const lastIndex = endString.lastIndexOf(",")
349
+ let replacedStr = replaceCharUsingSubstring(endString, lastIndex, '');
350
+ dependenciesJSONParse(replacedStr + ohPackageData.substring(position, ohPackageData.length))
345
351
  }
346
- } catch (error) {
347
- let errorArray = error.message.split(" ")
348
- let position = Number(errorArray[errorArray.length-1])
349
- let endString = ohPackageData.substring(0, position)
350
- const lastIndex = endString.lastIndexOf(",")
351
- let replacedStr = replaceCharUsingSubstring(endString, lastIndex, '');
352
- dependenciesJSONParse(replacedStr + ohPackageData.substring(position, ohPackageData.length))
353
- }
354
352
  }
355
353
 
356
- function buildProfileJSONParse(buildProfileData:string) {
357
- try {
358
- let buildProfileBean = JSON.parse(buildProfileData)
359
- let buildOptionBean = buildProfileBean.buildOption
360
- if (buildOptionBean.arkOptions == undefined) {
361
- printBuildProFile(buildProfileData, 0)
362
- } else {
363
- let arkOptions = buildOptionBean.arkOptions
364
- if (arkOptions.runtimeOnly == undefined) {
365
- printBuildProFile(buildProfileData, 1)
366
-
367
- } else {
368
- let runtimeOnly = arkOptions.runtimeOnly
369
- if (runtimeOnly.packages == undefined) {
370
- printBuildProFile(buildProfileData, 2)
354
+ function buildProfileJSONParse(buildProfileData: string) {
355
+ try {
356
+ let buildProfileBean = JSON.parse(buildProfileData)
357
+ let buildOptionBean = buildProfileBean.buildOption
358
+ if (buildOptionBean.arkOptions == undefined) {
359
+ printBuildProFile(buildProfileData, 0)
371
360
  } else {
372
- if (dependenciesKey.toString() != runtimeOnly["packages"].toString()) {
373
- printBuildProFile(buildProfileData, 3)
374
- }
361
+ let arkOptions = buildOptionBean.arkOptions
362
+ if (arkOptions.runtimeOnly == undefined) {
363
+ printBuildProFile(buildProfileData, 1)
364
+
365
+ } else {
366
+ let runtimeOnly = arkOptions.runtimeOnly
367
+ if (runtimeOnly.packages == undefined) {
368
+ printBuildProFile(buildProfileData, 2)
369
+ } else {
370
+ if (dependenciesKey.toString() != runtimeOnly["packages"].toString()) {
371
+ printBuildProFile(buildProfileData, 3)
372
+ }
373
+ }
374
+ }
375
375
  }
376
- }
376
+ } catch (error) {
377
+ let errorArray = error.message.split(" ")
378
+ let position = Number(errorArray[errorArray.length - 1])
379
+ let endString = buildProfileData.substring(0, position)
380
+ const lastIndex = endString.lastIndexOf(",")
381
+ let replacedStr = replaceCharUsingSubstring(endString, lastIndex, '');
382
+ buildProfileJSONParse(replacedStr + buildProfileData.substring(position, buildProfileData.length))
377
383
  }
378
- } catch (error) {
379
- let errorArray = error.message.split(" ")
380
- let position = Number(errorArray[errorArray.length-1])
381
- let endString = buildProfileData.substring(0, position)
382
- const lastIndex = endString.lastIndexOf(",")
383
- let replacedStr = replaceCharUsingSubstring(endString, lastIndex, '');
384
- buildProfileJSONParse(replacedStr + buildProfileData.substring(position, buildProfileData.length))
385
- }
386
384
  }
387
385
 
388
386
  function printBuildProFile(buildProfileData: string, type: number) {
389
- let endArray = ""
390
- dependenciesKey.forEach((item, index) => {
391
- if (index == dependenciesKey.length - 1) {
392
- endArray = endArray + " \"" + item + "\"\n"
393
- } else {
394
- endArray = endArray + " \"" + item + "\",\n"
387
+ let endArray = ""
388
+ dependenciesKey.forEach((item, index) => {
389
+ if (index == dependenciesKey.length - 1) {
390
+ endArray = endArray + " \"" + item + "\"\n"
391
+ } else {
392
+ endArray = endArray + " \"" + item + "\",\n"
393
+ }
394
+ })
395
+
396
+ if (endArray == "") {
397
+ return
398
+ }
399
+
400
+ let buildProfileContent = ""
401
+ if (type == 0) {
402
+ let buildProfileTag = "\"buildOption\": {"
403
+ let buildProfilePosition = buildProfileData.indexOf(buildProfileTag) + buildProfileTag.length
404
+ let buildProfileBuilder = "\n \"arkOptions\": {\n" +
405
+ " \"runtimeOnly\": {\n" +
406
+ " \"sources\": [\n" +
407
+ " ],\n" +
408
+ " \"packages\": [\n" +
409
+ endArray +
410
+ " ]\n" +
411
+ " }\n" +
412
+ " }"
413
+
414
+ buildProfileContent = insertString(buildProfileData, buildProfilePosition, buildProfileBuilder)
415
+ } else if (type == 1) {
416
+ let buildProfileTag = " \"buildOption\": {\n" +
417
+ " \"arkOptions\": {"
418
+ let buildProfilePosition = buildProfileData.indexOf(buildProfileTag) + buildProfileTag.length
419
+ let buildProfileBuilder = "\n \"runtimeOnly\": {\n" +
420
+ " \"sources\": [\n" +
421
+ " ],\n" +
422
+ " \"packages\": [\n" +
423
+ endArray +
424
+ " ]\n" +
425
+ " }"
426
+ buildProfileContent = insertString(buildProfileData, buildProfilePosition, buildProfileBuilder)
427
+ } else if (type == 2) {
428
+ let buildProfileTag = "\"runtimeOnly\": {"
429
+ let buildProfilePosition = buildProfileData.indexOf(buildProfileTag) + buildProfileTag.length
430
+ let buildProfileBuilder = "\n \"packages\": [\n" +
431
+ endArray +
432
+ " ]\n"
433
+ buildProfileContent = insertString(buildProfileData, buildProfilePosition, buildProfileBuilder)
434
+ } else if (type == 3) {
435
+ let buildProfileTag = "\"packages\": ["
436
+ let buildProfilePosition = buildProfileData.indexOf(buildProfileTag) + buildProfileTag.length
437
+ let buildProfileBuilder = "\n" + endArray
438
+ buildProfileContent = insertString(buildProfileData, buildProfilePosition, buildProfileBuilder)
395
439
  }
396
- })
397
-
398
- if (endArray == "") {
399
- return
400
- }
401
-
402
- let buildProfileContent = ""
403
- if (type == 0) {
404
- let buildProfileTag = "\"buildOption\": {"
405
- let buildProfilePosition = buildProfileData.indexOf(buildProfileTag) + buildProfileTag.length
406
- let buildProfileBuilder = "\n \"arkOptions\": {\n" +
407
- " \"runtimeOnly\": {\n" +
408
- " \"sources\": [\n" +
409
- " ],\n" +
410
- " \"packages\": [\n" +
411
- endArray +
412
- " ]\n" +
413
- " }\n" +
414
- " }"
415
-
416
- buildProfileContent = insertString(buildProfileData, buildProfilePosition, buildProfileBuilder)
417
- } else if (type == 1) {
418
- let buildProfileTag = " \"buildOption\": {\n" +
419
- " \"arkOptions\": {"
420
- let buildProfilePosition = buildProfileData.indexOf(buildProfileTag) + buildProfileTag.length
421
- let buildProfileBuilder = "\n \"runtimeOnly\": {\n" +
422
- " \"sources\": [\n" +
423
- " ],\n" +
424
- " \"packages\": [\n" +
425
- endArray +
426
- " ]\n" +
427
- " }"
428
- buildProfileContent = insertString(buildProfileData, buildProfilePosition, buildProfileBuilder)
429
- } else if (type == 2) {
430
- let buildProfileTag = "\"runtimeOnly\": {"
431
- let buildProfilePosition = buildProfileData.indexOf(buildProfileTag) + buildProfileTag.length
432
- let buildProfileBuilder = "\n \"packages\": [\n" +
433
- endArray +
434
- " ]\n"
435
- buildProfileContent = insertString(buildProfileData, buildProfilePosition, buildProfileBuilder)
436
- } else if (type == 3) {
437
- let buildProfileTag = "\"packages\": ["
438
- let buildProfilePosition = buildProfileData.indexOf(buildProfileTag) + buildProfileTag.length
439
- let buildProfileBuilder = "\n" + endArray
440
- buildProfileContent = insertString(buildProfileData, buildProfilePosition, buildProfileBuilder)
441
- }
442
-
443
- writeFileSync(buildProfilePath, buildProfileContent);
440
+
441
+ writeFileSync(buildProfilePath, buildProfileContent);
444
442
 
445
443
  }
446
444
 
447
445
  function getApp() {
448
- let initAppConfig = ""
449
- let importAppConfig = ""
450
- entryUpperCaseNameArray.forEach((name, index) => {
451
- let isHsp = entryIsHsp[index]
452
- let endHsp = ""
453
- if (isHsp) {
454
- endHsp = "getARouter()"
455
- }
456
- initAppConfig = initAppConfig + (" new " + name + "RouterConfig(" + endHsp + "),\n")
457
- dependenciesKey.forEach((item, index) => {
458
- if (item.indexOf(name.toLowerCase()) != -1) {
459
- //证明是包含的
460
- importAppConfig = importAppConfig + "\nimport { " + name +
461
- "RouterConfig } from '" + item + "';\n"
462
- }
446
+ let initAppConfig = ""
447
+ let importAppConfig = ""
448
+ entryUpperCaseNameArray.forEach((name, index) => {
449
+ let isHsp = entryIsHsp[index]
450
+ let endHsp = ""
451
+ if (isHsp) {
452
+ endHsp = "getARouter()"
453
+ }
454
+ initAppConfig = initAppConfig + (" new " + name + "RouterConfig(" + endHsp + "),\n")
455
+ dependenciesKey.forEach((item, index) => {
456
+ if (item.indexOf(name.toLowerCase()) != -1) {
457
+ //证明是包含的
458
+ importAppConfig = importAppConfig + "\nimport { " + name +
459
+ "RouterConfig } from '" + item + "';\n"
460
+ }
461
+ })
463
462
  })
464
- })
465
-
466
- let appContent = "import { getARouter,routerInitConfig } from '@abner/router';\n" +
467
- "import { AbilityStage } from '@kit.AbilityKit';\n" +
468
- "import { EntryRouterConfig } from '../../../EntryRouterConfig';\n" +
469
- importAppConfig +
470
- "\n" +
471
- "export class App extends AbilityStage {\n" +
472
- " onCreate(): void {\n" +
473
- " routerInitConfig([\n" +
474
- initAppConfig +
475
- " new EntryRouterConfig()\n" +
476
- " ])\n" +
477
- " }\n" +
478
- "}"
479
- return appContent
463
+
464
+ let appContent = "import { getARouter,routerInitConfig } from '@abner/router';\n" +
465
+ "import { AbilityStage } from '@kit.AbilityKit';\n" +
466
+ "import { EntryRouterConfig } from '../../../EntryRouterConfig';\n" +
467
+ importAppConfig +
468
+ "\n" +
469
+ "export class App extends AbilityStage {\n" +
470
+ " onCreate(): void {\n" +
471
+ " routerInitConfig([\n" +
472
+ initAppConfig +
473
+ " new EntryRouterConfig()\n" +
474
+ " ])\n" +
475
+ " }\n" +
476
+ "}"
477
+ return appContent
480
478
  }
481
479
 
482
- function replaceCharUsingSubstring(str:string, index:number, newChar:string) {
483
- if (index < 0 || index >= str.length) {
484
- return str;
485
- }
486
- return str.substring(0, index) + newChar + str.substring(index + 1);
480
+ function replaceCharUsingSubstring(str: string, index: number, newChar: string) {
481
+ if (index < 0 || index >= str.length) {
482
+ return str;
483
+ }
484
+ return str.substring(0, index) + newChar + str.substring(index + 1);
487
485
  }