ohos-router 1.0.0 → 1.0.1

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