-
-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathutils.ts
368 lines (327 loc) · 9.7 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import { TreeNode } from './tree'
import type { RouteRecordOverride, TreeRouteParam } from './treeNodeValue'
import { pascalCase } from 'scule'
import {
ResolvedOptions,
RoutesFolderOptionResolved,
_OverridableOption,
} from '../options'
export function warn(
msg: string,
type: 'warn' | 'error' | 'debug' = 'warn'
): void {
console[type](`⚠️ [unplugin-vue-router]: ${msg}`)
}
export function logTree(tree: TreeNode, log: (str: string) => any) {
log(printTree(tree))
}
const MAX_LEVEL = 1000
function printTree(
tree: TreeNode | TreeNode['children'],
level = 0,
parentPre = '',
treeStr = ''
): string {
// end of recursion
if (typeof tree !== 'object' || level >= MAX_LEVEL) return ''
if (tree instanceof Map) {
const total = tree.size
let index = 0
for (const [_key, child] of tree) {
const hasNext = index++ < total - 1
const { children } = child
treeStr += `${`${parentPre}${hasNext ? '├' : '└'}${
'─' + (children.size > 0 ? '┬' : '')
} `}${child}\n`
if (children) {
treeStr += printTree(
children,
level + 1,
`${parentPre}${hasNext ? '│' : ' '} `
)
}
}
} else {
const children = tree.children
treeStr = `${tree}\n`
if (children) {
treeStr += printTree(children, level + 1)
}
}
return treeStr
}
/**
* Type safe alternative to Array.isArray
* https://github.com/microsoft/TypeScript/pull/48228
*/
export const isArray: (arg: ArrayLike<any> | any) => arg is ReadonlyArray<any> =
Array.isArray
export function trimExtension(
path: string,
extensions: ResolvedOptions['extensions']
) {
for (const extension of extensions) {
const lastDot = path.endsWith(extension) ? -extension.length : 0
if (lastDot < 0) {
// usually only one extension should match
return path.slice(0, lastDot)
}
}
// no extension found, return the original path
return path
}
export function throttle(fn: () => void, wait: number, initialWait: number) {
let pendingExecutionTimeout: ReturnType<typeof setTimeout> | null = null
let pendingExecution = false
let executionTimeout: ReturnType<typeof setTimeout> | null = null
return () => {
if (pendingExecutionTimeout == null) {
pendingExecutionTimeout = setTimeout(() => {
pendingExecutionTimeout = null
if (pendingExecution) {
pendingExecution = false
fn()
}
}, wait)
executionTimeout = setTimeout(() => {
executionTimeout = null
fn()
}, initialWait)
} else if (executionTimeout == null) {
// we run the function recently, so we can skip it and add a pending execution
pendingExecution = true
}
}
}
const LEADING_SLASH_RE = /^\//
const TRAILING_SLASH_RE = /\/$/
export function joinPath(...paths: string[]): string {
let result = ''
for (const path of paths) {
result =
result.replace(TRAILING_SLASH_RE, '') +
// check path to avoid adding a trailing slash when joining an empty string
(path && '/' + path.replace(LEADING_SLASH_RE, ''))
}
return result
}
function paramToName({ paramName, modifier, isSplat }: TreeRouteParam) {
return `${isSplat ? '$' : ''}${
paramName.charAt(0).toUpperCase() + paramName.slice(1)
}${
modifier
// ? modifier === '+'
// ? 'OneOrMore'
// : modifier === '?'
// ? 'ZeroOrOne'
// : 'ZeroOrMore'
// : ''
}`
}
/**
* Creates a name based of the node path segments.
*
* @param node - the node to get the path from
* @param parent - the parent node
* @returns a route name
*/
export function getPascalCaseRouteName(node: TreeNode): string {
if (node.parent?.isRoot() && node.value.pathSegment === '') return 'Root'
let name = node.value.subSegments
.map((segment) => {
if (typeof segment === 'string') {
return pascalCase(segment)
}
// else it's a param
return paramToName(segment)
})
.join('')
if (node.value.components.size && node.children.has('index')) {
name += 'Parent'
}
const parent = node.parent
return (
(parent && !parent.isRoot()
? getPascalCaseRouteName(parent).replace(/Parent$/, '')
: '') + name
)
}
/**
* Joins the path segments of a node into a name that corresponds to the filepath represented by the node.
*
* @param node - the node to get the path from
* @returns a route name
*/
export function getFileBasedRouteName(node: TreeNode): string {
if (!node.parent) return ''
return (
getFileBasedRouteName(node.parent) +
'/' +
(node.value.rawSegment === 'index' ? '' : node.value.rawSegment)
)
}
export function mergeRouteRecordOverride(
a: RouteRecordOverride,
b: RouteRecordOverride
): RouteRecordOverride {
const merged: RouteRecordOverride = {}
const keys = [
...new Set<keyof RouteRecordOverride>([
...(Object.keys(a) as (keyof RouteRecordOverride)[]),
...(Object.keys(b) as (keyof RouteRecordOverride)[]),
]),
]
for (const key of keys) {
if (key === 'alias') {
const newAlias: string[] = []
merged[key] = newAlias.concat(a.alias || [], b.alias || [])
} else if (key === 'meta') {
merged[key] = mergeDeep(a[key] || {}, b[key] || {})
} else {
// @ts-expect-error: TS cannot see it's the same key
merged[key] = b[key] ?? a[key]
}
}
return merged
}
function isObject(obj: any): obj is Record<any, any> {
return obj && typeof obj === 'object'
}
function mergeDeep(...objects: Array<Record<any, any>>): Record<any, any> {
return objects.reduce((prev, obj) => {
Object.keys(obj).forEach((key) => {
const pVal = prev[key]
const oVal = obj[key]
if (Array.isArray(pVal) && Array.isArray(oVal)) {
prev[key] = pVal.concat(...oVal)
} else if (isObject(pVal) && isObject(oVal)) {
prev[key] = mergeDeep(pVal, oVal)
} else {
prev[key] = oVal
}
})
return prev
}, {})
}
/**
* Returns a route path to be used by the router with any defined prefix from an absolute path to a file. Since it
* returns a route path, it will remove the extension from the file.
*
* @param options - RoutesFolderOption to apply
* @param filePath - absolute path to file
* @returns a route path to be used by the router with any defined prefix
*/
export function asRoutePath(
{
src,
path = '',
extensions,
}: Pick<RoutesFolderOptionResolved, 'src' | 'path' | 'extensions'>,
filePath: string
) {
return trimExtension(
typeof path === 'string'
? // add the path prefix if any
path +
// remove the absolute path to the pages folder
filePath.slice(src.length + 1)
: path(filePath),
extensions
)
}
/**
* Builds a pattern from a file pattern and a list of extensions.
*
* @param filePattern - the file pattern to append the extensions to e.g. **/*
* @param extensions array of extensions to append to the pattern e.g. ['.vue', '.js']
* @returns
*/
export function appendExtensionListToPattern(
filePatterns: string,
extensions: string[]
): string
export function appendExtensionListToPattern(
filePatterns: string[],
extensions: string[]
): string[]
export function appendExtensionListToPattern(
filePatterns: string | string[],
extensions: string[]
): string[] | string {
const extensionPattern =
extensions.length === 1
? extensions[0]
: `.{${extensions
.map((extension) => extension.replace('.', ''))
.join(',')}}`
return Array.isArray(filePatterns)
? filePatterns.map((filePattern) => `${filePattern}${extensionPattern}`)
: `${filePatterns}${extensionPattern}`
}
export interface ImportEntry {
// name of the variable to import
name: string
// optional name to use when importing
as?: string
}
export class ImportsMap {
// path -> import as -> import name
// e.g map['vue-router']['myUseRouter'] = 'useRouter' -> import { useRouter as myUseRouter } from 'vue-router'
private map = new Map<string, Map<string, string>>()
constructor() {}
add(path: string, importEntry: ImportEntry): this
add(path: string, importEntry: string): this
add(path: string, importEntry: string | ImportEntry): this {
if (!this.map.has(path)) {
this.map.set(path, new Map())
}
const imports = this.map.get(path)!
if (typeof importEntry === 'string') {
imports.set(importEntry, importEntry)
} else {
imports.set(importEntry.as || importEntry.name, importEntry.name)
}
return this
}
addDefault(path: string, as: string): this {
return this.add(path, { name: 'default', as })
}
/**
* Get the list of imports for the given path.
*
* @param path - the path to get the import list for
* @returns the list of imports for the given path
*/
getImportList(path: string): Required<ImportEntry>[] {
if (!this.map.has(path)) return []
return Array.from(this.map.get(path)!).map(([as, name]) => ({
as: as || name,
name,
}))
}
toString(): string {
let importStatements = ''
for (const [path, imports] of this.map) {
if (!imports.size) continue
// only one import and it's the default one
if (imports.size === 1) {
// we extract the first and only entry
const [[importName, maybeDefault]] = [...imports.entries()] as [
[string, string],
]
// we only care if this is the default import
if (maybeDefault === 'default') {
importStatements += `import ${importName} from '${path}'\n`
continue
}
}
importStatements += `import { ${Array.from(imports)
.map(([as, name]) => (as === name ? name : `${name} as ${as}`))
.join(', ')} } from '${path}'\n`
}
return importStatements
}
get size(): number {
return this.map.size
}
}