-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathutils.ts
More file actions
98 lines (81 loc) · 2.63 KB
/
utils.ts
File metadata and controls
98 lines (81 loc) · 2.63 KB
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
import type { Component } from 'vue'
import { createResolver } from '@nuxt/kit'
import type { NuxtConfigLayer } from '@nuxt/schema'
import { allIcons, libraryName } from './config'
import type { PresetComponent } from './types'
export function resolvePath (path: string): Promise<string> {
const { resolvePath } = createResolver(import.meta.url)
return resolvePath(path)
}
export async function resolveComponentPath (
path: string,
cache: boolean | undefined
): Promise<string> {
if (cache) {
return `#build/${libraryName}-cache.mjs`
}
return await resolvePath(`${libraryName}/${path}`)
}
export function getLayersDir (layers: readonly NuxtConfigLayer[]) {
const list = []
for (const layer of layers) {
const srcDir = layer.config.srcDir || layer.cwd
if (
srcDir.includes('node_modules') &&
isObject(layer.config.elementPlus) &&
layer.config.elementPlus?.importStyle !== false
) {
list.push(srcDir)
}
}
return list
}
export function isObject (value: any): value is Record<string, any> {
return typeof value === 'object' && value !== null && !isArray(value)
}
export function isFunction (value: any): value is Function {
return typeof value === 'function'
}
export function isArray (value: any): value is any[] {
return Array.isArray(value)
}
export function isVueComponent (value: any): value is Component {
return (
typeof value === 'object' &&
value.name &&
(value.props || value.emits || value.setup || value.render)
)
}
export function toArray<T extends any | any[]> (
value: T
): T extends any[] ? T : T[] {
return (isArray(value) ? value : [value]) as any
}
export function toRegExp (arr: string[], flags?: string): RegExp {
return new RegExp(`\\b(${arr.join('|')})\\b`, flags)
}
export async function genLibraryImport (
[name, as, from]: Required<Exclude<PresetComponent, string>>,
cache: boolean | undefined
): Promise<string> {
const fromPath = await resolveComponentPath(from, cache)
return `import { ${name} as ${as} } from '${fromPath}';\n`
}
export async function genSideEffectsImport (from: string): Promise<string> {
const fromPath = await resolvePath(from)
return `import '${fromPath}';\n`
}
export function genIconPresets (
prefix: string,
from?: string
): Exclude<PresetComponent, string>[] {
return allIcons.map((name) => {
return [name, `${prefix}${name}`, from] as Exclude<PresetComponent, string>
})
}
export function camelize (value: string): string {
return value.replace(/(^|-)(\w)/g, (a, b, c) => c.toUpperCase())
}
export function hyphenate (value: string): string {
return value.replace(/\B([A-Z])/g, '-$1').toLowerCase()
}