@mpilk/mp-route 2.0.0 → 3.0.0
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/dist/chunk-6MZ3UWKK.js +156 -132
- package/dist/cli.cjs +221 -150
- package/dist/index.cjs +175 -144
- package/package.json +2 -2
package/dist/chunk-6MZ3UWKK.js
CHANGED
|
@@ -1,84 +1,97 @@
|
|
|
1
|
-
import path from 'node:path'
|
|
2
|
-
import fs from 'node:fs'
|
|
3
|
-
import pino from 'pino'
|
|
4
|
-
import { loadFileContent, loadJson, outputFile } from '@
|
|
5
|
-
import { isEmpty, isArray, isPlainObject } from 'lodash-es'
|
|
6
|
-
import chokidar from 'chokidar'
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import fs from 'node:fs'
|
|
3
|
+
import pino from 'pino'
|
|
4
|
+
import { loadFileContent, loadJson, outputFile } from '@mpilk/mp-shared'
|
|
5
|
+
import { isEmpty, isArray, isPlainObject } from 'lodash-es'
|
|
6
|
+
import chokidar from 'chokidar'
|
|
7
7
|
|
|
8
8
|
// src/builder.ts
|
|
9
|
-
var _indexPath =
|
|
9
|
+
var _indexPath = ''
|
|
10
10
|
var logger = pino({
|
|
11
11
|
transport: {
|
|
12
|
-
target:
|
|
12
|
+
target: 'pino-pretty',
|
|
13
13
|
options: {
|
|
14
14
|
colorize: true
|
|
15
15
|
// ignore: 'hostname,pid',
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
|
-
})
|
|
19
|
-
var verboseLogger = logger.child({ verbose: true })
|
|
18
|
+
})
|
|
19
|
+
var verboseLogger = logger.child({ verbose: true })
|
|
20
20
|
function builderWithWatch(options) {
|
|
21
|
-
let timer = null
|
|
21
|
+
let timer = null
|
|
22
22
|
function run() {
|
|
23
|
-
if (timer)
|
|
24
|
-
clearTimeout(timer);
|
|
23
|
+
if (timer) clearTimeout(timer)
|
|
25
24
|
timer = setTimeout(() => {
|
|
26
|
-
timer = null
|
|
27
|
-
return builder(options)
|
|
28
|
-
}, options?.delay || 200)
|
|
25
|
+
timer = null
|
|
26
|
+
return builder(options)
|
|
27
|
+
}, options?.delay || 200)
|
|
29
28
|
}
|
|
30
|
-
return chokidar
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
29
|
+
return chokidar
|
|
30
|
+
.watch([
|
|
31
|
+
`src/**/${options?.routeName || 'route'}.{ts,js}`,
|
|
32
|
+
`src/${options?.tabbarName || 'tabbar'}.{ts,js}`
|
|
33
|
+
])
|
|
34
|
+
.on('all', (eventName) => {
|
|
35
|
+
switch (eventName) {
|
|
36
|
+
case 'add':
|
|
37
|
+
case 'change':
|
|
38
|
+
case 'unlink':
|
|
39
|
+
run()
|
|
40
|
+
break
|
|
41
|
+
}
|
|
42
|
+
})
|
|
42
43
|
}
|
|
43
44
|
async function builder(options = {}) {
|
|
44
|
-
const {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
45
|
+
const {
|
|
46
|
+
input = './src',
|
|
47
|
+
routeName = 'route',
|
|
48
|
+
tabbarName = 'tabbar',
|
|
49
|
+
pagesPath = './src/pages.json',
|
|
50
|
+
verbose
|
|
51
|
+
} = options
|
|
52
|
+
logger.info('\u5F00\u59CB\u751F\u6210\u8DEF\u7531\u6587\u4EF6...')
|
|
53
|
+
const filePaths = loadDirFiles(input, routeName)
|
|
54
|
+
logger.info(
|
|
55
|
+
`\u6839\u636E ${routeName} \u627E\u5230 ${filePaths.length} \u4E2A\u6587\u4EF6`
|
|
56
|
+
)
|
|
57
|
+
const routeConfigs = []
|
|
49
58
|
for (const filePath of filePaths) {
|
|
50
|
-
verbose &&
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
59
|
+
verbose &&
|
|
60
|
+
verboseLogger.info(`\u5F00\u59CB\u89E3\u6790\u6587\u4EF6 ${filePath}`)
|
|
61
|
+
let content = await loadFileContent(filePath)
|
|
62
|
+
if (!content || typeof content !== 'object' || isEmpty(content)) {
|
|
63
|
+
verbose &&
|
|
64
|
+
verboseLogger.info(
|
|
65
|
+
`\u6587\u4EF6 ${filePath} \u4E3A\u7A7A\uFF0C\u8DF3\u8FC7`
|
|
66
|
+
)
|
|
67
|
+
continue
|
|
55
68
|
}
|
|
56
|
-
if (!isArray(content))
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
69
|
+
if (!isArray(content)) content = [content]
|
|
70
|
+
verbose &&
|
|
71
|
+
verboseLogger.info(
|
|
72
|
+
`\u6587\u4EF6 ${filePath} \u89E3\u6790\u5B8C\u6210\uFF1A${JSON.stringify(
|
|
73
|
+
content
|
|
74
|
+
)}`
|
|
75
|
+
)
|
|
76
|
+
routeConfigs.push(...content)
|
|
60
77
|
}
|
|
61
|
-
const rs = buildRoute(routeConfigs)
|
|
78
|
+
const rs = buildRoute(routeConfigs)
|
|
62
79
|
if (_indexPath) {
|
|
63
|
-
const idx2 = rs.pages.findIndex((page) => page.path === _indexPath)
|
|
64
|
-
if (idx2 !== -1)
|
|
65
|
-
rs.pages.unshift(...rs.pages.splice(idx2, 1));
|
|
80
|
+
const idx2 = rs.pages.findIndex((page) => page.path === _indexPath)
|
|
81
|
+
if (idx2 !== -1) rs.pages.unshift(...rs.pages.splice(idx2, 1))
|
|
66
82
|
}
|
|
67
|
-
logger.info(
|
|
68
|
-
if (!rs.condition.list.length)
|
|
69
|
-
rs.condition = void 0;
|
|
83
|
+
logger.info('\u8DEF\u7531\u6587\u4EF6\u89E3\u6790\u5B8C\u6210')
|
|
84
|
+
if (!rs.condition.list.length) rs.condition = void 0
|
|
70
85
|
else if (rs.condition.list.length && rs.condition.current === -1)
|
|
71
|
-
rs.condition.current = 0
|
|
72
|
-
const idx = rs.pages.findIndex((page) => page[
|
|
73
|
-
if (idx !== -1)
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
const [tabbarPath] = loadDirFiles("./src", tabbarName);
|
|
86
|
+
rs.condition.current = 0
|
|
87
|
+
const idx = rs.pages.findIndex((page) => page['_index'])
|
|
88
|
+
if (idx !== -1) rs.pages.unshift(...rs.pages.splice(idx, 1))
|
|
89
|
+
let tabbarObj
|
|
90
|
+
const [tabbarPath] = loadDirFiles('./src', tabbarName)
|
|
77
91
|
if (tabbarPath && !isEmpty(tabbarPath)) {
|
|
78
|
-
logger.info(`\u6839\u636E ${tabbarName} \u627E\u52301\u4E2A\u6587\u4EF6`)
|
|
79
|
-
tabbarObj = await loadFileContent(tabbarPath)
|
|
80
|
-
if (tabbarObj && !isEmpty(tabbarObj))
|
|
81
|
-
rs.tabBar = tabbarObj;
|
|
92
|
+
logger.info(`\u6839\u636E ${tabbarName} \u627E\u52301\u4E2A\u6587\u4EF6`)
|
|
93
|
+
tabbarObj = await loadFileContent(tabbarPath)
|
|
94
|
+
if (tabbarObj && !isEmpty(tabbarObj)) rs.tabBar = tabbarObj
|
|
82
95
|
}
|
|
83
96
|
const {
|
|
84
97
|
debug: _debug = false,
|
|
@@ -88,7 +101,7 @@ async function builder(options = {}) {
|
|
|
88
101
|
condition,
|
|
89
102
|
tabBar,
|
|
90
103
|
...otherProps
|
|
91
|
-
} = await loadJson(pagesPath)
|
|
104
|
+
} = await loadJson(pagesPath)
|
|
92
105
|
const writeContent = {
|
|
93
106
|
debug: _debug,
|
|
94
107
|
pages: rs.pages || pages,
|
|
@@ -97,9 +110,9 @@ async function builder(options = {}) {
|
|
|
97
110
|
condition: rs.condition || condition,
|
|
98
111
|
tabBar: tabbarObj || tabBar,
|
|
99
112
|
...otherProps
|
|
100
|
-
}
|
|
101
|
-
await outputFile(pagesPath, JSON.stringify(writeContent, null, 2))
|
|
102
|
-
logger.info(
|
|
113
|
+
}
|
|
114
|
+
await outputFile(pagesPath, JSON.stringify(writeContent, null, 2))
|
|
115
|
+
logger.info('\u8DEF\u7531\u6587\u4EF6\u751F\u6210\u5B8C\u6210\uFF01')
|
|
103
116
|
}
|
|
104
117
|
function buildRoute(content, ext = {}) {
|
|
105
118
|
const result = {
|
|
@@ -110,129 +123,140 @@ function buildRoute(content, ext = {}) {
|
|
|
110
123
|
current: -1,
|
|
111
124
|
list: []
|
|
112
125
|
}
|
|
113
|
-
}
|
|
114
|
-
const { parentName =
|
|
126
|
+
}
|
|
127
|
+
const { parentName = '', parentPath = '', subpackageRoot = '' } = ext
|
|
115
128
|
for (const route of content) {
|
|
116
|
-
const {
|
|
129
|
+
const {
|
|
130
|
+
subPackages = false,
|
|
131
|
+
isSubpackage = subPackages,
|
|
132
|
+
path: path2,
|
|
133
|
+
title,
|
|
134
|
+
name,
|
|
135
|
+
children
|
|
136
|
+
} = route
|
|
117
137
|
const style = {
|
|
118
138
|
navigationBarTitleText: title || name || parentName,
|
|
119
139
|
...route.style
|
|
120
|
-
}
|
|
121
|
-
const _path = pathJoin(parentPath, path2)
|
|
140
|
+
}
|
|
141
|
+
const _path = pathJoin(parentPath, path2)
|
|
122
142
|
if (isSubpackage && !subpackageRoot) {
|
|
123
|
-
let { preLoadPath, preLoadNetwork } = route
|
|
143
|
+
let { preLoadPath, preLoadNetwork } = route
|
|
124
144
|
const { pages, preloadRule, condition } = buildRoute(children ?? [], {
|
|
125
145
|
parentName: style.navigationBarTitleText,
|
|
126
146
|
subpackageRoot: _path
|
|
127
|
-
})
|
|
147
|
+
})
|
|
128
148
|
const subPackage = {
|
|
129
149
|
root: _path,
|
|
130
150
|
pages
|
|
131
|
-
}
|
|
132
|
-
result.preloadRule = deepMerge(result.preloadRule, preloadRule)
|
|
133
|
-
result.condition = deepMerge(result.condition, condition)
|
|
151
|
+
}
|
|
152
|
+
result.preloadRule = deepMerge(result.preloadRule, preloadRule)
|
|
153
|
+
result.condition = deepMerge(result.condition, condition)
|
|
134
154
|
if (condition.current > -1)
|
|
135
|
-
result.condition.current = result.condition?.list?.indexOf(
|
|
136
|
-
|
|
155
|
+
result.condition.current = result.condition?.list?.indexOf(
|
|
156
|
+
condition.list[condition.current]
|
|
157
|
+
)
|
|
158
|
+
result.subPackages.push(subPackage)
|
|
137
159
|
if (preLoadPath) {
|
|
138
|
-
if (!isArray(preLoadPath))
|
|
139
|
-
preLoadPath = [preLoadPath];
|
|
160
|
+
if (!isArray(preLoadPath)) preLoadPath = [preLoadPath]
|
|
140
161
|
const preLoadRule = preLoadPath.reduce((acc, cur) => {
|
|
141
162
|
acc[pathJoin(cur)] = {
|
|
142
163
|
network: preLoadNetwork,
|
|
143
164
|
packages: [_path]
|
|
144
|
-
}
|
|
145
|
-
return acc
|
|
146
|
-
}, {})
|
|
147
|
-
result.preloadRule = deepMerge(result.preloadRule, preLoadRule)
|
|
165
|
+
}
|
|
166
|
+
return acc
|
|
167
|
+
}, {})
|
|
168
|
+
result.preloadRule = deepMerge(result.preloadRule, preLoadRule)
|
|
148
169
|
}
|
|
149
|
-
continue
|
|
170
|
+
continue
|
|
150
171
|
}
|
|
151
172
|
if (children?.length) {
|
|
152
|
-
const {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
result.
|
|
163
|
-
result.
|
|
173
|
+
const {
|
|
174
|
+
pages,
|
|
175
|
+
subPackages: subPackages2,
|
|
176
|
+
preloadRule,
|
|
177
|
+
condition
|
|
178
|
+
} = buildRoute(children, {
|
|
179
|
+
parentName: style.navigationBarTitleText,
|
|
180
|
+
parentPath: _path,
|
|
181
|
+
subpackageRoot
|
|
182
|
+
})
|
|
183
|
+
result.pages = deepMerge(result.pages, pages)
|
|
184
|
+
result.subPackages = deepMerge(result.subPackages, subPackages2)
|
|
185
|
+
result.preloadRule = deepMerge(result.preloadRule, preloadRule)
|
|
186
|
+
result.condition = deepMerge(result.condition, condition)
|
|
164
187
|
if (condition.current > -1)
|
|
165
|
-
result.condition.current = result.condition?.list?.indexOf(
|
|
188
|
+
result.condition.current = result.condition?.list?.indexOf(
|
|
189
|
+
condition.list[condition.current]
|
|
190
|
+
)
|
|
166
191
|
} else {
|
|
167
|
-
const { conditionActive, conditionQuery } = route
|
|
192
|
+
const { conditionActive, conditionQuery } = route
|
|
168
193
|
const item = {
|
|
169
194
|
path: _path,
|
|
170
195
|
style
|
|
171
|
-
}
|
|
172
|
-
route.isIndex && (_indexPath = _path)
|
|
173
|
-
result.pages.push(item)
|
|
196
|
+
}
|
|
197
|
+
route.isIndex && (_indexPath = _path)
|
|
198
|
+
result.pages.push(item)
|
|
174
199
|
if (conditionQuery || conditionActive) {
|
|
175
200
|
const conditionItem = {
|
|
176
201
|
name: style.navigationBarTitleText,
|
|
177
202
|
path: _path,
|
|
178
203
|
query: conditionQuery
|
|
179
|
-
}
|
|
180
|
-
result.condition.list.push(conditionItem)
|
|
204
|
+
}
|
|
205
|
+
result.condition.list.push(conditionItem)
|
|
181
206
|
if (conditionActive)
|
|
182
|
-
result.condition.current =
|
|
207
|
+
result.condition.current =
|
|
208
|
+
result.condition.list?.indexOf(conditionItem)
|
|
183
209
|
}
|
|
184
210
|
}
|
|
185
211
|
}
|
|
186
|
-
return result
|
|
212
|
+
return result
|
|
187
213
|
}
|
|
188
|
-
var extension = [
|
|
214
|
+
var extension = ['.ts', '.js', '.mjs', '.json']
|
|
189
215
|
function loadDirFiles(roots, name, exitByFirst = false) {
|
|
190
|
-
if (!Array.isArray(roots))
|
|
191
|
-
|
|
192
|
-
const files = [];
|
|
216
|
+
if (!Array.isArray(roots)) roots = [roots]
|
|
217
|
+
const files = []
|
|
193
218
|
for (let root of roots) {
|
|
194
|
-
root = path.resolve(root)
|
|
195
|
-
if (!fs.existsSync(root))
|
|
196
|
-
|
|
197
|
-
const
|
|
198
|
-
const regex = new RegExp(`${name}(${extension.join("|")})$`);
|
|
219
|
+
root = path.resolve(root)
|
|
220
|
+
if (!fs.existsSync(root)) continue
|
|
221
|
+
const stat = fs.statSync(root)
|
|
222
|
+
const regex = new RegExp(`${name}(${extension.join('|')})$`)
|
|
199
223
|
if (stat.isFile() && regex.test(root)) {
|
|
200
|
-
files.push(root)
|
|
201
|
-
if (exitByFirst)
|
|
202
|
-
break;
|
|
224
|
+
files.push(root)
|
|
225
|
+
if (exitByFirst) break
|
|
203
226
|
}
|
|
204
227
|
if (stat.isDirectory()) {
|
|
205
|
-
const dirents = fs.readdirSync(root).map((o) => path.resolve(root, o))
|
|
206
|
-
files.push(...loadDirFiles(dirents, name))
|
|
228
|
+
const dirents = fs.readdirSync(root).map((o) => path.resolve(root, o))
|
|
229
|
+
files.push(...loadDirFiles(dirents, name))
|
|
207
230
|
}
|
|
208
231
|
}
|
|
209
|
-
return files
|
|
232
|
+
return files
|
|
210
233
|
}
|
|
211
234
|
function pathJoin(...paths) {
|
|
212
|
-
return paths.join(
|
|
235
|
+
return paths.join('/').split('/').filter(Boolean).join('/')
|
|
213
236
|
}
|
|
214
237
|
function deepMerge(target, ...sources) {
|
|
215
|
-
if (typeof target !==
|
|
216
|
-
|
|
217
|
-
if (!sources.length)
|
|
218
|
-
return target;
|
|
238
|
+
if (typeof target !== 'object' || target === null) target = {}
|
|
239
|
+
if (!sources.length) return target
|
|
219
240
|
for (const source of sources) {
|
|
220
|
-
if (typeof source !==
|
|
221
|
-
continue;
|
|
241
|
+
if (typeof source !== 'object' || source === null) continue
|
|
222
242
|
if (isArray(target) && isArray(source)) {
|
|
223
|
-
const set = /* @__PURE__ */ new Set([...target, ...source])
|
|
224
|
-
return [...set]
|
|
243
|
+
const set = /* @__PURE__ */ new Set([...target, ...source])
|
|
244
|
+
return [...set]
|
|
225
245
|
} else if (isPlainObject(target) && isPlainObject(source)) {
|
|
226
246
|
for (const key in source) {
|
|
227
|
-
if (
|
|
228
|
-
|
|
247
|
+
if (
|
|
248
|
+
key in target &&
|
|
249
|
+
typeof target[key] === 'object' &&
|
|
250
|
+
typeof source[key] === 'object'
|
|
251
|
+
) {
|
|
252
|
+
target[key] = deepMerge(target[key], source[key])
|
|
229
253
|
} else {
|
|
230
|
-
target[key] = source[key] || target[key]
|
|
254
|
+
target[key] = source[key] || target[key]
|
|
231
255
|
}
|
|
232
256
|
}
|
|
233
257
|
}
|
|
234
258
|
}
|
|
235
|
-
return target
|
|
259
|
+
return target
|
|
236
260
|
}
|
|
237
261
|
|
|
238
|
-
export { builder, builderWithWatch }
|
|
262
|
+
export { builder, builderWithWatch }
|
package/dist/cli.cjs
CHANGED
|
@@ -1,96 +1,111 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
'use strict'
|
|
2
|
+
'use strict'
|
|
3
3
|
|
|
4
|
-
var commander = require('commander')
|
|
5
|
-
var path = require('path')
|
|
6
|
-
var fs = require('fs')
|
|
7
|
-
var pino = require('pino')
|
|
8
|
-
var mpShared = require('@
|
|
9
|
-
var lodashEs = require('lodash-es')
|
|
10
|
-
var chokidar = require('chokidar')
|
|
4
|
+
var commander = require('commander')
|
|
5
|
+
var path = require('path')
|
|
6
|
+
var fs = require('fs')
|
|
7
|
+
var pino = require('pino')
|
|
8
|
+
var mpShared = require('@mpilk/mp-shared')
|
|
9
|
+
var lodashEs = require('lodash-es')
|
|
10
|
+
var chokidar = require('chokidar')
|
|
11
11
|
|
|
12
|
-
function _interopDefault
|
|
12
|
+
function _interopDefault(e) {
|
|
13
|
+
return e && e.__esModule ? e : { default: e }
|
|
14
|
+
}
|
|
13
15
|
|
|
14
|
-
var path__default = /*#__PURE__*/_interopDefault(path)
|
|
15
|
-
var fs__default = /*#__PURE__*/_interopDefault(fs)
|
|
16
|
-
var pino__default = /*#__PURE__*/_interopDefault(pino)
|
|
17
|
-
var chokidar__default = /*#__PURE__*/_interopDefault(chokidar)
|
|
16
|
+
var path__default = /*#__PURE__*/ _interopDefault(path)
|
|
17
|
+
var fs__default = /*#__PURE__*/ _interopDefault(fs)
|
|
18
|
+
var pino__default = /*#__PURE__*/ _interopDefault(pino)
|
|
19
|
+
var chokidar__default = /*#__PURE__*/ _interopDefault(chokidar)
|
|
18
20
|
|
|
19
21
|
// package.json
|
|
20
|
-
var version =
|
|
21
|
-
var _indexPath =
|
|
22
|
+
var version = '1.1.3'
|
|
23
|
+
var _indexPath = ''
|
|
22
24
|
var logger = pino__default.default({
|
|
23
25
|
transport: {
|
|
24
|
-
target:
|
|
26
|
+
target: 'pino-pretty',
|
|
25
27
|
options: {
|
|
26
28
|
colorize: true
|
|
27
29
|
// ignore: 'hostname,pid',
|
|
28
30
|
}
|
|
29
31
|
}
|
|
30
|
-
})
|
|
31
|
-
var verboseLogger = logger.child({ verbose: true })
|
|
32
|
+
})
|
|
33
|
+
var verboseLogger = logger.child({ verbose: true })
|
|
32
34
|
function builderWithWatch(options) {
|
|
33
|
-
let timer = null
|
|
35
|
+
let timer = null
|
|
34
36
|
function run() {
|
|
35
|
-
if (timer)
|
|
36
|
-
clearTimeout(timer);
|
|
37
|
+
if (timer) clearTimeout(timer)
|
|
37
38
|
timer = setTimeout(() => {
|
|
38
|
-
timer = null
|
|
39
|
-
return builder(options)
|
|
40
|
-
}, options?.delay || 200)
|
|
39
|
+
timer = null
|
|
40
|
+
return builder(options)
|
|
41
|
+
}, options?.delay || 200)
|
|
41
42
|
}
|
|
42
|
-
return chokidar__default.default
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
43
|
+
return chokidar__default.default
|
|
44
|
+
.watch([
|
|
45
|
+
`src/**/${options?.routeName || 'route'}.{ts,js}`,
|
|
46
|
+
`src/${options?.tabbarName || 'tabbar'}.{ts,js}`
|
|
47
|
+
])
|
|
48
|
+
.on('all', (eventName) => {
|
|
49
|
+
switch (eventName) {
|
|
50
|
+
case 'add':
|
|
51
|
+
case 'change':
|
|
52
|
+
case 'unlink':
|
|
53
|
+
run()
|
|
54
|
+
break
|
|
55
|
+
}
|
|
56
|
+
})
|
|
54
57
|
}
|
|
55
58
|
async function builder(options = {}) {
|
|
56
|
-
const {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
59
|
+
const {
|
|
60
|
+
input = './src',
|
|
61
|
+
routeName = 'route',
|
|
62
|
+
tabbarName = 'tabbar',
|
|
63
|
+
pagesPath = './src/pages.json',
|
|
64
|
+
verbose
|
|
65
|
+
} = options
|
|
66
|
+
logger.info('\u5F00\u59CB\u751F\u6210\u8DEF\u7531\u6587\u4EF6...')
|
|
67
|
+
const filePaths = loadDirFiles(input, routeName)
|
|
68
|
+
logger.info(
|
|
69
|
+
`\u6839\u636E ${routeName} \u627E\u5230 ${filePaths.length} \u4E2A\u6587\u4EF6`
|
|
70
|
+
)
|
|
71
|
+
const routeConfigs = []
|
|
61
72
|
for (const filePath of filePaths) {
|
|
62
|
-
verbose &&
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
73
|
+
verbose &&
|
|
74
|
+
verboseLogger.info(`\u5F00\u59CB\u89E3\u6790\u6587\u4EF6 ${filePath}`)
|
|
75
|
+
let content = await mpShared.loadFileContent(filePath)
|
|
76
|
+
if (!content || typeof content !== 'object' || lodashEs.isEmpty(content)) {
|
|
77
|
+
verbose &&
|
|
78
|
+
verboseLogger.info(
|
|
79
|
+
`\u6587\u4EF6 ${filePath} \u4E3A\u7A7A\uFF0C\u8DF3\u8FC7`
|
|
80
|
+
)
|
|
81
|
+
continue
|
|
67
82
|
}
|
|
68
|
-
if (!lodashEs.isArray(content))
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
83
|
+
if (!lodashEs.isArray(content)) content = [content]
|
|
84
|
+
verbose &&
|
|
85
|
+
verboseLogger.info(
|
|
86
|
+
`\u6587\u4EF6 ${filePath} \u89E3\u6790\u5B8C\u6210\uFF1A${JSON.stringify(
|
|
87
|
+
content
|
|
88
|
+
)}`
|
|
89
|
+
)
|
|
90
|
+
routeConfigs.push(...content)
|
|
72
91
|
}
|
|
73
|
-
const rs = buildRoute(routeConfigs)
|
|
92
|
+
const rs = buildRoute(routeConfigs)
|
|
74
93
|
if (_indexPath) {
|
|
75
|
-
const idx2 = rs.pages.findIndex((page) => page.path === _indexPath)
|
|
76
|
-
if (idx2 !== -1)
|
|
77
|
-
rs.pages.unshift(...rs.pages.splice(idx2, 1));
|
|
94
|
+
const idx2 = rs.pages.findIndex((page) => page.path === _indexPath)
|
|
95
|
+
if (idx2 !== -1) rs.pages.unshift(...rs.pages.splice(idx2, 1))
|
|
78
96
|
}
|
|
79
|
-
logger.info(
|
|
80
|
-
if (!rs.condition.list.length)
|
|
81
|
-
rs.condition = void 0;
|
|
97
|
+
logger.info('\u8DEF\u7531\u6587\u4EF6\u89E3\u6790\u5B8C\u6210')
|
|
98
|
+
if (!rs.condition.list.length) rs.condition = void 0
|
|
82
99
|
else if (rs.condition.list.length && rs.condition.current === -1)
|
|
83
|
-
rs.condition.current = 0
|
|
84
|
-
const idx = rs.pages.findIndex((page) => page[
|
|
85
|
-
if (idx !== -1)
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
const [tabbarPath] = loadDirFiles("./src", tabbarName);
|
|
100
|
+
rs.condition.current = 0
|
|
101
|
+
const idx = rs.pages.findIndex((page) => page['_index'])
|
|
102
|
+
if (idx !== -1) rs.pages.unshift(...rs.pages.splice(idx, 1))
|
|
103
|
+
let tabbarObj
|
|
104
|
+
const [tabbarPath] = loadDirFiles('./src', tabbarName)
|
|
89
105
|
if (tabbarPath && !lodashEs.isEmpty(tabbarPath)) {
|
|
90
|
-
logger.info(`\u6839\u636E ${tabbarName} \u627E\u52301\u4E2A\u6587\u4EF6`)
|
|
91
|
-
tabbarObj = await mpShared.loadFileContent(tabbarPath)
|
|
92
|
-
if (tabbarObj && !lodashEs.isEmpty(tabbarObj))
|
|
93
|
-
rs.tabBar = tabbarObj;
|
|
106
|
+
logger.info(`\u6839\u636E ${tabbarName} \u627E\u52301\u4E2A\u6587\u4EF6`)
|
|
107
|
+
tabbarObj = await mpShared.loadFileContent(tabbarPath)
|
|
108
|
+
if (tabbarObj && !lodashEs.isEmpty(tabbarObj)) rs.tabBar = tabbarObj
|
|
94
109
|
}
|
|
95
110
|
const {
|
|
96
111
|
debug: _debug = false,
|
|
@@ -100,7 +115,7 @@ async function builder(options = {}) {
|
|
|
100
115
|
condition,
|
|
101
116
|
tabBar,
|
|
102
117
|
...otherProps
|
|
103
|
-
} = await mpShared.loadJson(pagesPath)
|
|
118
|
+
} = await mpShared.loadJson(pagesPath)
|
|
104
119
|
const writeContent = {
|
|
105
120
|
debug: _debug,
|
|
106
121
|
pages: rs.pages || pages,
|
|
@@ -109,9 +124,9 @@ async function builder(options = {}) {
|
|
|
109
124
|
condition: rs.condition || condition,
|
|
110
125
|
tabBar: tabbarObj || tabBar,
|
|
111
126
|
...otherProps
|
|
112
|
-
}
|
|
113
|
-
await mpShared.outputFile(pagesPath, JSON.stringify(writeContent, null, 2))
|
|
114
|
-
logger.info(
|
|
127
|
+
}
|
|
128
|
+
await mpShared.outputFile(pagesPath, JSON.stringify(writeContent, null, 2))
|
|
129
|
+
logger.info('\u8DEF\u7531\u6587\u4EF6\u751F\u6210\u5B8C\u6210\uFF01')
|
|
115
130
|
}
|
|
116
131
|
function buildRoute(content, ext = {}) {
|
|
117
132
|
const result = {
|
|
@@ -122,139 +137,195 @@ function buildRoute(content, ext = {}) {
|
|
|
122
137
|
current: -1,
|
|
123
138
|
list: []
|
|
124
139
|
}
|
|
125
|
-
}
|
|
126
|
-
const { parentName =
|
|
140
|
+
}
|
|
141
|
+
const { parentName = '', parentPath = '', subpackageRoot = '' } = ext
|
|
127
142
|
for (const route of content) {
|
|
128
|
-
const {
|
|
143
|
+
const {
|
|
144
|
+
subPackages = false,
|
|
145
|
+
isSubpackage = subPackages,
|
|
146
|
+
path: path2,
|
|
147
|
+
title,
|
|
148
|
+
name,
|
|
149
|
+
children
|
|
150
|
+
} = route
|
|
129
151
|
const style = {
|
|
130
152
|
navigationBarTitleText: title || name || parentName,
|
|
131
153
|
...route.style
|
|
132
|
-
}
|
|
133
|
-
const _path = pathJoin(parentPath, path2)
|
|
154
|
+
}
|
|
155
|
+
const _path = pathJoin(parentPath, path2)
|
|
134
156
|
if (isSubpackage && !subpackageRoot) {
|
|
135
|
-
let { preLoadPath, preLoadNetwork } = route
|
|
157
|
+
let { preLoadPath, preLoadNetwork } = route
|
|
136
158
|
const { pages, preloadRule, condition } = buildRoute(children ?? [], {
|
|
137
159
|
parentName: style.navigationBarTitleText,
|
|
138
160
|
subpackageRoot: _path
|
|
139
|
-
})
|
|
161
|
+
})
|
|
140
162
|
const subPackage = {
|
|
141
163
|
root: _path,
|
|
142
164
|
pages
|
|
143
|
-
}
|
|
144
|
-
result.preloadRule = deepMerge(result.preloadRule, preloadRule)
|
|
145
|
-
result.condition = deepMerge(result.condition, condition)
|
|
165
|
+
}
|
|
166
|
+
result.preloadRule = deepMerge(result.preloadRule, preloadRule)
|
|
167
|
+
result.condition = deepMerge(result.condition, condition)
|
|
146
168
|
if (condition.current > -1)
|
|
147
|
-
result.condition.current = result.condition?.list?.indexOf(
|
|
148
|
-
|
|
169
|
+
result.condition.current = result.condition?.list?.indexOf(
|
|
170
|
+
condition.list[condition.current]
|
|
171
|
+
)
|
|
172
|
+
result.subPackages.push(subPackage)
|
|
149
173
|
if (preLoadPath) {
|
|
150
|
-
if (!lodashEs.isArray(preLoadPath))
|
|
151
|
-
preLoadPath = [preLoadPath];
|
|
174
|
+
if (!lodashEs.isArray(preLoadPath)) preLoadPath = [preLoadPath]
|
|
152
175
|
const preLoadRule = preLoadPath.reduce((acc, cur) => {
|
|
153
176
|
acc[pathJoin(cur)] = {
|
|
154
177
|
network: preLoadNetwork,
|
|
155
178
|
packages: [_path]
|
|
156
|
-
}
|
|
157
|
-
return acc
|
|
158
|
-
}, {})
|
|
159
|
-
result.preloadRule = deepMerge(result.preloadRule, preLoadRule)
|
|
179
|
+
}
|
|
180
|
+
return acc
|
|
181
|
+
}, {})
|
|
182
|
+
result.preloadRule = deepMerge(result.preloadRule, preLoadRule)
|
|
160
183
|
}
|
|
161
|
-
continue
|
|
184
|
+
continue
|
|
162
185
|
}
|
|
163
186
|
if (children?.length) {
|
|
164
|
-
const {
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
result.
|
|
175
|
-
result.
|
|
187
|
+
const {
|
|
188
|
+
pages,
|
|
189
|
+
subPackages: subPackages2,
|
|
190
|
+
preloadRule,
|
|
191
|
+
condition
|
|
192
|
+
} = buildRoute(children, {
|
|
193
|
+
parentName: style.navigationBarTitleText,
|
|
194
|
+
parentPath: _path,
|
|
195
|
+
subpackageRoot
|
|
196
|
+
})
|
|
197
|
+
result.pages = deepMerge(result.pages, pages)
|
|
198
|
+
result.subPackages = deepMerge(result.subPackages, subPackages2)
|
|
199
|
+
result.preloadRule = deepMerge(result.preloadRule, preloadRule)
|
|
200
|
+
result.condition = deepMerge(result.condition, condition)
|
|
176
201
|
if (condition.current > -1)
|
|
177
|
-
result.condition.current = result.condition?.list?.indexOf(
|
|
202
|
+
result.condition.current = result.condition?.list?.indexOf(
|
|
203
|
+
condition.list[condition.current]
|
|
204
|
+
)
|
|
178
205
|
} else {
|
|
179
|
-
const { conditionActive, conditionQuery } = route
|
|
206
|
+
const { conditionActive, conditionQuery } = route
|
|
180
207
|
const item = {
|
|
181
208
|
path: _path,
|
|
182
209
|
style
|
|
183
|
-
}
|
|
184
|
-
route.isIndex && (_indexPath = _path)
|
|
185
|
-
result.pages.push(item)
|
|
210
|
+
}
|
|
211
|
+
route.isIndex && (_indexPath = _path)
|
|
212
|
+
result.pages.push(item)
|
|
186
213
|
if (conditionQuery || conditionActive) {
|
|
187
214
|
const conditionItem = {
|
|
188
215
|
name: style.navigationBarTitleText,
|
|
189
216
|
path: _path,
|
|
190
217
|
query: conditionQuery
|
|
191
|
-
}
|
|
192
|
-
result.condition.list.push(conditionItem)
|
|
218
|
+
}
|
|
219
|
+
result.condition.list.push(conditionItem)
|
|
193
220
|
if (conditionActive)
|
|
194
|
-
result.condition.current =
|
|
221
|
+
result.condition.current =
|
|
222
|
+
result.condition.list?.indexOf(conditionItem)
|
|
195
223
|
}
|
|
196
224
|
}
|
|
197
225
|
}
|
|
198
|
-
return result
|
|
226
|
+
return result
|
|
199
227
|
}
|
|
200
|
-
var extension = [
|
|
228
|
+
var extension = ['.ts', '.js', '.mjs', '.json']
|
|
201
229
|
function loadDirFiles(roots, name, exitByFirst = false) {
|
|
202
|
-
if (!Array.isArray(roots))
|
|
203
|
-
|
|
204
|
-
const files = [];
|
|
230
|
+
if (!Array.isArray(roots)) roots = [roots]
|
|
231
|
+
const files = []
|
|
205
232
|
for (let root of roots) {
|
|
206
|
-
root = path__default.default.resolve(root)
|
|
207
|
-
if (!fs__default.default.existsSync(root))
|
|
208
|
-
|
|
209
|
-
const
|
|
210
|
-
const regex = new RegExp(`${name}(${extension.join("|")})$`);
|
|
233
|
+
root = path__default.default.resolve(root)
|
|
234
|
+
if (!fs__default.default.existsSync(root)) continue
|
|
235
|
+
const stat = fs__default.default.statSync(root)
|
|
236
|
+
const regex = new RegExp(`${name}(${extension.join('|')})$`)
|
|
211
237
|
if (stat.isFile() && regex.test(root)) {
|
|
212
|
-
files.push(root)
|
|
213
|
-
if (exitByFirst)
|
|
214
|
-
break;
|
|
238
|
+
files.push(root)
|
|
239
|
+
if (exitByFirst) break
|
|
215
240
|
}
|
|
216
241
|
if (stat.isDirectory()) {
|
|
217
|
-
const dirents = fs__default.default
|
|
218
|
-
|
|
242
|
+
const dirents = fs__default.default
|
|
243
|
+
.readdirSync(root)
|
|
244
|
+
.map((o) => path__default.default.resolve(root, o))
|
|
245
|
+
files.push(...loadDirFiles(dirents, name))
|
|
219
246
|
}
|
|
220
247
|
}
|
|
221
|
-
return files
|
|
248
|
+
return files
|
|
222
249
|
}
|
|
223
250
|
function pathJoin(...paths) {
|
|
224
|
-
return paths.join(
|
|
251
|
+
return paths.join('/').split('/').filter(Boolean).join('/')
|
|
225
252
|
}
|
|
226
253
|
function deepMerge(target, ...sources) {
|
|
227
|
-
if (typeof target !==
|
|
228
|
-
|
|
229
|
-
if (!sources.length)
|
|
230
|
-
return target;
|
|
254
|
+
if (typeof target !== 'object' || target === null) target = {}
|
|
255
|
+
if (!sources.length) return target
|
|
231
256
|
for (const source of sources) {
|
|
232
|
-
if (typeof source !==
|
|
233
|
-
continue;
|
|
257
|
+
if (typeof source !== 'object' || source === null) continue
|
|
234
258
|
if (lodashEs.isArray(target) && lodashEs.isArray(source)) {
|
|
235
|
-
const set = /* @__PURE__ */ new Set([...target, ...source])
|
|
236
|
-
return [...set]
|
|
237
|
-
} else if (
|
|
259
|
+
const set = /* @__PURE__ */ new Set([...target, ...source])
|
|
260
|
+
return [...set]
|
|
261
|
+
} else if (
|
|
262
|
+
lodashEs.isPlainObject(target) &&
|
|
263
|
+
lodashEs.isPlainObject(source)
|
|
264
|
+
) {
|
|
238
265
|
for (const key in source) {
|
|
239
|
-
if (
|
|
240
|
-
|
|
266
|
+
if (
|
|
267
|
+
key in target &&
|
|
268
|
+
typeof target[key] === 'object' &&
|
|
269
|
+
typeof source[key] === 'object'
|
|
270
|
+
) {
|
|
271
|
+
target[key] = deepMerge(target[key], source[key])
|
|
241
272
|
} else {
|
|
242
|
-
target[key] = source[key] || target[key]
|
|
273
|
+
target[key] = source[key] || target[key]
|
|
243
274
|
}
|
|
244
275
|
}
|
|
245
276
|
}
|
|
246
277
|
}
|
|
247
|
-
return target
|
|
278
|
+
return target
|
|
248
279
|
}
|
|
249
280
|
|
|
250
281
|
// src/cli.ts
|
|
251
|
-
commander.program
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
282
|
+
commander.program
|
|
283
|
+
.name('uni-route')
|
|
284
|
+
.description(
|
|
285
|
+
`\u8FD9\u662F\u4E00\u4E2A\u7528\u6765\u751F\u6210 uni-app \u8DEF\u7531\u6587\u4EF6\u7684\u5DE5\u5177
|
|
286
|
+
\u901A\u8FC7\u626B\u63CFsrc\u76EE\u5F55\u4E0B\u7684\u914D\u7F6E\u6587\u4EF6\uFF0C\u751F\u6210 uni-app \u8DEF\u7531\u914D\u7F6E\u6587\u4EF6\u3002`
|
|
287
|
+
)
|
|
288
|
+
.version(version)
|
|
289
|
+
commander.program
|
|
290
|
+
.option(
|
|
291
|
+
'-i, --input <input>',
|
|
292
|
+
'\u626B\u63CF\u914D\u7F6E\u7684\u6587\u4EF6\u5939\u5165\u53E3\u8DEF\u5F84',
|
|
293
|
+
'src/'
|
|
294
|
+
)
|
|
295
|
+
.option(
|
|
296
|
+
'-rn, --routeName <routeName>',
|
|
297
|
+
'\u914D\u7F6E\u6587\u4EF6\u8DEF\u5F84',
|
|
298
|
+
'route'
|
|
299
|
+
)
|
|
300
|
+
.option(
|
|
301
|
+
'-tn, --tabbarName <tabbarName>',
|
|
302
|
+
'\u914D\u7F6E\u6587\u4EF6\u8DEF\u5F84',
|
|
303
|
+
'tabbar'
|
|
304
|
+
)
|
|
305
|
+
.option(
|
|
306
|
+
'-pp, --pagesPath <pagesPath>',
|
|
307
|
+
'\u914D\u7F6E\u6587\u4EF6\u8DEF\u5F84',
|
|
308
|
+
'src/pages.json'
|
|
309
|
+
)
|
|
310
|
+
.option(
|
|
311
|
+
'-vvv, --verbose',
|
|
312
|
+
'\u8F93\u51FA\u8BE6\u7EC6\u7684\u8C03\u8BD5\u4FE1\u606F'
|
|
313
|
+
)
|
|
314
|
+
commander.program
|
|
315
|
+
.command('build', { isDefault: true })
|
|
316
|
+
.description(
|
|
317
|
+
'\u641C\u7D22\u8DEF\u5F84\u4E0B\u7684\u8DEF\u7531\u6587\u4EF6\uFF0C\u751F\u6210\u8DEF\u7531\u914D\u7F6E'
|
|
318
|
+
)
|
|
319
|
+
.action((_, command) => {
|
|
320
|
+
builder(command.optsWithGlobals())
|
|
321
|
+
})
|
|
322
|
+
commander.program
|
|
323
|
+
.command('watch')
|
|
324
|
+
.description(
|
|
325
|
+
'\u76D1\u542C\u6587\u4EF6\u53D8\u5316\uFF0C\u5B9E\u65F6\u751F\u6210\u8DEF\u7531\u914D\u7F6E'
|
|
326
|
+
)
|
|
327
|
+
.option('-t --delay [delay]', '\u5EF6\u8FDF\u65F6\u95F4')
|
|
328
|
+
.action((_, command) => {
|
|
329
|
+
builderWithWatch(command.optsWithGlobals())
|
|
330
|
+
})
|
|
331
|
+
commander.program.parse()
|
package/dist/index.cjs
CHANGED
|
@@ -1,99 +1,114 @@
|
|
|
1
|
-
'use strict'
|
|
1
|
+
'use strict'
|
|
2
2
|
|
|
3
|
-
var path = require('path')
|
|
4
|
-
var fs = require('fs')
|
|
5
|
-
var pino = require('pino')
|
|
6
|
-
var mpShared = require('@
|
|
7
|
-
var lodashEs = require('lodash-es')
|
|
8
|
-
var chokidar = require('chokidar')
|
|
3
|
+
var path = require('path')
|
|
4
|
+
var fs = require('fs')
|
|
5
|
+
var pino = require('pino')
|
|
6
|
+
var mpShared = require('@mpilk/mp-shared')
|
|
7
|
+
var lodashEs = require('lodash-es')
|
|
8
|
+
var chokidar = require('chokidar')
|
|
9
9
|
|
|
10
|
-
function _interopDefault
|
|
10
|
+
function _interopDefault(e) {
|
|
11
|
+
return e && e.__esModule ? e : { default: e }
|
|
12
|
+
}
|
|
11
13
|
|
|
12
|
-
var path__default = /*#__PURE__*/_interopDefault(path)
|
|
13
|
-
var fs__default = /*#__PURE__*/_interopDefault(fs)
|
|
14
|
-
var pino__default = /*#__PURE__*/_interopDefault(pino)
|
|
15
|
-
var chokidar__default = /*#__PURE__*/_interopDefault(chokidar)
|
|
14
|
+
var path__default = /*#__PURE__*/ _interopDefault(path)
|
|
15
|
+
var fs__default = /*#__PURE__*/ _interopDefault(fs)
|
|
16
|
+
var pino__default = /*#__PURE__*/ _interopDefault(pino)
|
|
17
|
+
var chokidar__default = /*#__PURE__*/ _interopDefault(chokidar)
|
|
16
18
|
|
|
17
19
|
// src/helper.ts
|
|
18
20
|
function defineMiniRouter(config) {
|
|
19
|
-
return config
|
|
21
|
+
return config
|
|
20
22
|
}
|
|
21
23
|
function defineMiniTabbar(config) {
|
|
22
|
-
return config
|
|
24
|
+
return config
|
|
23
25
|
}
|
|
24
|
-
var _indexPath =
|
|
26
|
+
var _indexPath = ''
|
|
25
27
|
var logger = pino__default.default({
|
|
26
28
|
transport: {
|
|
27
|
-
target:
|
|
29
|
+
target: 'pino-pretty',
|
|
28
30
|
options: {
|
|
29
31
|
colorize: true
|
|
30
32
|
// ignore: 'hostname,pid',
|
|
31
33
|
}
|
|
32
34
|
}
|
|
33
|
-
})
|
|
34
|
-
var verboseLogger = logger.child({ verbose: true })
|
|
35
|
+
})
|
|
36
|
+
var verboseLogger = logger.child({ verbose: true })
|
|
35
37
|
function builderWithWatch(options) {
|
|
36
|
-
let timer = null
|
|
38
|
+
let timer = null
|
|
37
39
|
function run() {
|
|
38
|
-
if (timer)
|
|
39
|
-
clearTimeout(timer);
|
|
40
|
+
if (timer) clearTimeout(timer)
|
|
40
41
|
timer = setTimeout(() => {
|
|
41
|
-
timer = null
|
|
42
|
-
return builder(options)
|
|
43
|
-
}, options?.delay || 200)
|
|
42
|
+
timer = null
|
|
43
|
+
return builder(options)
|
|
44
|
+
}, options?.delay || 200)
|
|
44
45
|
}
|
|
45
|
-
return chokidar__default.default
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
46
|
+
return chokidar__default.default
|
|
47
|
+
.watch([
|
|
48
|
+
`src/**/${options?.routeName || 'route'}.{ts,js}`,
|
|
49
|
+
`src/${options?.tabbarName || 'tabbar'}.{ts,js}`
|
|
50
|
+
])
|
|
51
|
+
.on('all', (eventName) => {
|
|
52
|
+
switch (eventName) {
|
|
53
|
+
case 'add':
|
|
54
|
+
case 'change':
|
|
55
|
+
case 'unlink':
|
|
56
|
+
run()
|
|
57
|
+
break
|
|
58
|
+
}
|
|
59
|
+
})
|
|
57
60
|
}
|
|
58
61
|
async function builder(options = {}) {
|
|
59
|
-
const {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
62
|
+
const {
|
|
63
|
+
input = './src',
|
|
64
|
+
routeName = 'route',
|
|
65
|
+
tabbarName = 'tabbar',
|
|
66
|
+
pagesPath = './src/pages.json',
|
|
67
|
+
verbose
|
|
68
|
+
} = options
|
|
69
|
+
logger.info('\u5F00\u59CB\u751F\u6210\u8DEF\u7531\u6587\u4EF6...')
|
|
70
|
+
const filePaths = loadDirFiles(input, routeName)
|
|
71
|
+
logger.info(
|
|
72
|
+
`\u6839\u636E ${routeName} \u627E\u5230 ${filePaths.length} \u4E2A\u6587\u4EF6`
|
|
73
|
+
)
|
|
74
|
+
const routeConfigs = []
|
|
64
75
|
for (const filePath of filePaths) {
|
|
65
|
-
verbose &&
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
76
|
+
verbose &&
|
|
77
|
+
verboseLogger.info(`\u5F00\u59CB\u89E3\u6790\u6587\u4EF6 ${filePath}`)
|
|
78
|
+
let content = await mpShared.loadFileContent(filePath)
|
|
79
|
+
if (!content || typeof content !== 'object' || lodashEs.isEmpty(content)) {
|
|
80
|
+
verbose &&
|
|
81
|
+
verboseLogger.info(
|
|
82
|
+
`\u6587\u4EF6 ${filePath} \u4E3A\u7A7A\uFF0C\u8DF3\u8FC7`
|
|
83
|
+
)
|
|
84
|
+
continue
|
|
70
85
|
}
|
|
71
|
-
if (!lodashEs.isArray(content))
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
86
|
+
if (!lodashEs.isArray(content)) content = [content]
|
|
87
|
+
verbose &&
|
|
88
|
+
verboseLogger.info(
|
|
89
|
+
`\u6587\u4EF6 ${filePath} \u89E3\u6790\u5B8C\u6210\uFF1A${JSON.stringify(
|
|
90
|
+
content
|
|
91
|
+
)}`
|
|
92
|
+
)
|
|
93
|
+
routeConfigs.push(...content)
|
|
75
94
|
}
|
|
76
|
-
const rs = buildRoute(routeConfigs)
|
|
95
|
+
const rs = buildRoute(routeConfigs)
|
|
77
96
|
if (_indexPath) {
|
|
78
|
-
const idx2 = rs.pages.findIndex((page) => page.path === _indexPath)
|
|
79
|
-
if (idx2 !== -1)
|
|
80
|
-
rs.pages.unshift(...rs.pages.splice(idx2, 1));
|
|
97
|
+
const idx2 = rs.pages.findIndex((page) => page.path === _indexPath)
|
|
98
|
+
if (idx2 !== -1) rs.pages.unshift(...rs.pages.splice(idx2, 1))
|
|
81
99
|
}
|
|
82
|
-
logger.info(
|
|
83
|
-
if (!rs.condition.list.length)
|
|
84
|
-
rs.condition = void 0;
|
|
100
|
+
logger.info('\u8DEF\u7531\u6587\u4EF6\u89E3\u6790\u5B8C\u6210')
|
|
101
|
+
if (!rs.condition.list.length) rs.condition = void 0
|
|
85
102
|
else if (rs.condition.list.length && rs.condition.current === -1)
|
|
86
|
-
rs.condition.current = 0
|
|
87
|
-
const idx = rs.pages.findIndex((page) => page[
|
|
88
|
-
if (idx !== -1)
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
const [tabbarPath] = loadDirFiles("./src", tabbarName);
|
|
103
|
+
rs.condition.current = 0
|
|
104
|
+
const idx = rs.pages.findIndex((page) => page['_index'])
|
|
105
|
+
if (idx !== -1) rs.pages.unshift(...rs.pages.splice(idx, 1))
|
|
106
|
+
let tabbarObj
|
|
107
|
+
const [tabbarPath] = loadDirFiles('./src', tabbarName)
|
|
92
108
|
if (tabbarPath && !lodashEs.isEmpty(tabbarPath)) {
|
|
93
|
-
logger.info(`\u6839\u636E ${tabbarName} \u627E\u52301\u4E2A\u6587\u4EF6`)
|
|
94
|
-
tabbarObj = await mpShared.loadFileContent(tabbarPath)
|
|
95
|
-
if (tabbarObj && !lodashEs.isEmpty(tabbarObj))
|
|
96
|
-
rs.tabBar = tabbarObj;
|
|
109
|
+
logger.info(`\u6839\u636E ${tabbarName} \u627E\u52301\u4E2A\u6587\u4EF6`)
|
|
110
|
+
tabbarObj = await mpShared.loadFileContent(tabbarPath)
|
|
111
|
+
if (tabbarObj && !lodashEs.isEmpty(tabbarObj)) rs.tabBar = tabbarObj
|
|
97
112
|
}
|
|
98
113
|
const {
|
|
99
114
|
debug: _debug = false,
|
|
@@ -103,7 +118,7 @@ async function builder(options = {}) {
|
|
|
103
118
|
condition,
|
|
104
119
|
tabBar,
|
|
105
120
|
...otherProps
|
|
106
|
-
} = await mpShared.loadJson(pagesPath)
|
|
121
|
+
} = await mpShared.loadJson(pagesPath)
|
|
107
122
|
const writeContent = {
|
|
108
123
|
debug: _debug,
|
|
109
124
|
pages: rs.pages || pages,
|
|
@@ -112,9 +127,9 @@ async function builder(options = {}) {
|
|
|
112
127
|
condition: rs.condition || condition,
|
|
113
128
|
tabBar: tabbarObj || tabBar,
|
|
114
129
|
...otherProps
|
|
115
|
-
}
|
|
116
|
-
await mpShared.outputFile(pagesPath, JSON.stringify(writeContent, null, 2))
|
|
117
|
-
logger.info(
|
|
130
|
+
}
|
|
131
|
+
await mpShared.outputFile(pagesPath, JSON.stringify(writeContent, null, 2))
|
|
132
|
+
logger.info('\u8DEF\u7531\u6587\u4EF6\u751F\u6210\u5B8C\u6210\uFF01')
|
|
118
133
|
}
|
|
119
134
|
function buildRoute(content, ext = {}) {
|
|
120
135
|
const result = {
|
|
@@ -125,132 +140,148 @@ function buildRoute(content, ext = {}) {
|
|
|
125
140
|
current: -1,
|
|
126
141
|
list: []
|
|
127
142
|
}
|
|
128
|
-
}
|
|
129
|
-
const { parentName =
|
|
143
|
+
}
|
|
144
|
+
const { parentName = '', parentPath = '', subpackageRoot = '' } = ext
|
|
130
145
|
for (const route of content) {
|
|
131
|
-
const {
|
|
146
|
+
const {
|
|
147
|
+
subPackages = false,
|
|
148
|
+
isSubpackage = subPackages,
|
|
149
|
+
path: path2,
|
|
150
|
+
title,
|
|
151
|
+
name,
|
|
152
|
+
children
|
|
153
|
+
} = route
|
|
132
154
|
const style = {
|
|
133
155
|
navigationBarTitleText: title || name || parentName,
|
|
134
156
|
...route.style
|
|
135
|
-
}
|
|
136
|
-
const _path = pathJoin(parentPath, path2)
|
|
157
|
+
}
|
|
158
|
+
const _path = pathJoin(parentPath, path2)
|
|
137
159
|
if (isSubpackage && !subpackageRoot) {
|
|
138
|
-
let { preLoadPath, preLoadNetwork } = route
|
|
160
|
+
let { preLoadPath, preLoadNetwork } = route
|
|
139
161
|
const { pages, preloadRule, condition } = buildRoute(children ?? [], {
|
|
140
162
|
parentName: style.navigationBarTitleText,
|
|
141
163
|
subpackageRoot: _path
|
|
142
|
-
})
|
|
164
|
+
})
|
|
143
165
|
const subPackage = {
|
|
144
166
|
root: _path,
|
|
145
167
|
pages
|
|
146
|
-
}
|
|
147
|
-
result.preloadRule = deepMerge(result.preloadRule, preloadRule)
|
|
148
|
-
result.condition = deepMerge(result.condition, condition)
|
|
168
|
+
}
|
|
169
|
+
result.preloadRule = deepMerge(result.preloadRule, preloadRule)
|
|
170
|
+
result.condition = deepMerge(result.condition, condition)
|
|
149
171
|
if (condition.current > -1)
|
|
150
|
-
result.condition.current = result.condition?.list?.indexOf(
|
|
151
|
-
|
|
172
|
+
result.condition.current = result.condition?.list?.indexOf(
|
|
173
|
+
condition.list[condition.current]
|
|
174
|
+
)
|
|
175
|
+
result.subPackages.push(subPackage)
|
|
152
176
|
if (preLoadPath) {
|
|
153
|
-
if (!lodashEs.isArray(preLoadPath))
|
|
154
|
-
preLoadPath = [preLoadPath];
|
|
177
|
+
if (!lodashEs.isArray(preLoadPath)) preLoadPath = [preLoadPath]
|
|
155
178
|
const preLoadRule = preLoadPath.reduce((acc, cur) => {
|
|
156
179
|
acc[pathJoin(cur)] = {
|
|
157
180
|
network: preLoadNetwork,
|
|
158
181
|
packages: [_path]
|
|
159
|
-
}
|
|
160
|
-
return acc
|
|
161
|
-
}, {})
|
|
162
|
-
result.preloadRule = deepMerge(result.preloadRule, preLoadRule)
|
|
182
|
+
}
|
|
183
|
+
return acc
|
|
184
|
+
}, {})
|
|
185
|
+
result.preloadRule = deepMerge(result.preloadRule, preLoadRule)
|
|
163
186
|
}
|
|
164
|
-
continue
|
|
187
|
+
continue
|
|
165
188
|
}
|
|
166
189
|
if (children?.length) {
|
|
167
|
-
const {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
result.
|
|
178
|
-
result.
|
|
190
|
+
const {
|
|
191
|
+
pages,
|
|
192
|
+
subPackages: subPackages2,
|
|
193
|
+
preloadRule,
|
|
194
|
+
condition
|
|
195
|
+
} = buildRoute(children, {
|
|
196
|
+
parentName: style.navigationBarTitleText,
|
|
197
|
+
parentPath: _path,
|
|
198
|
+
subpackageRoot
|
|
199
|
+
})
|
|
200
|
+
result.pages = deepMerge(result.pages, pages)
|
|
201
|
+
result.subPackages = deepMerge(result.subPackages, subPackages2)
|
|
202
|
+
result.preloadRule = deepMerge(result.preloadRule, preloadRule)
|
|
203
|
+
result.condition = deepMerge(result.condition, condition)
|
|
179
204
|
if (condition.current > -1)
|
|
180
|
-
result.condition.current = result.condition?.list?.indexOf(
|
|
205
|
+
result.condition.current = result.condition?.list?.indexOf(
|
|
206
|
+
condition.list[condition.current]
|
|
207
|
+
)
|
|
181
208
|
} else {
|
|
182
|
-
const { conditionActive, conditionQuery } = route
|
|
209
|
+
const { conditionActive, conditionQuery } = route
|
|
183
210
|
const item = {
|
|
184
211
|
path: _path,
|
|
185
212
|
style
|
|
186
|
-
}
|
|
187
|
-
route.isIndex && (_indexPath = _path)
|
|
188
|
-
result.pages.push(item)
|
|
213
|
+
}
|
|
214
|
+
route.isIndex && (_indexPath = _path)
|
|
215
|
+
result.pages.push(item)
|
|
189
216
|
if (conditionQuery || conditionActive) {
|
|
190
217
|
const conditionItem = {
|
|
191
218
|
name: style.navigationBarTitleText,
|
|
192
219
|
path: _path,
|
|
193
220
|
query: conditionQuery
|
|
194
|
-
}
|
|
195
|
-
result.condition.list.push(conditionItem)
|
|
221
|
+
}
|
|
222
|
+
result.condition.list.push(conditionItem)
|
|
196
223
|
if (conditionActive)
|
|
197
|
-
result.condition.current =
|
|
224
|
+
result.condition.current =
|
|
225
|
+
result.condition.list?.indexOf(conditionItem)
|
|
198
226
|
}
|
|
199
227
|
}
|
|
200
228
|
}
|
|
201
|
-
return result
|
|
229
|
+
return result
|
|
202
230
|
}
|
|
203
|
-
var extension = [
|
|
231
|
+
var extension = ['.ts', '.js', '.mjs', '.json']
|
|
204
232
|
function loadDirFiles(roots, name, exitByFirst = false) {
|
|
205
|
-
if (!Array.isArray(roots))
|
|
206
|
-
|
|
207
|
-
const files = [];
|
|
233
|
+
if (!Array.isArray(roots)) roots = [roots]
|
|
234
|
+
const files = []
|
|
208
235
|
for (let root of roots) {
|
|
209
|
-
root = path__default.default.resolve(root)
|
|
210
|
-
if (!fs__default.default.existsSync(root))
|
|
211
|
-
|
|
212
|
-
const
|
|
213
|
-
const regex = new RegExp(`${name}(${extension.join("|")})$`);
|
|
236
|
+
root = path__default.default.resolve(root)
|
|
237
|
+
if (!fs__default.default.existsSync(root)) continue
|
|
238
|
+
const stat = fs__default.default.statSync(root)
|
|
239
|
+
const regex = new RegExp(`${name}(${extension.join('|')})$`)
|
|
214
240
|
if (stat.isFile() && regex.test(root)) {
|
|
215
|
-
files.push(root)
|
|
216
|
-
if (exitByFirst)
|
|
217
|
-
break;
|
|
241
|
+
files.push(root)
|
|
242
|
+
if (exitByFirst) break
|
|
218
243
|
}
|
|
219
244
|
if (stat.isDirectory()) {
|
|
220
|
-
const dirents = fs__default.default
|
|
221
|
-
|
|
245
|
+
const dirents = fs__default.default
|
|
246
|
+
.readdirSync(root)
|
|
247
|
+
.map((o) => path__default.default.resolve(root, o))
|
|
248
|
+
files.push(...loadDirFiles(dirents, name))
|
|
222
249
|
}
|
|
223
250
|
}
|
|
224
|
-
return files
|
|
251
|
+
return files
|
|
225
252
|
}
|
|
226
253
|
function pathJoin(...paths) {
|
|
227
|
-
return paths.join(
|
|
254
|
+
return paths.join('/').split('/').filter(Boolean).join('/')
|
|
228
255
|
}
|
|
229
256
|
function deepMerge(target, ...sources) {
|
|
230
|
-
if (typeof target !==
|
|
231
|
-
|
|
232
|
-
if (!sources.length)
|
|
233
|
-
return target;
|
|
257
|
+
if (typeof target !== 'object' || target === null) target = {}
|
|
258
|
+
if (!sources.length) return target
|
|
234
259
|
for (const source of sources) {
|
|
235
|
-
if (typeof source !==
|
|
236
|
-
continue;
|
|
260
|
+
if (typeof source !== 'object' || source === null) continue
|
|
237
261
|
if (lodashEs.isArray(target) && lodashEs.isArray(source)) {
|
|
238
|
-
const set = /* @__PURE__ */ new Set([...target, ...source])
|
|
239
|
-
return [...set]
|
|
240
|
-
} else if (
|
|
262
|
+
const set = /* @__PURE__ */ new Set([...target, ...source])
|
|
263
|
+
return [...set]
|
|
264
|
+
} else if (
|
|
265
|
+
lodashEs.isPlainObject(target) &&
|
|
266
|
+
lodashEs.isPlainObject(source)
|
|
267
|
+
) {
|
|
241
268
|
for (const key in source) {
|
|
242
|
-
if (
|
|
243
|
-
|
|
269
|
+
if (
|
|
270
|
+
key in target &&
|
|
271
|
+
typeof target[key] === 'object' &&
|
|
272
|
+
typeof source[key] === 'object'
|
|
273
|
+
) {
|
|
274
|
+
target[key] = deepMerge(target[key], source[key])
|
|
244
275
|
} else {
|
|
245
|
-
target[key] = source[key] || target[key]
|
|
276
|
+
target[key] = source[key] || target[key]
|
|
246
277
|
}
|
|
247
278
|
}
|
|
248
279
|
}
|
|
249
280
|
}
|
|
250
|
-
return target
|
|
281
|
+
return target
|
|
251
282
|
}
|
|
252
283
|
|
|
253
|
-
exports.builder = builder
|
|
254
|
-
exports.builderWithWatch = builderWithWatch
|
|
255
|
-
exports.defineMiniRouter = defineMiniRouter
|
|
256
|
-
exports.defineMiniTabbar = defineMiniTabbar
|
|
284
|
+
exports.builder = builder
|
|
285
|
+
exports.builderWithWatch = builderWithWatch
|
|
286
|
+
exports.defineMiniRouter = defineMiniRouter
|
|
287
|
+
exports.defineMiniTabbar = defineMiniTabbar
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mpilk/mp-route",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"lodash-es": "^4.17.21",
|
|
42
42
|
"pino": "^9.0.0",
|
|
43
43
|
"pino-pretty": "^11.0.0",
|
|
44
|
-
"@mpilk/mp-shared": "^
|
|
44
|
+
"@mpilk/mp-shared": "^2.0.0"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"@types/lodash-es": "^4.17.12"
|