-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
inlineSpecifiersLockfileConverters.ts
114 lines (100 loc) · 4.08 KB
/
inlineSpecifiersLockfileConverters.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
import type { Lockfile, ProjectSnapshot, ResolvedDependencies } from '@pnpm/lockfile-types'
import {
INLINE_SPECIFIERS_FORMAT_LOCKFILE_VERSION_SUFFIX,
InlineSpecifiersLockfile,
InlineSpecifiersProjectSnapshot,
InlineSpecifiersResolvedDependencies,
} from './InlineSpecifiersLockfile'
export function isExperimentalInlineSpecifiersFormat (
lockfile: InlineSpecifiersLockfile | Lockfile
): lockfile is InlineSpecifiersLockfile {
const { lockfileVersion } = lockfile
return typeof lockfileVersion === 'string' && lockfileVersion.endsWith(INLINE_SPECIFIERS_FORMAT_LOCKFILE_VERSION_SUFFIX)
}
export function convertToInlineSpecifiersFormat (lockfile: Lockfile): InlineSpecifiersLockfile {
return {
...lockfile,
lockfileVersion: `${lockfile.lockfileVersion}${INLINE_SPECIFIERS_FORMAT_LOCKFILE_VERSION_SUFFIX}`,
importers: mapValues(lockfile.importers, convertProjectSnapshotToInlineSpecifiersFormat),
}
}
export function revertFromInlineSpecifiersFormatIfNecessary (lockfile: Lockfile | InlineSpecifiersLockfile): Lockfile {
return isExperimentalInlineSpecifiersFormat(lockfile)
? revertFromInlineSpecifiersFormat(lockfile)
: lockfile
}
export function revertFromInlineSpecifiersFormat (lockfile: InlineSpecifiersLockfile): Lockfile {
const { lockfileVersion, importers, ...rest } = lockfile
const originalVersionStr = lockfileVersion.replace(INLINE_SPECIFIERS_FORMAT_LOCKFILE_VERSION_SUFFIX, '')
const originalVersion = Number(originalVersionStr)
if (isNaN(originalVersion)) {
throw new Error(`Unable to revert lockfile from inline specifiers format. Invalid version parsed: ${originalVersionStr}`)
}
return {
...rest,
lockfileVersion: originalVersion,
importers: mapValues(importers, revertProjectSnapshot),
}
}
function convertProjectSnapshotToInlineSpecifiersFormat (
projectSnapshot: ProjectSnapshot
): InlineSpecifiersProjectSnapshot {
const { specifiers, ...rest } = projectSnapshot
const convertBlock = (block?: ResolvedDependencies) =>
block != null
? convertResolvedDependenciesToInlineSpecifiersFormat(block, { specifiers })
: block
return {
...rest,
dependencies: convertBlock(projectSnapshot.dependencies),
optionalDependencies: convertBlock(projectSnapshot.optionalDependencies),
devDependencies: convertBlock(projectSnapshot.devDependencies),
}
}
function convertResolvedDependenciesToInlineSpecifiersFormat (
resolvedDependencies: ResolvedDependencies,
{ specifiers }: { specifiers: ResolvedDependencies}
): InlineSpecifiersResolvedDependencies {
return mapValues(resolvedDependencies, (version, depName) => ({
specifier: specifiers[depName],
version,
}))
}
function revertProjectSnapshot (from: InlineSpecifiersProjectSnapshot): ProjectSnapshot {
const specifiers: ResolvedDependencies = {}
function moveSpecifiers (from: InlineSpecifiersResolvedDependencies): ResolvedDependencies {
const resolvedDependencies: ResolvedDependencies = {}
for (const [depName, { specifier, version }] of Object.entries(from)) {
const existingValue = specifiers[depName]
if (existingValue != null && existingValue !== specifier) {
throw new Error(`Project snapshot lists the same dependency more than once with conflicting versions: ${depName}`)
}
specifiers[depName] = specifier
resolvedDependencies[depName] = version
}
return resolvedDependencies
}
const dependencies = from.dependencies == null
? from.dependencies
: moveSpecifiers(from.dependencies)
const devDependencies = from.devDependencies == null
? from.devDependencies
: moveSpecifiers(from.devDependencies)
const optionalDependencies = from.optionalDependencies == null
? from.optionalDependencies
: moveSpecifiers(from.optionalDependencies)
return {
...from,
specifiers,
dependencies,
devDependencies,
optionalDependencies,
}
}
function mapValues<T, U> (obj: Record<string, T>, mapper: (val: T, key: string) => U): Record<string, U> {
const result = {}
for (const [key, value] of Object.entries(obj)) {
result[key] = mapper(value, key)
}
return result
}