ohos-router 1.0.8 → 1.0.9

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 (3) hide show
  1. package/package.json +1 -1
  2. package/router.ts +58 -52
  3. package/fs.d.ts +0 -4311
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ohos-router",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "description": "鸿蒙路由管理插件",
5
5
  "main": "main.js",
6
6
  "scripts": {
package/router.ts CHANGED
@@ -1,5 +1,4 @@
1
- import {HvigorNode} from '@ohos/hvigor';
2
- import {readFileSync, writeFileSync, existsSync} from 'fs'
1
+ import { HvigorNode, FileUtil } from '@ohos/hvigor';
3
2
 
4
3
  let entryModulePath = ""
5
4
  let entryUpperCaseNameArray: string[] = []
@@ -9,13 +8,12 @@ export function pluginStart(node: HvigorNode, isExecute: boolean) {
9
8
  if (!isExecute) {
10
9
  return
11
10
  }
12
-
13
11
  entryUpperCaseNameArray = []
14
12
  entryIsHsp = []
15
13
  node.subNodes((childNode: HvigorNode) => {
16
14
  try {
17
- let hvigorfilePath = childNode.getNodePath() + "\\hvigorfile.ts"
18
- let hvigorfileData = readFileSync(hvigorfilePath, 'utf-8');
15
+ let hvigorfilePath = FileUtil.pathResolve(childNode.getNodePath(), "hvigorfile.ts")
16
+ let hvigorfileData = FileUtil.readFileSync(hvigorfilePath);
19
17
  let isHsp = hvigorfileData.indexOf("hspTasks") != -1
20
18
  let isHap = hvigorfileData.indexOf("hapTasks") != -1
21
19
  let nodeName = childNode.getNodeName()
@@ -25,42 +23,40 @@ export function pluginStart(node: HvigorNode, isExecute: boolean) {
25
23
  entryUpperCaseNameArray.push(upperCaseName)
26
24
  entryIsHsp.push(isHsp)
27
25
  }
28
- let configPath = childNode.getNodePath() + "\\" + upperCaseName + "RouterConfig.ets"
26
+ let configPath = FileUtil.pathResolve(childNode.getNodePath(), upperCaseName + "RouterConfig.ets")
29
27
 
30
- if (!existsSync(configPath)) {
31
- try {
32
- writeFileSync(configPath, getRouterConfig(upperCaseName, nodeName, isHsp));
33
- } catch (error) {
34
- log(JSON.stringify(error))
35
- }
28
+ if (!FileUtil.exist(configPath)) {
29
+ FileUtil.ensureFileSync(configPath)
30
+ FileUtil.writeFileSync(configPath, getRouterConfig(upperCaseName, nodeName, isHsp))
36
31
  if (!isHap) {
37
- let indexFilePath = childNode.getNodePath() + "\\" + "Index.ets"
38
- let indexFileData = readFileSync(indexFilePath, 'utf-8');
32
+ let indexFilePath = FileUtil.pathResolve(childNode.getNodePath(), "Index.ets")
33
+ let indexFileData = FileUtil.readFileSync(indexFilePath);
39
34
  if (indexFileData.indexOf(upperCaseName) == -1) {
40
35
  indexFileData = "export { " + upperCaseName +
41
36
  "RouterConfig } from './" + upperCaseName +
42
37
  "RouterConfig'\n" + indexFileData
43
- writeFileSync(indexFilePath, indexFileData);
38
+
39
+ FileUtil.writeFileSync(indexFilePath, indexFileData)
44
40
  }
45
41
  }
46
42
  }
43
+
47
44
  if (isHap) {
48
45
  entryModulePath = childNode.getNodePath()
49
46
  writeBuildProfile(childNode)
50
47
  }
48
+
51
49
  let fileList = childNode.getNodeDir()
52
50
  .asFileList()
53
51
  .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")
52
+ return item.getPath().endsWith(".ets") && item.getPath().indexOf("build") == -1 &&
53
+ item.getPath().indexOf("oh_modules") == -1 && item.getPath().indexOf("ohosTest") == -1
58
54
  })
59
55
 
60
56
  fileList.forEach((file) => {
61
- let data = readFileSync(file.filePath, 'utf-8');
57
+ let data = FileUtil.readFileSync(file.filePath);
62
58
  if (data.indexOf("@RouterPath") != -1) {
63
- let routerPathEnd = data.split("@RouterPath")[1]
59
+ let routerPathEnd = data.toString().split("@RouterPath")[1]
64
60
  let routerName = ""
65
61
  if (routerPathEnd.startsWith("(\"")) {
66
62
  routerName = routerPathEnd.substring(2, routerPathEnd.indexOf(")") - 1)
@@ -70,7 +66,7 @@ export function pluginStart(node: HvigorNode, isExecute: boolean) {
70
66
  var importPath = "./src" + file.filePath.split("src")[1]
71
67
  importPath = importPath.replace(".ets", "").replaceAll("\\", "/")
72
68
  let viewName = importPath.substring(importPath.lastIndexOf("/") + 1, importPath.length)
73
- let fileData = readFileSync(configPath, 'utf-8')
69
+ let fileData = FileUtil.readFileSync(configPath)
74
70
  if (fileData.indexOf(routerName) == -1) {
75
71
  let switchTag = "switch (builderName) {"
76
72
  let switchPosition = fileData.indexOf(switchTag) + switchTag.length
@@ -121,7 +117,7 @@ export function pluginStart(node: HvigorNode, isExecute: boolean) {
121
117
  }
122
118
  destinationEnd = importContent + destinationEnd
123
119
 
124
- writeFileSync(configPath, destinationEnd);
120
+ FileUtil.writeFileSync(configPath, destinationEnd);
125
121
  }
126
122
  }
127
123
  })
@@ -132,21 +128,21 @@ export function pluginStart(node: HvigorNode, isExecute: boolean) {
132
128
  })
133
129
 
134
130
  try {
135
- let moduleJson5Path = entryModulePath + "\\src\\main\\module.json5"
136
- let moduleJson5Data = readFileSync(moduleJson5Path, 'utf-8');
131
+ let moduleJson5Path = FileUtil.pathResolve(entryModulePath + "/src/main/", "module.json5")
132
+ let moduleJson5Data = FileUtil.readJson5(moduleJson5Path)
137
133
  moduleJson5JSONParse(moduleJson5Data)
138
- let appPath = entryModulePath + "\\src\\main\\ets\\App.ets"
134
+ let appPath = FileUtil.pathResolve(entryModulePath + "/src/main/ets/", "App.ets")
139
135
  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);
136
+ FileUtil.ensureFileSync(appPath)
137
+ FileUtil.writeFileSync(appPath, getApp())
138
+ let moduleBean = moduleJson5Data.module
139
+ const updated = addPropertyToBeginning(moduleBean, "srcEntry", "./ets/App.ets")
140
+ moduleJson5Data.module = updated
141
+ FileUtil.writeFileSync(moduleJson5Path, JSON.stringify(moduleJson5Data))
146
142
  } else {
147
- let endSrc = moduleSrcEntry.substring(1, moduleSrcEntry.length).replaceAll("/", "\\")
148
- let endAppPath = entryModulePath + "\\src\\main" + endSrc
149
- let appFileData = readFileSync(endAppPath, 'utf-8');
143
+ let endSrc = moduleSrcEntry.substring(1, moduleSrcEntry.length)
144
+ let endAppPath = entryModulePath + "/src/main" + endSrc
145
+ let appFileData = FileUtil.readFileSync(endAppPath);
150
146
  if (appFileData.indexOf("routerInitConfig([") != -1) {
151
147
  let initHaveConfig = ""
152
148
  let importHaveConfig = ""
@@ -176,7 +172,7 @@ export function pluginStart(node: HvigorNode, isExecute: boolean) {
176
172
  let routerHaveJson = "\n" + initHaveConfig
177
173
  let routerInitHaveContent = insertString(appFileData, routerInitHavePosition, routerHaveJson)
178
174
  routerInitHaveContent = importHaveConfig + routerInitHaveContent
179
- writeFileSync(endAppPath, routerInitHaveContent);
175
+ FileUtil.writeFileSync(endAppPath, routerInitHaveContent);
180
176
  }
181
177
  } else {
182
178
  let initNewConfig = ""
@@ -207,7 +203,7 @@ export function pluginStart(node: HvigorNode, isExecute: boolean) {
207
203
  "import { EntryRouterConfig } from '../../../EntryRouterConfig';\n"
208
204
  + importNewConfig
209
205
  + routerInitContent
210
- writeFileSync(endAppPath, routerInitContent);
206
+ FileUtil.writeFileSync(endAppPath, routerInitContent);
211
207
  }
212
208
  }
213
209
 
@@ -217,22 +213,22 @@ export function pluginStart(node: HvigorNode, isExecute: boolean) {
217
213
 
218
214
  }
219
215
 
216
+
220
217
  let moduleSrcEntry: string
221
218
 
222
- function moduleJson5JSONParse(moduleJson5Data: string) {
219
+ function moduleJson5JSONParse(moduleJsonBean) {
223
220
  try {
224
- let moduleJsonBean = JSON.parse(moduleJson5Data)
225
221
  let module = moduleJsonBean.module
226
222
  moduleSrcEntry = module.srcEntry
227
223
  } catch (error) {
224
+ let moduleJson5Data = JSON.stringify(moduleJsonBean)
228
225
  let errorArray = error.message.split(" ")
229
226
  let position = Number(errorArray[errorArray.length - 1])
230
227
  let endString = moduleJson5Data.substring(0, position)
231
228
  const lastIndex = endString.lastIndexOf(",")
232
229
  let replacedStr = replaceCharUsingSubstring(endString, lastIndex, '');
233
- moduleJson5JSONParse(replacedStr + moduleJson5Data.substring(position, moduleJson5Data.length))
230
+ moduleJson5JSONParse(JSON.parse(replacedStr + moduleJson5Data.substring(position, moduleJson5Data.length)))
234
231
  }
235
-
236
232
  }
237
233
 
238
234
  function insertString(src: string, pos: number, val: string): string {
@@ -251,7 +247,8 @@ function getRouterConfig(upperCaseName: string, nodeName: string, isHsp: boolean
251
247
  if (isHsp) {
252
248
  configText = configText + " ARouter, setARouter, "
253
249
  }
254
- configText = configText + "RouterConfig,routerGetBuilderType, routerInitBuilder,NavDestinationView } from '@abner/router';\n" +
250
+ configText =
251
+ configText + "RouterConfig,routerGetBuilderType, routerInitBuilder,NavDestinationView } from '@abner/router';\n" +
255
252
  "\n" +
256
253
  "/**\n" +
257
254
  " * AUTHOR:AbnerMing\n" +
@@ -303,19 +300,19 @@ function getRouterConfig(upperCaseName: string, nodeName: string, isHsp: boolean
303
300
  " }\n" +
304
301
  "}"
305
302
 
306
-
307
303
  return configText
308
304
  }
309
305
 
306
+
310
307
  var buildProfilePath = ""
311
308
 
312
309
  function writeBuildProfile(childNode: HvigorNode) {
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)
310
+ let ohPackagePath = FileUtil.pathResolve(childNode.getNodePath(), "oh-package.json5")
311
+ let ohPackageData = FileUtil.readJson5(ohPackagePath)
312
+ dependenciesJSONParse(JSON.stringify(ohPackageData))
313
+ buildProfilePath = FileUtil.pathResolve(childNode.getNodePath(), "build-profile.json5")
314
+ let buildProfileData = FileUtil.readJson5(buildProfilePath)
315
+ buildProfileJSONParse(JSON.stringify(buildProfileData))
319
316
  }
320
317
 
321
318
  function getNowTime() {
@@ -329,6 +326,7 @@ function log(params: string) {
329
326
  console.error(params);
330
327
  }
331
328
 
329
+
332
330
  let dependenciesKey: string[]
333
331
 
334
332
  function dependenciesJSONParse(ohPackageData: string) {
@@ -383,6 +381,7 @@ function buildProfileJSONParse(buildProfileData: string) {
383
381
  }
384
382
  }
385
383
 
384
+
386
385
  function printBuildProFile(buildProfileData: string, type: number) {
387
386
  let endArray = ""
388
387
  dependenciesKey.forEach((item, index) => {
@@ -438,8 +437,7 @@ function printBuildProFile(buildProfileData: string, type: number) {
438
437
  buildProfileContent = insertString(buildProfileData, buildProfilePosition, buildProfileBuilder)
439
438
  }
440
439
 
441
- writeFileSync(buildProfilePath, buildProfileContent);
442
-
440
+ FileUtil.writeFileSync(buildProfilePath, buildProfileContent);
443
441
  }
444
442
 
445
443
  function getApp() {
@@ -454,7 +452,6 @@ function getApp() {
454
452
  initAppConfig = initAppConfig + (" new " + name + "RouterConfig(" + endHsp + "),\n")
455
453
  dependenciesKey.forEach((item, index) => {
456
454
  if (item.indexOf(name.toLowerCase()) != -1) {
457
- //证明是包含的
458
455
  importAppConfig = importAppConfig + "\nimport { " + name +
459
456
  "RouterConfig } from '" + item + "';\n"
460
457
  }
@@ -482,4 +479,13 @@ function replaceCharUsingSubstring(str: string, index: number, newChar: string)
482
479
  return str;
483
480
  }
484
481
  return str.substring(0, index) + newChar + str.substring(index + 1);
482
+ }
483
+
484
+ function addPropertyToBeginning<T extends object, K extends string, V>(
485
+ obj: T,
486
+ key: K,
487
+ value: V
488
+ ): T & { [P in K]: V } {
489
+ const newObj = { [key]: value, ...obj } as T & { [P in K]: V };
490
+ return newObj;
485
491
  }