/
utils.ts
324 lines (254 loc) · 7.76 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
import { resolve as _resolve, dirname, isAbsolute, normalize, posix, sep } from 'node:path'
import { existsSync, lstatSync, readdirSync, rmdirSync } from 'node:fs'
import typescript from 'typescript'
import type { CompilerOptions } from 'typescript'
const windowsSlashRE = /\\+/g
export function slash(p: string): string {
return p.replace(windowsSlashRE, '/')
}
export function normalizePath(id: string): string {
return posix.normalize(slash(id))
}
export function resolve(...paths: string[]) {
return normalizePath(_resolve(...paths))
}
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 === 'function' || typeof value === 'object') &&
typeof (value as any).then === 'function'
)
}
export async function wrapPromise<T>(maybePromise: T | Promise<T>) {
return isPromise(maybePromise) ? await maybePromise : maybePromise
}
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 normalizePath(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)
}
const speRE = /[\\/]/
export function queryPublicPath(paths: string[]) {
if (paths.length === 0) {
return ''
} else if (paths.length === 1) {
return dirname(paths[0])
}
let publicPath = normalize(dirname(paths[0])) + sep
let publicUnits = publicPath.split(speRE)
let index = publicUnits.length - 1
for (const path of paths.slice(1)) {
if (!index) {
return publicPath
}
const dirPath = normalize(dirname(path)) + sep
if (dirPath.startsWith(publicPath)) {
continue
}
const units = dirPath.split(speRE)
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(sep) + sep
break
}
}
}
return publicPath.slice(0, -1)
}
export function removeDirIfEmpty(dir: string) {
if (!existsSync(dir)) {
return
}
let onlyHasDir = true
for (const file of readdirSync(dir)) {
const abs = resolve(dir, file)
if (lstatSync(abs).isDirectory()) {
if (!removeDirIfEmpty(abs)) {
onlyHasDir = false
}
} else {
onlyHasDir = false
}
}
if (onlyHasDir) {
rmdirSync(dir)
}
return onlyHasDir
}
export function getTsConfig(
tsConfigPath: string,
readFileSync: (filePath: string, encoding?: string | undefined) => string | undefined
) {
// #95 Should parse include or exclude from the base config when they are missing from
// the inheriting config. If the inherit config doesn't have `include` or `exclude` field,
// should get them from the parent config.
const tsConfig: {
compilerOptions: CompilerOptions,
include?: string[],
exclude?: string[],
extends?: string | string[]
} = {
compilerOptions: {},
...(typescript.readConfigFile(tsConfigPath, readFileSync).config ?? {})
}
if (tsConfig.extends) {
ensureArray(tsConfig.extends).forEach((configPath: string) => {
const config = getTsConfig(ensureAbsolute(configPath, dirname(tsConfigPath)), readFileSync)
// #171 Need to collect the full `compilerOptions` for `@microsoft/api-extractor`
Object.assign(tsConfig.compilerOptions, config.compilerOptions)
if (!tsConfig.include) {
tsConfig.include = config.include
}
if (!tsConfig.exclude) {
tsConfig.exclude = config.exclude
}
})
}
return tsConfig
}
/**
* @see https://github.com/mozilla/source-map/blob/master/lib/base64-vlq.js
*/
const BASE64_ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split('')
function base64Encode(number: number) {
if (number >= 0 && number < BASE64_ALPHABET.length) {
return BASE64_ALPHABET[number]
}
throw new TypeError('Base64 integer must be between 0 and 63: ' + number)
}
const VLQ_BASE_SHIFT = 5
const VLQ_BASE = 1 << VLQ_BASE_SHIFT
const VLQ_BASE_MASK = VLQ_BASE - 1
const VLQ_CONTINUATION_BIT = VLQ_BASE
function toVLQSigned(number: number) {
return number < 0 ? (-number << 1) + 1 : (number << 1) + 0
}
export function base64VLQEncode(numbers: number[]) {
let encoded = ''
for (const number of numbers) {
let vlq = toVLQSigned(number)
let digit: number
do {
digit = vlq & VLQ_BASE_MASK
vlq >>>= VLQ_BASE_SHIFT
if (vlq > 0) {
// There are still more digits in this value, so we must make sure the
// continuation bit is marked.
digit |= VLQ_CONTINUATION_BIT
}
encoded += base64Encode(digit)
} while (vlq > 0)
}
return encoded
}
const pkgPathCache = new Map<string, string | undefined>()
export function tryGetPkgPath(beginPath: string) {
beginPath = normalizePath(beginPath)
if (pkgPathCache.has(beginPath)) {
return pkgPathCache.get(beginPath)
}
const pkgPath = resolve(beginPath, 'package.json')
if (existsSync(pkgPath)) {
pkgPathCache.set(beginPath, pkgPath)
return pkgPath
}
const parentDir = normalizePath(dirname(beginPath))
if (!parentDir || parentDir === beginPath) {
pkgPathCache.set(beginPath, undefined)
return
}
return tryGetPkgPath(parentDir)
}
type CapitalCase<T extends string> = T extends `${infer First} ${infer Rest}`
? CapitalCase<`${First}-${Rest}`>
: T extends `${infer First}-${infer Rest}`
? `${Capitalize<First>}${CapitalCase<Rest>}`
: Capitalize<T>
export function toCapitalCase<T extends string>(value: T) {
value = value.trim().replace(/\s+/g, '-') as T
value = value.replace(/-+(\w)/g, (_, char) => (char ? char.toUpperCase() : '')) as T
return (value.charAt(0).toLocaleUpperCase() + value.slice(1)).replace(
/[^\w]/g,
''
) as CapitalCase<T>
}