ohos-router 1.2.5 → 1.2.7

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.
package/.idea/r/router.ts DELETED
@@ -1,563 +0,0 @@
1
- import { HvigorNode } from '@ohos/hvigor';
2
- import { readFileSync, writeFileSync, mkdir, readdirSync, readFile, existsSync } from 'fs'
3
-
4
- let entryModulePath = ""
5
- let entryUpperCaseNameArray:string[] = []
6
- let entryIsHsp:boolean[] = []
7
-
8
- export function pluginStart(node: HvigorNode, isExecute: boolean) {
9
- if (!isExecute) {
10
- return
11
- }
12
-
13
- entryUpperCaseNameArray = []
14
- entryIsHsp = []
15
- let nodePath = node.getNodePath()
16
- node.subNodes((childNode: HvigorNode) => {
17
- try {
18
- //第一步,判断当前的Module是静态还是动态还是entry
19
- let hvigorfilePath = childNode.getNodePath() + "\\hvigorfile.ts"
20
- let hvigorfileData = readFileSync(hvigorfilePath, 'utf-8');
21
- let isHsp = hvigorfileData.indexOf("hspTasks") != -1 //动态包
22
- let isHap = hvigorfileData.indexOf("hapTasks") != -1 //entry//包
23
-
24
- //第二步,创建需要的路由管理配置
25
- let nodeName = childNode.getNodeName() //模块的名字
26
- let upperCaseName = nodeName.substring(0, 1).toUpperCase() + nodeName.substring(1, nodeName.length)
27
- //默认Entry不追加
28
- if ("Entry" != upperCaseName) {
29
- entryUpperCaseNameArray.push(upperCaseName)
30
- entryIsHsp.push(isHsp)
31
- }
32
- let configPath = childNode.getNodePath() + "\\" + upperCaseName + "RouterConfig.ets"
33
-
34
- if (!existsSync(configPath)) {
35
- //不存在,就去创建
36
- try {
37
- writeFileSync(configPath, getRouterConfig(upperCaseName, nodeName, isHsp));
38
- } catch (error) {
39
- log(JSON.stringify(error))
40
- }
41
- //另外需要在index.ets文件里进行导出
42
- if (!isHap) {
43
- //导出,除了主Module外
44
- let indexFilePath = childNode.getNodePath() + "\\" + "Index.ets"
45
- let indexFileData = readFileSync(indexFilePath, 'utf-8');
46
- if (indexFileData.indexOf(upperCaseName) == -1) {
47
- //如果没有再写入
48
- indexFileData = "export { " + upperCaseName +
49
- "RouterConfig } from './" + upperCaseName +
50
- "RouterConfig'\n" + indexFileData
51
- writeFileSync(indexFilePath, indexFileData);
52
- }
53
- }
54
- }
55
-
56
- //第四步注册各个组件,在entry中build-profile.json5
57
- if (isHap) {
58
- entryModulePath = childNode.getNodePath()
59
- //往build-profile.json5里添加数据
60
- writeBuildProfile(childNode)
61
- }
62
- //第5步骤,最重要寻找 @RouterPath("entry_back"),自动在当前的配置文件进行注册
63
- let fileList = childNode.getNodeDir()
64
- .asFileList()
65
- .filter(item => {
66
- return item.getPath().indexOf("build") == -1
67
- && item.getPath().indexOf("oh_modules") == -1
68
- && item.getPath().indexOf("src\\main") != -1
69
- && item.getPath().endsWith(".ets")
70
- })
71
-
72
- fileList.forEach((file) => {
73
- let data = readFileSync(file.filePath, 'utf-8');
74
- //必须包含
75
- if (data.indexOf("@RouterPath") != -1) {
76
- //找到带有@RouterPath的标识
77
- let routerPathEnd = data.split("@RouterPath")[1]
78
- let routerName = ""
79
- if (routerPathEnd.startsWith("(\"")) {
80
- //如果包含
81
- routerName = routerPathEnd.substring(2, routerPathEnd.indexOf(")") - 1)
82
- } else {
83
- routerName = routerPathEnd.substring(1, routerPathEnd.indexOf(")"))
84
- }
85
- //进行注册 configPath:文件路径,nodeName:Module
86
- var importPath = "./src" + file.filePath.split("src")[1] //这是需要的导包
87
- importPath = importPath.replace(".ets", "").replaceAll("\\", "/")
88
- let viewName = importPath.substring(importPath.lastIndexOf("/") + 1, importPath.length)
89
- let fileData = readFileSync(configPath, 'utf-8')
90
- //不包含的情况下进行文件重写,也就是config文件里没有
91
- if (fileData.indexOf(routerName) == -1) {
92
- //第一步,插入 import
93
- let switchTag = "switch (builderName) {"
94
- let switchPosition = fileData.indexOf(switchTag) + switchTag.length
95
-
96
- let switchBuilder = ""
97
-
98
- if (routerPathEnd.startsWith("(\"")) {
99
- switchBuilder = "\n case \"" + routerName + "\":\n" +
100
- " import(\"" + importPath + "\")\n" +
101
- " break"
102
- } else {
103
- switchBuilder = "\n case " + routerName + ":\n" +
104
- " import(\"" + importPath + "\")\n" +
105
- " break"
106
- }
107
-
108
- let configEnd = insertString(fileData, switchPosition, switchBuilder)
109
- //第一步,插入 View
110
- let destinationEnd = ""
111
- let DestinationTag = "NavDestination() {"
112
- let destinationPosition = configEnd.indexOf(DestinationTag) + DestinationTag.length
113
-
114
- let destinationBuilder = ""
115
- if (routerPathEnd.startsWith("(\"")) {
116
- destinationBuilder = "\n if (routerGetBuilderType() == \"" + routerName + "\") {\n" +
117
- " " + viewName + "()\n" +
118
- " } "
119
- } else {
120
- destinationBuilder = "\n if (routerGetBuilderType() == " + routerName + ") {\n" +
121
- " " + viewName + "()\n" +
122
- " } "
123
- }
124
-
125
- if (configEnd.indexOf("routerGetBuilderType()") != -1) {
126
- //证明已经存在了
127
- destinationBuilder = destinationBuilder + "else "
128
- }
129
- destinationEnd = insertString(configEnd, destinationPosition, destinationBuilder)
130
-
131
- let importContent = ""
132
-
133
- //如果导包路径不在,才去,重写
134
- if (fileData.indexOf(importPath) == -1) {
135
- importContent = "import { " + viewName +
136
- " } from '" + importPath + "';\n"
137
- }
138
- let className = routerName.split(".")[0]
139
- if (!routerPathEnd.startsWith("(\"") && fileData.indexOf("./" + className) == -1) {
140
- //不包含,就需要进行导实际的包,目前仅支持在外部定义路由管理
141
- importContent = importContent + "import { " + className +
142
- " } from './" + className + "';\n"
143
- }
144
- destinationEnd = importContent + destinationEnd
145
-
146
- writeFileSync(configPath, destinationEnd);
147
- }
148
- }
149
- })
150
-
151
- } catch (error) {
152
- console.log(error)
153
- }
154
- })
155
-
156
- try {
157
-
158
- //第六部,在App中进行初始化
159
- //先判断是否存在AbilityStage
160
- let moduleJson5Path = entryModulePath + "\\src\\main\\module.json5"
161
- let moduleJson5Data = readFileSync(moduleJson5Path, 'utf-8');
162
- moduleJson5JSONParse(moduleJson5Data)
163
- let appPath = entryModulePath + "\\src\\main\\ets\\App.ets"
164
- if (moduleSrcEntry == undefined) {
165
- //没有
166
- writeFileSync(appPath, getApp());
167
- //同时改变module.json5
168
- let moduleTag = "\"module\": {"
169
- let modulePosition = moduleJson5Data.indexOf(moduleTag) + moduleTag.length
170
- let mJson = "\n \"srcEntry\": \"./ets/App.ets\","
171
- let moduleContent = insertString(moduleJson5Data, modulePosition, mJson)
172
- writeFileSync(moduleJson5Path, moduleContent);
173
- } else {
174
- //有
175
- let endSrc = moduleSrcEntry.substring(1, moduleSrcEntry.length).replaceAll("/", "\\")
176
- let endAppPath = entryModulePath + "\\src\\main" + endSrc
177
- let appFileData = readFileSync(endAppPath, 'utf-8');
178
- if (appFileData.indexOf("routerInitConfig([") != -1) {
179
- //存在
180
- let initHaveConfig = ""
181
- let importHaveConfig = ""
182
- let isHaveConfig = false;
183
- entryUpperCaseNameArray.forEach((name, index) => {
184
- let isHsp = entryIsHsp[index]
185
- let endHsp = ""
186
- if (isHsp) {
187
- endHsp = "getARouter()"
188
- }
189
-
190
- if (appFileData.indexOf(name) == -1) {
191
- //不包含
192
- isHaveConfig = true;
193
- initHaveConfig = initHaveConfig + (" new " + name + "RouterConfig(" + endHsp + "),\n")
194
- dependenciesKey.forEach((item, index) => {
195
- if (item.indexOf(name.toLowerCase()) != -1) {
196
- //证明是包含的
197
- importHaveConfig = importHaveConfig + "\nimport { " + name +
198
- "RouterConfig } from '" + item + "';\n"
199
- }
200
- })
201
- }
202
- })
203
-
204
- if (isHaveConfig) {
205
- //为true进行写入,减少每次都要进行
206
- let routerInitHaveTag = "routerInitConfig(["
207
- let routerInitHavePosition = appFileData.indexOf(routerInitHaveTag) + routerInitHaveTag.length
208
- let routerHaveJson = "\n" + initHaveConfig
209
- let routerInitHaveContent = insertString(appFileData, routerInitHavePosition, routerHaveJson)
210
- routerInitHaveContent = importHaveConfig + routerInitHaveContent
211
- writeFileSync(endAppPath, routerInitHaveContent);
212
- }
213
- } else {
214
- //不存在,需要获取所有的配置,配置包括导的包,配置文件
215
- let initNewConfig = ""
216
- let importNewConfig = ""
217
- entryUpperCaseNameArray.forEach((name, index) => {
218
- let isHsp = entryIsHsp[index]
219
- let endHsp = ""
220
- if (isHsp) {
221
- endHsp = "getARouter()"
222
- }
223
- initNewConfig = initNewConfig + (" new " + name + "RouterConfig(" + endHsp + "),\n")
224
- dependenciesKey.forEach((item, index) => {
225
- if (item.indexOf(name.toLowerCase()) != -1) {
226
- //证明是包含的
227
- importNewConfig = importNewConfig + "\nimport { " + name +
228
- "RouterConfig } from '" + item + "';\n"
229
- }
230
- })
231
- })
232
-
233
- let routerInitTag = "onCreate(): void {"
234
- let routerInitPosition = appFileData.indexOf(routerInitTag) + routerInitTag.length
235
- let routerJson = "\n routerInitConfig([\n" +
236
- initNewConfig +
237
- " new EntryRouterConfig()\n" +
238
- " ])\n"
239
- let routerInitContent = insertString(appFileData, routerInitPosition, routerJson)
240
- routerInitContent = "import { getARouter,routerInitConfig } from '@abner/router';\n" +
241
- "import { EntryRouterConfig } from '../../../EntryRouterConfig';\n"
242
- + importNewConfig
243
- + routerInitContent
244
- writeFileSync(endAppPath, routerInitContent);
245
- }
246
- }
247
-
248
- } catch (error) {
249
-
250
- }
251
-
252
- }
253
-
254
- let moduleSrcEntry:string
255
-
256
- function moduleJson5JSONParse(moduleJson5Data:string) {
257
- try {
258
- let moduleJsonBean = JSON.parse(moduleJson5Data)
259
- //解析成功
260
- let module = moduleJsonBean.module
261
- moduleSrcEntry = module.srcEntry
262
- } catch (error) {
263
- let errorArray = error.message.split(" ")
264
- let position = Number(errorArray[errorArray.length-1])
265
- let endString = moduleJson5Data.substring(0, position)
266
- const lastIndex = endString.lastIndexOf(",")
267
- //获取最后的错误之处
268
- let replacedStr = replaceCharUsingSubstring(endString, lastIndex, '');
269
- moduleJson5JSONParse(replacedStr + moduleJson5Data.substring(position, moduleJson5Data.length))
270
- }
271
-
272
- }
273
-
274
- function insertString(src: string, pos: number, val: string): string {
275
- if (pos < -src.length - 1 || pos > src.length) {
276
- throw "insert position is error";
277
- }
278
- if (pos >= 0) {
279
- return src.slice(0, pos) + val + src.slice(pos);
280
- } else {
281
- return src.slice(0, src.length + 1 + pos) + val + src.slice(src.length + 1 + pos);
282
- }
283
- }
284
-
285
- function getRouterConfig(upperCaseName: string, nodeName: string, isHsp: boolean) {
286
- var configText = "import { ";
287
- if (isHsp) {
288
- //是动态包
289
- configText = configText + " ARouter, setARouter, "
290
- }
291
- configText = configText + "RouterConfig,routerGetBuilderType, routerInitBuilder } from '@abner/router';\n" +
292
- "\n" +
293
- "/**\n" +
294
- " * AUTHOR:AbnerMing\n" +
295
- " * INTRODUCE:动态配置路径\n" +
296
- " * */\n" +
297
- "function importPath(builderName: string) {\n" +
298
- " switch (builderName) {\n" +
299
- "\n" +
300
- " }\n" +
301
- "}\n" +
302
- "\n" +
303
- "/**\n" +
304
- " * AUTHOR:AbnerMing\n" +
305
- " * INTRODUCE:动态配置组件\n" +
306
- " * */\n" +
307
- "@Builder\n" +
308
- "export function viewBuilder() {\n" +
309
- " NavDestination() {\n" +
310
- "\n" +
311
- " }\n" +
312
- " .hideTitleBar(true)\n" +
313
- " .height('100%')\n" +
314
- "\n" +
315
- "}\n" +
316
- "\n" +
317
- "\n" +
318
- "/**\n" +
319
- " * AUTHOR:AbnerMing\n" +
320
- " * DATE:" + getNowTime() + "\n" +
321
- " * INTRODUCE:路由配置,此路由配置文件为自动生成,无特殊情况下请无须改动\n" +
322
- " * */\n" +
323
- "export class " + upperCaseName + "RouterConfig implements RouterConfig {\n";
324
-
325
- if (isHsp) {
326
- //是动态包
327
- configText = configText + " constructor(router: ARouter) {\n" +
328
- " setARouter(router)\n" +
329
- " }\n"
330
- }
331
-
332
- configText = configText + " getModuleName(): string {\n" +
333
- " return \"" + nodeName + "\"\n" +
334
- " }\n" +
335
- "\n" +
336
- " async initRouter(builderName: string): Promise<void> {\n" +
337
- " return await new Promise((resolve: Function) => {\n" +
338
- " importPath(builderName)\n" +
339
- " routerInitBuilder(builderName, wrapBuilder(viewBuilder))\n" +
340
- " resolve()\n" +
341
- " })\n" +
342
- " }\n" +
343
- "}"
344
-
345
-
346
- return configText
347
- }
348
-
349
- var buildProfilePath = ""
350
-
351
- function writeBuildProfile(childNode: HvigorNode) {
352
- let ohPackagePath = childNode.getNodePath() + "\\oh-package.json5"
353
- let ohPackageData = readFileSync(ohPackagePath, 'utf-8');
354
- dependenciesJSONParse(ohPackageData)
355
- buildProfilePath = childNode.getNodePath() + "\\build-profile.json5"
356
- // //这里注册
357
- let buildProfileData = readFileSync(buildProfilePath, 'utf-8');
358
- // //判断是否含有 arkOptions runtimeOnly packages,如果没有直接创建
359
- buildProfileJSONParse(buildProfileData)
360
- }
361
-
362
- //获取当前的时间
363
- function getNowTime() {
364
- let d = new Date();
365
- return d.getFullYear() + "年" + (d.getMonth() + 1) + "月" + d.getDate() + "日 " + d.getHours() + "时" +
366
- d.getMinutes() +
367
- "分" + d.getSeconds() + "秒"
368
- }
369
-
370
- function log(params:string) {
371
- console.error(params);
372
- }
373
-
374
- let dependenciesKey: string[]
375
-
376
- function dependenciesJSONParse(ohPackageData: string) {
377
- try {
378
- dependenciesKey = []
379
- let ohPackAgeBean = JSON.parse(ohPackageData)
380
- //拿到dependencies对象
381
- let dependencies = ohPackAgeBean.dependencies
382
- for (var key in dependencies) {
383
- //只获取带有file的组件,远程组件不获取
384
- if (dependencies[key].indexOf("file:..") != -1) {
385
- dependenciesKey.push(key)
386
- }
387
- }
388
- } catch (error) {
389
- let errorArray = error.message.split(" ")
390
- let position = Number(errorArray[errorArray.length-1])
391
- let endString = ohPackageData.substring(0, position)
392
- const lastIndex = endString.lastIndexOf(",")
393
- //获取最后的错误之处
394
- let replacedStr = replaceCharUsingSubstring(endString, lastIndex, '');
395
- dependenciesJSONParse(replacedStr + ohPackageData.substring(position, ohPackageData.length))
396
- }
397
- }
398
-
399
- function buildProfileJSONParse(buildProfileData:string) {
400
- try {
401
- let buildProfileBean = JSON.parse(buildProfileData)
402
- let buildOptionBean = buildProfileBean.buildOption
403
- if (buildOptionBean.arkOptions == undefined) {
404
- //创建
405
- // let arkOptions = {
406
- // runtimeOnly: {
407
- // sources: [],
408
- // packages: dependenciesKey
409
- // }
410
- // }
411
- // buildOptionBean["arkOptions"] = arkOptions
412
- //重新覆盖
413
- //对buildProfile文件进行覆盖写入
414
- //writeFileSync(buildProfilePath, JSON.stringify(buildProfileBean));
415
- //使用这种方式,防止格式
416
- printBuildProFile(buildProfileData, 0)
417
- } else {
418
- //有了这个参数
419
- let arkOptions = buildOptionBean.arkOptions
420
- if (arkOptions.runtimeOnly == undefined) {
421
- // let runtimeOnly = {
422
- // sources: [],
423
- // packages: dependenciesKey
424
- // }
425
- // arkOptions["runtimeOnly"] = runtimeOnly
426
- //对buildProfile文件进行覆盖写入
427
- //writeFileSync(buildProfilePath, JSON.stringify(buildProfileBean));
428
-
429
- printBuildProFile(buildProfileData, 1)
430
-
431
- } else {
432
- let runtimeOnly = arkOptions.runtimeOnly
433
- //判断是否有packages这个参数
434
- if (runtimeOnly.packages == undefined) {
435
- //没有
436
- //runtimeOnly["packages"] = dependenciesKey
437
- //对buildProfile文件进行覆盖写入
438
- //writeFileSync(buildProfilePath, JSON.stringify(buildProfileBean));
439
- printBuildProFile(buildProfileData, 2)
440
- } else {
441
- //有这个参数,也得需要更改
442
- if (dependenciesKey.toString() != runtimeOnly["packages"].toString()) {
443
- // runtimeOnly["packages"] = dependenciesKey
444
- // writeFileSync(buildProfilePath, JSON.stringify(buildProfileBean));
445
- printBuildProFile(buildProfileData, 3)
446
- }
447
- }
448
- }
449
- }
450
- } catch (error) {
451
- let errorArray = error.message.split(" ")
452
- let position = Number(errorArray[errorArray.length-1])
453
- let endString = buildProfileData.substring(0, position)
454
- const lastIndex = endString.lastIndexOf(",")
455
- //获取最后的错误之处
456
- let replacedStr = replaceCharUsingSubstring(endString, lastIndex, '');
457
- buildProfileJSONParse(replacedStr + buildProfileData.substring(position, buildProfileData.length))
458
- }
459
- }
460
-
461
- //打印到文件
462
- function printBuildProFile(buildProfileData: string, type: number) {
463
- let endArray = ""
464
- dependenciesKey.forEach((item, index) => {
465
- if (index == dependenciesKey.length - 1) {
466
- endArray = endArray + " \"" + item + "\"\n"
467
- } else {
468
- endArray = endArray + " \"" + item + "\",\n"
469
- }
470
- })
471
-
472
- if (endArray == "") {
473
- //为空
474
- return
475
- }
476
-
477
- let buildProfileContent = ""
478
- if (type == 0) {
479
- let buildProfileTag = "\"buildOption\": {"
480
- let buildProfilePosition = buildProfileData.indexOf(buildProfileTag) + buildProfileTag.length
481
- let buildProfileBuilder = "\n \"arkOptions\": {\n" +
482
- " \"runtimeOnly\": {\n" +
483
- " \"sources\": [\n" +
484
- " ],\n" +
485
- " \"packages\": [\n" +
486
- endArray +
487
- " ]\n" +
488
- " }\n" +
489
- " }"
490
-
491
- buildProfileContent = insertString(buildProfileData, buildProfilePosition, buildProfileBuilder)
492
- } else if (type == 1) {
493
- let buildProfileTag = " \"buildOption\": {\n" +
494
- " \"arkOptions\": {"
495
- let buildProfilePosition = buildProfileData.indexOf(buildProfileTag) + buildProfileTag.length
496
- let buildProfileBuilder = "\n \"runtimeOnly\": {\n" +
497
- " \"sources\": [\n" +
498
- " ],\n" +
499
- " \"packages\": [\n" +
500
- endArray +
501
- " ]\n" +
502
- " }"
503
- buildProfileContent = insertString(buildProfileData, buildProfilePosition, buildProfileBuilder)
504
- } else if (type == 2) {
505
- let buildProfileTag = "\"runtimeOnly\": {"
506
- let buildProfilePosition = buildProfileData.indexOf(buildProfileTag) + buildProfileTag.length
507
- let buildProfileBuilder = "\n \"packages\": [\n" +
508
- endArray +
509
- " ]\n"
510
- buildProfileContent = insertString(buildProfileData, buildProfilePosition, buildProfileBuilder)
511
- } else if (type == 3) {
512
- let buildProfileTag = "\"packages\": ["
513
- let buildProfilePosition = buildProfileData.indexOf(buildProfileTag) + buildProfileTag.length
514
- let buildProfileBuilder = "\n" + endArray
515
- buildProfileContent = insertString(buildProfileData, buildProfilePosition, buildProfileBuilder)
516
- }
517
-
518
- writeFileSync(buildProfilePath, buildProfileContent);
519
-
520
- }
521
-
522
- function getApp() {
523
- let initAppConfig = ""
524
- let importAppConfig = ""
525
- entryUpperCaseNameArray.forEach((name, index) => {
526
- let isHsp = entryIsHsp[index]
527
- let endHsp = ""
528
- //如果是动态包
529
- if (isHsp) {
530
- endHsp = "getARouter()"
531
- }
532
- initAppConfig = initAppConfig + (" new " + name + "RouterConfig(" + endHsp + "),\n")
533
- dependenciesKey.forEach((item, index) => {
534
- if (item.indexOf(name.toLowerCase()) != -1) {
535
- //证明是包含的
536
- importAppConfig = importAppConfig + "\nimport { " + name +
537
- "RouterConfig } from '" + item + "';\n"
538
- }
539
- })
540
- })
541
-
542
- let appContent = "import { getARouter,routerInitConfig } from '@abner/router';\n" +
543
- "import { AbilityStage } from '@kit.AbilityKit';\n" +
544
- "import { EntryRouterConfig } from '../../../EntryRouterConfig';\n" +
545
- importAppConfig +
546
- "\n" +
547
- "export class App extends AbilityStage {\n" +
548
- " onCreate(): void {\n" +
549
- " routerInitConfig([\n" +
550
- initAppConfig +
551
- " new EntryRouterConfig()\n" +
552
- " ])\n" +
553
- " }\n" +
554
- "}"
555
- return appContent
556
- }
557
-
558
- function replaceCharUsingSubstring(str:string, index:number, newChar:string) {
559
- if (index < 0 || index >= str.length) {
560
- return str;
561
- }
562
- return str.substring(0, index) + newChar + str.substring(index + 1);
563
- }