-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetadata-extensions.ts
More file actions
45 lines (42 loc) · 1.26 KB
/
Copy pathmetadata-extensions.ts
File metadata and controls
45 lines (42 loc) · 1.26 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
/**
* @file Package-extension lookup: match a package name + version against the
* `packageExtensions` overrides table (the same data pnpm/yarn use to patch
* missing dependency metadata) and merge the matching entries.
*/
import { getPackageExtensions } from '../constants/packages'
import { satisfies } from '../external/semver'
import { merge } from '../objects/mutate'
const packageExtensions = getPackageExtensions()
/**
* Find package extensions for a given package.
*
* @example
* ;```typescript
* const extensions = findPackageExtensions('my-pkg', '1.0.0')
* ```
*/
export function findPackageExtensions(
pkgName: string,
pkgVer: string,
): unknown {
let result: unknown
for (const entry of packageExtensions) {
const selector = String(entry[0])
const ext = entry[1]
const lastAtSignIndex = selector.lastIndexOf('@')
const name = selector.slice(0, lastAtSignIndex)
if (pkgName === name) {
// semver is imported at the top
const range = selector.slice(lastAtSignIndex + 1)
if (satisfies(pkgVer, range)) {
if (result === undefined) {
result = {}
}
if (typeof ext === 'object' && ext !== null) {
merge(result as object, ext)
}
}
}
}
return result
}