/
utils.ts
148 lines (120 loc) · 3.3 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
import { resolve, isAbsolute, dirname } from 'path'
export function isNativeObj<T extends Record<string, any> = Record<string, any>>(
value: T
): value is T {
return Object.prototype.toString.call(value) === '[object Object]'
}
export function isRegExp(value: unknown): value is RegExp {
return Object.prototype.toString.call(value) === '[object RegExp]'
}
export function isPromise(value: unknown): value is Promise<any> {
return (
!!value &&
typeof (value as any).then === 'function' &&
typeof (value as any).catch === 'function'
)
}
export function mergeObjects<T extends Record<string, unknown>, U extends Record<string, unknown>>(
sourceObj: T,
targetObj: U
) {
const loop: Array<{
source: Record<string, any>,
target: Record<string, any>
// merged: Record<string, any>
}> = [
{
source: sourceObj,
target: targetObj
// merged: mergedObj
}
]
while (loop.length) {
const { source, target } = loop.pop()!
Object.keys(target).forEach(key => {
if (isNativeObj(target[key])) {
if (!isNativeObj(source[key])) {
source[key] = {}
}
loop.push({
source: source[key],
target: target[key]
})
} else if (Array.isArray(target[key])) {
if (!Array.isArray(source[key])) {
source[key] = []
}
loop.push({
source: source[key],
target: target[key]
})
} else {
source[key] = target[key]
}
})
}
return sourceObj as T & U
}
export function ensureAbsolute(path: string, root: string) {
return path ? (isAbsolute(path) ? path : resolve(root, path)) : root
}
export function ensureArray<T>(value: T | T[]) {
return Array.isArray(value) ? value : value ? [value] : []
}
export async function runParallel<T>(
maxConcurrency: number,
source: T[],
iteratorFn: (item: T, source: T[]) => Promise<any>
) {
const ret: Promise<any>[] = []
const executing: Promise<any>[] = []
for (const item of source) {
const p = Promise.resolve().then(() => iteratorFn(item, source))
ret.push(p)
if (maxConcurrency <= source.length) {
const e: Promise<any> = p.then(() => executing.splice(executing.indexOf(e), 1))
executing.push(e)
if (executing.length >= maxConcurrency) {
await Promise.race(executing)
}
}
}
return Promise.all(ret)
}
export function queryPublicPath(paths: string[]) {
if (paths.length === 0) {
return ''
} else if (paths.length === 1) {
return dirname(paths[0])
}
let publicPath = dirname(paths[0]) + '\\'
let publicUnits = publicPath.split(/[\\/]/)
let index = publicUnits.length - 1
for (const path of paths.slice(1)) {
if (!index) {
return publicPath
}
const dirPath = dirname(path) + '\\'
if (dirPath.startsWith(publicPath)) {
continue
}
const units = dirPath.split(/[\\/]/)
if (units.length < index) {
publicPath = dirPath
publicUnits = units
continue
}
for (let i = 0; i <= index; ++i) {
if (publicUnits[i] !== units[i]) {
if (!i) {
return ''
}
index = i - 1
publicUnits = publicUnits.slice(0, index + 1)
publicPath = publicUnits.join('\\') + '\\'
break
}
}
}
return publicPath.slice(0, -1)
}