/
write.ts
213 lines (192 loc) · 7.21 KB
/
write.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
import { promises as fs } from 'fs'
import path from 'path'
import { DEPENDENCIES_FIELDS } from '@pnpm/types'
import { Lockfile, ProjectSnapshot } from '@pnpm/lockfile-types'
import { WANTED_LOCKFILE } from '@pnpm/constants'
import rimraf from '@zkochan/rimraf'
import yaml from 'js-yaml'
import equals from 'ramda/src/equals.js'
import isEmpty from 'ramda/src/isEmpty.js'
import writeFileAtomicCB from 'write-file-atomic'
import logger from './logger'
import { sortLockfileKeys } from './sortLockfileKeys'
import { getWantedLockfileName } from './lockfileName'
import { convertToInlineSpecifiersFormat } from './experiments/inlineSpecifiersLockfileConverters'
async function writeFileAtomic (filename: string, data: string) {
return new Promise<void>((resolve, reject) => writeFileAtomicCB(filename, data, {}, (err?: Error) => (err != null) ? reject(err) : resolve()))
}
const LOCKFILE_YAML_FORMAT = {
blankLines: true,
lineWidth: 1000,
noCompatMode: true,
noRefs: true,
sortKeys: false,
}
export async function writeWantedLockfile (
pkgPath: string,
wantedLockfile: Lockfile,
opts?: {
forceSharedFormat?: boolean
useInlineSpecifiersFormat?: boolean
useGitBranchLockfile?: boolean
mergeGitBranchLockfiles?: boolean
}
) {
const wantedLockfileName: string = await getWantedLockfileName(opts)
return writeLockfile(wantedLockfileName, pkgPath, wantedLockfile, opts)
}
export async function writeCurrentLockfile (
virtualStoreDir: string,
currentLockfile: Lockfile,
opts?: {
forceSharedFormat?: boolean
}
) {
await fs.mkdir(virtualStoreDir, { recursive: true })
return writeLockfile('lock.yaml', virtualStoreDir, currentLockfile, opts)
}
interface LockfileFormatOptions {
forceSharedFormat?: boolean
useInlineSpecifiersFormat?: boolean
}
async function writeLockfile (
lockfileFilename: string,
pkgPath: string,
wantedLockfile: Lockfile,
opts?: LockfileFormatOptions
) {
const lockfilePath = path.join(pkgPath, lockfileFilename)
// empty lockfile is not saved
if (isEmptyLockfile(wantedLockfile)) {
return rimraf(lockfilePath)
}
const lockfileToStringify = (opts?.useInlineSpecifiersFormat ?? false)
? convertToInlineSpecifiersFormat(wantedLockfile) as unknown as Lockfile
: wantedLockfile
const yamlDoc = yamlStringify(lockfileToStringify, opts?.forceSharedFormat === true)
return writeFileAtomic(lockfilePath, yamlDoc)
}
function yamlStringify (lockfile: Lockfile, forceSharedFormat: boolean) {
let normalizedLockfile = normalizeLockfile(lockfile, forceSharedFormat)
normalizedLockfile = sortLockfileKeys(normalizedLockfile)
return yaml.dump(normalizedLockfile, LOCKFILE_YAML_FORMAT)
}
function isEmptyLockfile (lockfile: Lockfile) {
return Object.values(lockfile.importers).every((importer) => isEmpty(importer.specifiers ?? {}) && isEmpty(importer.dependencies ?? {}))
}
export type LockfileFile = Omit<Lockfile, 'importers'> & Partial<ProjectSnapshot> & Partial<Pick<Lockfile, 'importers'>>
export function normalizeLockfile (lockfile: Lockfile, forceSharedFormat: boolean) {
let lockfileToSave!: LockfileFile
if (!forceSharedFormat && equals(Object.keys(lockfile.importers), ['.'])) {
lockfileToSave = {
...lockfile,
...lockfile.importers['.'],
}
delete lockfileToSave.importers
for (const depType of DEPENDENCIES_FIELDS) {
if (isEmpty(lockfileToSave[depType])) {
delete lockfileToSave[depType]
}
}
if (isEmpty(lockfileToSave.packages) || (lockfileToSave.packages == null)) {
delete lockfileToSave.packages
}
} else {
lockfileToSave = {
...lockfile,
importers: Object.keys(lockfile.importers).reduce((acc, alias) => {
const importer = lockfile.importers[alias]
const normalizedImporter = {
specifiers: importer.specifiers ?? {},
}
if (importer.dependenciesMeta != null && !isEmpty(importer.dependenciesMeta)) {
normalizedImporter['dependenciesMeta'] = importer.dependenciesMeta
}
for (const depType of DEPENDENCIES_FIELDS) {
if (!isEmpty(importer[depType] ?? {})) {
normalizedImporter[depType] = importer[depType]
}
}
acc[alias] = normalizedImporter
return acc
}, {}),
}
if (isEmpty(lockfileToSave.packages) || (lockfileToSave.packages == null)) {
delete lockfileToSave.packages
}
}
if ((lockfileToSave.overrides != null) && isEmpty(lockfileToSave.overrides)) {
delete lockfileToSave.overrides
}
if ((lockfileToSave.patchedDependencies != null) && isEmpty(lockfileToSave.patchedDependencies)) {
delete lockfileToSave.patchedDependencies
}
if (lockfileToSave.neverBuiltDependencies != null) {
if (isEmpty(lockfileToSave.neverBuiltDependencies)) {
delete lockfileToSave.neverBuiltDependencies
} else {
lockfileToSave.neverBuiltDependencies = lockfileToSave.neverBuiltDependencies.sort()
}
}
if (lockfileToSave.onlyBuiltDependencies != null) {
lockfileToSave.onlyBuiltDependencies = lockfileToSave.onlyBuiltDependencies.sort()
}
if (!lockfileToSave.packageExtensionsChecksum) {
delete lockfileToSave.packageExtensionsChecksum
}
return lockfileToSave
}
export default async function writeLockfiles (
opts: {
forceSharedFormat?: boolean
useInlineSpecifiersFormat?: boolean
wantedLockfile: Lockfile
wantedLockfileDir: string
currentLockfile: Lockfile
currentLockfileDir: string
useGitBranchLockfile?: boolean
mergeGitBranchLockfiles?: boolean
}
) {
const wantedLockfileName: string = await getWantedLockfileName(opts)
const wantedLockfilePath = path.join(opts.wantedLockfileDir, wantedLockfileName)
const currentLockfilePath = path.join(opts.currentLockfileDir, 'lock.yaml')
// empty lockfile is not saved
if (isEmptyLockfile(opts.wantedLockfile)) {
await Promise.all([
rimraf(wantedLockfilePath),
rimraf(currentLockfilePath),
])
return
}
const forceSharedFormat = opts?.forceSharedFormat === true
const wantedLockfileToStringify = (opts.useInlineSpecifiersFormat ?? false)
? convertToInlineSpecifiersFormat(opts.wantedLockfile) as unknown as Lockfile
: opts.wantedLockfile
const yamlDoc = yamlStringify(wantedLockfileToStringify, forceSharedFormat)
// in most cases the `pnpm-lock.yaml` and `node_modules/.pnpm-lock.yaml` are equal
// in those cases the YAML document can be stringified only once for both files
// which is more efficient
if (opts.wantedLockfile === opts.currentLockfile) {
await Promise.all([
writeFileAtomic(wantedLockfilePath, yamlDoc),
(async () => {
await fs.mkdir(path.dirname(currentLockfilePath), { recursive: true })
await writeFileAtomic(currentLockfilePath, yamlDoc)
})(),
])
return
}
logger.debug({
message: `\`${WANTED_LOCKFILE}\` differs from \`${path.relative(opts.wantedLockfileDir, currentLockfilePath)}\``,
prefix: opts.wantedLockfileDir,
})
const currentYamlDoc = yamlStringify(opts.currentLockfile, forceSharedFormat)
await Promise.all([
writeFileAtomic(wantedLockfilePath, yamlDoc),
(async () => {
await fs.mkdir(path.dirname(currentLockfilePath), { recursive: true })
await writeFileAtomic(currentLockfilePath, currentYamlDoc)
})(),
])
}