/
read.ts
245 lines (232 loc) · 7.27 KB
/
read.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
import { promises as fs } from 'fs'
import path from 'path'
import {
LOCKFILE_VERSION,
WANTED_LOCKFILE,
} from '@pnpm/constants'
import PnpmError from '@pnpm/error'
import mergeLockfileChanges from '@pnpm/merge-lockfile-changes'
import { Lockfile } from '@pnpm/lockfile-types'
import { DEPENDENCIES_FIELDS } from '@pnpm/types'
import comverToSemver from 'comver-to-semver'
import yaml from 'js-yaml'
import semver from 'semver'
import stripBom from 'strip-bom'
import { LockfileBreakingChangeError } from './errors'
import { autofixMergeConflicts, isDiff } from './gitMergeFile'
import logger from './logger'
import { LockfileFile } from './write'
import { getWantedLockfileName } from './lockfileName'
import { getGitBranchLockfileNames } from './gitBranchLockfile'
import { revertFromInlineSpecifiersFormatIfNecessary } from './experiments/inlineSpecifiersLockfileConverters'
export async function readCurrentLockfile (
virtualStoreDir: string,
opts: {
wantedVersion?: number
ignoreIncompatible: boolean
}
): Promise<Lockfile | null> {
const lockfilePath = path.join(virtualStoreDir, 'lock.yaml')
return (await _read(lockfilePath, virtualStoreDir, opts)).lockfile
}
export async function readWantedLockfileAndAutofixConflicts (
pkgPath: string,
opts: {
wantedVersion?: number
ignoreIncompatible: boolean
useGitBranchLockfile?: boolean
mergeGitBranchLockfiles?: boolean
}
): Promise<{
lockfile: Lockfile | null
hadConflicts: boolean
}> {
return _readWantedLockfile(pkgPath, {
...opts,
autofixMergeConflicts: true,
})
}
export async function readWantedLockfile (
pkgPath: string,
opts: {
wantedVersion?: number
ignoreIncompatible: boolean
useGitBranchLockfile?: boolean
mergeGitBranchLockfiles?: boolean
}
): Promise<Lockfile | null> {
return (await _readWantedLockfile(pkgPath, opts)).lockfile
}
async function _read (
lockfilePath: string,
prefix: string, // only for logging
opts: {
autofixMergeConflicts?: boolean
wantedVersion?: number
ignoreIncompatible: boolean
}
): Promise<{
lockfile: Lockfile | null
hadConflicts: boolean
}> {
let lockfileRawContent
try {
lockfileRawContent = stripBom(await fs.readFile(lockfilePath, 'utf8'))
} catch (err: any) { // eslint-disable-line
if ((err as NodeJS.ErrnoException).code !== 'ENOENT') {
throw err
}
return {
lockfile: null,
hadConflicts: false,
}
}
let lockfileFile: LockfileFile
let hadConflicts!: boolean
try {
lockfileFile = yaml.load(lockfileRawContent) as Lockfile
hadConflicts = false
} catch (err: any) { // eslint-disable-line
if (!opts.autofixMergeConflicts || !isDiff(lockfileRawContent)) {
throw new PnpmError('BROKEN_LOCKFILE', `The lockfile at "${lockfilePath}" is broken: ${err.message as string}`)
}
hadConflicts = true
lockfileFile = autofixMergeConflicts(lockfileRawContent)
logger.info({
message: `Merge conflict detected in ${WANTED_LOCKFILE} and successfully merged`,
prefix,
})
}
if (lockfileFile) {
const lockfile = revertFromInlineSpecifiersFormatIfNecessary(convertFromLockfileFileMutable(lockfileFile))
const lockfileSemver = comverToSemver((lockfile.lockfileVersion ?? 0).toString())
/* eslint-enable @typescript-eslint/dot-notation */
if (typeof opts.wantedVersion !== 'number' || semver.major(lockfileSemver) === semver.major(comverToSemver(opts.wantedVersion.toString()))) {
if (typeof opts.wantedVersion === 'number' && semver.gt(lockfileSemver, comverToSemver(opts.wantedVersion.toString()))) {
logger.warn({
message: `Your ${WANTED_LOCKFILE} was generated by a newer version of pnpm. ` +
`It is a compatible version but it might get downgraded to version ${opts.wantedVersion}`,
prefix,
})
}
return { lockfile, hadConflicts }
}
}
if (opts.ignoreIncompatible) {
logger.warn({
message: `Ignoring not compatible lockfile at ${lockfilePath}`,
prefix,
})
return { lockfile: null, hadConflicts: false }
}
throw new LockfileBreakingChangeError(lockfilePath)
}
export function createLockfileObject (
importerIds: string[],
opts: {
lockfileVersion: number
}
) {
const importers = importerIds.reduce((acc, importerId) => {
acc[importerId] = {
dependencies: {},
specifiers: {},
}
return acc
}, {})
return {
importers,
lockfileVersion: opts.lockfileVersion || LOCKFILE_VERSION,
}
}
async function _readWantedLockfile (
pkgPath: string,
opts: {
wantedVersion?: number
ignoreIncompatible: boolean
useGitBranchLockfile?: boolean
mergeGitBranchLockfiles?: boolean
autofixMergeConflicts?: boolean
}
): Promise<{
lockfile: Lockfile | null
hadConflicts: boolean
}> {
const lockfileNames: string[] = [WANTED_LOCKFILE]
if (opts.useGitBranchLockfile) {
const gitBranchLockfileName: string = await getWantedLockfileName(opts)
if (gitBranchLockfileName !== WANTED_LOCKFILE) {
lockfileNames.unshift(gitBranchLockfileName)
}
}
let result: { lockfile: Lockfile | null, hadConflicts: boolean } = { lockfile: null, hadConflicts: false }
for (const lockfileName of lockfileNames) {
result = await _read(path.join(pkgPath, lockfileName), pkgPath, { ...opts, autofixMergeConflicts: true })
if (result.lockfile) {
if (opts.mergeGitBranchLockfiles) {
result.lockfile = await _mergeGitBranchLockfiles(result.lockfile, pkgPath, pkgPath, opts)
}
break
}
}
return result
}
async function _mergeGitBranchLockfiles (
lockfile: Lockfile | null,
lockfileDir: string,
prefix: string,
opts: {
autofixMergeConflicts?: boolean
wantedVersion?: number
ignoreIncompatible: boolean
}
): Promise<Lockfile | null> {
if (!lockfile) {
return lockfile
}
const gitBranchLockfiles: Array<(Lockfile | null)> = (await _readGitBranchLockfiles(lockfileDir, prefix, opts)).map(({ lockfile }) => lockfile)
let mergedLockfile: Lockfile = lockfile
for (const gitBranchLockfile of gitBranchLockfiles) {
if (!gitBranchLockfile) {
continue
}
mergedLockfile = mergeLockfileChanges(mergedLockfile, gitBranchLockfile)
}
return mergedLockfile
}
async function _readGitBranchLockfiles (
lockfileDir: string,
prefix: string,
opts: {
autofixMergeConflicts?: boolean
wantedVersion?: number
ignoreIncompatible: boolean
}
): Promise<Array<{
lockfile: Lockfile | null
hadConflicts: boolean
}>> {
const files = await getGitBranchLockfileNames(lockfileDir)
return Promise.all(files.map((file) => _read(path.join(lockfileDir, file), prefix, opts)))
}
/**
* Reverts changes from the "forceSharedFormat" write option if necessary.
*/
function convertFromLockfileFileMutable (lockfileFile: LockfileFile): Lockfile {
if (typeof lockfileFile?.['importers'] === 'undefined') {
lockfileFile.importers = {
'.': {
specifiers: lockfileFile['specifiers'] ?? {},
dependenciesMeta: lockfileFile['dependenciesMeta'],
},
}
delete lockfileFile.specifiers
for (const depType of DEPENDENCIES_FIELDS) {
if (lockfileFile[depType] != null) {
lockfileFile.importers['.'][depType] = lockfileFile[depType]
delete lockfileFile[depType]
}
}
}
return lockfileFile as Lockfile
}