/
parsePref.ts
51 lines (49 loc) · 1.12 KB
/
parsePref.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
import parseNpmTarballUrl from 'parse-npm-tarball-url'
import getVersionSelectorType from 'version-selector-type'
export interface RegistryPackageSpec {
type: 'tag' | 'version' | 'range'
name: string
fetchSpec: string
normalizedPref?: string
}
export default function parsePref (
pref: string,
alias: string | undefined,
defaultTag: string,
registry: string
): RegistryPackageSpec | null {
let name = alias
if (pref.startsWith('npm:')) {
pref = pref.slice(4)
const index = pref.lastIndexOf('@')
if (index < 1) {
name = pref
pref = defaultTag
} else {
name = pref.slice(0, index)
pref = pref.slice(index + 1)
}
}
if (name) {
const selector = getVersionSelectorType(pref)
if (selector != null) {
return {
fetchSpec: selector.normalized,
name,
type: selector.type,
}
}
}
if (pref.startsWith(registry)) {
const pkg = parseNpmTarballUrl(pref)
if (pkg != null) {
return {
fetchSpec: pkg.version,
name: pkg.name,
normalizedPref: pref,
type: 'version',
}
}
}
return null
}