-
-
Notifications
You must be signed in to change notification settings - Fork 369
/
Copy pathutils.ts
150 lines (123 loc) · 3.88 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
import { parse } from 'path'
import minimatch from 'minimatch'
import { ResolvedConfig } from 'vite'
import { slash, toArray } from '@antfu/utils'
import { ComponentInfo, ResolvedOptions, ImportInfo } from '../types'
import { Context } from './context'
export interface ResolveComponent {
filename: string
namespace?: string
}
export function pascalCase(str: string) {
return capitalize(camelCase(str))
}
export function camelCase(str: string) {
return str.replace(/-(\w)/g, (_, c) => (c ? c.toUpperCase() : ''))
}
export function kebabCase(key: string) {
const result = key.replace(/([A-Z])/g, ' $1').trim()
return result.split(' ').join('-').toLowerCase()
}
export function capitalize(str: string) {
return str.charAt(0).toUpperCase() + str.slice(1)
}
export function parseId(id: string) {
const index = id.indexOf('?')
if (index < 0) {
return { path: id, query: {} }
}
else {
// @ts-ignore
const query = Object.fromEntries(new URLSearchParams(id.slice(index)))
return {
path: id.slice(0, index),
query,
}
}
}
export function isEmpty(value: any) {
if (!value || value === null || value === undefined || (Array.isArray(value) && Object.keys(value).length <= 0))
return true
else
return false
}
export function matchGlobs(filepath: string, globs: string[]) {
for (const glob of globs) {
if (minimatch(slash(filepath), glob))
return true
}
return false
}
export function stringifyImport(info: ImportInfo | string) {
if (typeof info === 'string')
return `import '${info}'`
if (!info.name)
return `import '${info.path}'`
else if (info.importName)
return `import { ${info.importName} as ${info.name} } from '${info.path}'`
else
return `import ${info.name} from '${info.path}'`
}
export function stringifyComponentImport({ name, path, importName, sideEffects }: ComponentInfo, ctx: Context) {
if (ctx.options.importPathTransform) {
const result = ctx.options.importPathTransform(path)
if (result != null)
path = result
}
const imports = [
stringifyImport({ name, path, importName }),
]
if (sideEffects)
toArray(sideEffects).forEach(i => imports.push(stringifyImport(i)))
return imports.join(';')
}
export function getNameFromFilePath(filePath: string, options: ResolvedOptions): string {
const { resolvedDirs, directoryAsNamespace, globalNamespaces } = options
const parsedFilePath = parse(slash(filePath))
let strippedPath = ''
// remove include directories from filepath
for (const dir of resolvedDirs) {
if (parsedFilePath.dir.startsWith(dir)) {
strippedPath = parsedFilePath.dir.slice(dir.length)
break
}
}
let folders = strippedPath.slice(1).split('/').filter(Boolean)
let filename = parsedFilePath.name
// set parent directory as filename if it is index
if (filename === 'index' && !directoryAsNamespace) {
filename = `${folders.slice(-1)[0]}`
return filename
}
if (directoryAsNamespace) {
// remove namesspaces from folder names
if (globalNamespaces.some((name: string) => folders.includes(name)))
folders = folders.filter(f => !globalNamespaces.includes(f))
if (filename.toLowerCase() === 'index')
filename = ''
if (!isEmpty(folders)) {
// add folders to filename
filename = [...folders, filename].filter(Boolean).join('-')
}
return filename
}
return filename
}
export function resolveAlias(filepath: string, alias: ResolvedConfig['resolve']['alias'] = []) {
const result = filepath
if (Array.isArray(alias)) {
for (const { find, replacement } of alias)
result.replace(find, replacement)
}
return result
}
export function getPkgVersion(pkgName: string, defaultVersion: string): string {
try {
/* eslint-disable @typescript-eslint/no-var-requires */
return require(`${pkgName}/package.json`).version
}
catch (err) {
console.error(err)
return defaultVersion
}
}