-
-
Notifications
You must be signed in to change notification settings - Fork 931
/
index.ts
570 lines (535 loc) · 17.6 KB
/
index.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
import { promises as fs } from 'fs'
import path from 'path'
import { contextLogger, packageManifestLogger } from '@pnpm/core-loggers'
import { PnpmError } from '@pnpm/error'
import { type Lockfile } from '@pnpm/lockfile-file'
import { logger } from '@pnpm/logger'
import {
type IncludedDependencies,
type Modules,
} from '@pnpm/modules-yaml'
import { readProjectsContext } from '@pnpm/read-projects-context'
import {
DEPENDENCIES_FIELDS,
type HoistedDependencies,
type ProjectManifest,
type ReadPackageHook,
type Registries,
} from '@pnpm/types'
import rimraf from '@zkochan/rimraf'
import { isCI } from 'ci-info'
import enquirer from 'enquirer'
import pathAbsolute from 'path-absolute'
import clone from 'ramda/src/clone'
import equals from 'ramda/src/equals'
import { checkCompatibility } from './checkCompatibility'
import { UnexpectedStoreError } from './checkCompatibility/UnexpectedStoreError'
import { UnexpectedVirtualStoreDirError } from './checkCompatibility/UnexpectedVirtualStoreDirError'
import { readLockfiles } from './readLockfiles'
export { UnexpectedStoreError, UnexpectedVirtualStoreDirError }
export interface PnpmContext {
currentLockfile: Lockfile
currentLockfileIsUpToDate: boolean
existsCurrentLockfile: boolean
existsWantedLockfile: boolean
existsNonEmptyWantedLockfile: boolean
extraBinPaths: string[]
extraNodePaths: string[]
lockfileHadConflicts: boolean
hoistedDependencies: HoistedDependencies
include: IncludedDependencies
modulesFile: Modules | null
pendingBuilds: string[]
projects: Record<string, {
modulesDir: string
id: string
} & HookOptions & Required<ProjectOptions>>
rootModulesDir: string
hoistPattern: string[] | undefined
hoistedModulesDir: string
publicHoistPattern: string[] | undefined
lockfileDir: string
virtualStoreDir: string
skipped: Set<string>
storeDir: string
wantedLockfile: Lockfile
wantedLockfileIsModified: boolean
registries: Registries
}
export interface ProjectOptions {
buildIndex: number
binsDir?: string
manifest: ProjectManifest
modulesDir?: string
rootDir: string
}
interface HookOptions {
originalManifest?: ProjectManifest
}
export interface GetContextOptions {
autoInstallPeers: boolean
excludeLinksFromLockfile: boolean
allProjects: Array<ProjectOptions & HookOptions>
confirmModulesPurge?: boolean
force: boolean
forceNewModules?: boolean
forceSharedLockfile: boolean
frozenLockfile?: boolean
extraBinPaths: string[]
extendNodePath?: boolean
lockfileDir: string
modulesDir?: string
nodeLinker: 'isolated' | 'hoisted' | 'pnp'
readPackageHook?: ReadPackageHook
include?: IncludedDependencies
registries: Registries
storeDir: string
useLockfile: boolean
useGitBranchLockfile?: boolean
mergeGitBranchLockfiles?: boolean
virtualStoreDir?: string
hoistPattern?: string[] | undefined
forceHoistPattern?: boolean
publicHoistPattern?: string[] | undefined
forcePublicHoistPattern?: boolean
global?: boolean
}
export async function getContext (
opts: GetContextOptions
): Promise<PnpmContext> {
const modulesDir = opts.modulesDir ?? 'node_modules'
let importersContext = await readProjectsContext(opts.allProjects, { lockfileDir: opts.lockfileDir, modulesDir })
const virtualStoreDir = pathAbsolute(opts.virtualStoreDir ?? path.join(modulesDir, '.pnpm'), opts.lockfileDir)
if (importersContext.modules != null) {
const { purged } = await validateModules(importersContext.modules, importersContext.projects, {
currentHoistPattern: importersContext.currentHoistPattern,
currentPublicHoistPattern: importersContext.currentPublicHoistPattern,
forceNewModules: opts.forceNewModules === true,
include: opts.include,
lockfileDir: opts.lockfileDir,
modulesDir,
registries: opts.registries,
storeDir: opts.storeDir,
virtualStoreDir,
confirmModulesPurge: opts.confirmModulesPurge && !isCI,
forceHoistPattern: opts.forceHoistPattern,
hoistPattern: opts.hoistPattern,
forcePublicHoistPattern: opts.forcePublicHoistPattern,
publicHoistPattern: opts.publicHoistPattern,
global: opts.global,
})
if (purged) {
importersContext = await readProjectsContext(opts.allProjects, {
lockfileDir: opts.lockfileDir,
modulesDir,
})
}
}
await fs.mkdir(opts.storeDir, { recursive: true })
opts.allProjects.forEach((project) => {
packageManifestLogger.debug({
initial: project.manifest,
prefix: project.rootDir,
})
})
if (opts.readPackageHook != null) {
for (const project of importersContext.projects) {
project.originalManifest = project.manifest
project.manifest = await opts.readPackageHook(clone(project.manifest), project.rootDir)
}
}
const extraBinPaths = [
...opts.extraBinPaths || [],
]
const hoistedModulesDir = path.join(virtualStoreDir, 'node_modules')
if (opts.hoistPattern?.length) {
extraBinPaths.unshift(path.join(hoistedModulesDir, '.bin'))
}
const hoistPattern = importersContext.currentHoistPattern ?? opts.hoistPattern
const ctx: PnpmContext = {
extraBinPaths,
extraNodePaths: getExtraNodePaths({ extendNodePath: opts.extendNodePath, nodeLinker: opts.nodeLinker, hoistPattern, virtualStoreDir }),
hoistedDependencies: importersContext.hoistedDependencies,
hoistedModulesDir,
hoistPattern,
include: opts.include ?? importersContext.include,
lockfileDir: opts.lockfileDir,
modulesFile: importersContext.modules,
pendingBuilds: importersContext.pendingBuilds,
projects: Object.fromEntries(importersContext.projects.map((project) => [project.rootDir, project])),
publicHoistPattern: importersContext.currentPublicHoistPattern ?? opts.publicHoistPattern,
registries: {
...opts.registries,
...importersContext.registries,
},
rootModulesDir: importersContext.rootModulesDir,
skipped: importersContext.skipped,
storeDir: opts.storeDir,
virtualStoreDir,
...await readLockfiles({
autoInstallPeers: opts.autoInstallPeers,
excludeLinksFromLockfile: opts.excludeLinksFromLockfile,
force: opts.force,
forceSharedLockfile: opts.forceSharedLockfile,
frozenLockfile: opts.frozenLockfile === true,
lockfileDir: opts.lockfileDir,
projects: importersContext.projects,
registry: opts.registries.default,
useLockfile: opts.useLockfile,
useGitBranchLockfile: opts.useGitBranchLockfile,
mergeGitBranchLockfiles: opts.mergeGitBranchLockfiles,
virtualStoreDir,
}),
}
contextLogger.debug({
currentLockfileExists: ctx.existsCurrentLockfile,
storeDir: opts.storeDir,
virtualStoreDir,
})
return ctx
}
async function validateModules (
modules: Modules,
projects: Array<{
modulesDir: string
id: string
rootDir: string
}>,
opts: {
currentHoistPattern?: string[]
currentPublicHoistPattern?: string[]
forceNewModules: boolean
include?: IncludedDependencies
lockfileDir: string
modulesDir: string
registries: Registries
storeDir: string
virtualStoreDir: string
confirmModulesPurge?: boolean
hoistPattern?: string[] | undefined
forceHoistPattern?: boolean
publicHoistPattern?: string[] | undefined
forcePublicHoistPattern?: boolean
global?: boolean
}
): Promise<{ purged: boolean }> {
const rootProject = projects.find(({ id }) => id === '.')
if (
opts.forcePublicHoistPattern &&
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
!equals(modules.publicHoistPattern, opts.publicHoistPattern || undefined)
) {
if (opts.forceNewModules && (rootProject != null)) {
await purgeModulesDirsOfImporter(opts, rootProject)
return { purged: true }
}
throw new PnpmError(
'PUBLIC_HOIST_PATTERN_DIFF',
'This modules directory was created using a different public-hoist-pattern value.' +
' Run "pnpm install" to recreate the modules directory.'
)
}
let purged = false
if (opts.forceHoistPattern && (rootProject != null)) {
try {
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
if (!equals(opts.currentHoistPattern, opts.hoistPattern || undefined)) {
throw new PnpmError(
'HOIST_PATTERN_DIFF',
'This modules directory was created using a different hoist-pattern value.' +
' Run "pnpm install" to recreate the modules directory.'
)
}
} catch (err: any) { // eslint-disable-line
if (!opts.forceNewModules) throw err
await purgeModulesDirsOfImporter(opts, rootProject)
purged = true
}
}
await Promise.all(projects.map(async (project) => {
try {
checkCompatibility(modules, {
modulesDir: project.modulesDir,
storeDir: opts.storeDir,
virtualStoreDir: opts.virtualStoreDir,
})
if (opts.lockfileDir !== project.rootDir && (opts.include != null) && modules.included) {
for (const depsField of DEPENDENCIES_FIELDS) {
if (opts.include[depsField] !== modules.included[depsField]) {
throw new PnpmError('INCLUDED_DEPS_CONFLICT',
`modules directory (at "${opts.lockfileDir}") was installed with ${stringifyIncludedDeps(modules.included)}. ` +
`Current install wants ${stringifyIncludedDeps(opts.include)}.`
)
}
}
}
} catch (err: any) { // eslint-disable-line
if (!opts.forceNewModules) throw err
await purgeModulesDirsOfImporter(opts, project)
purged = true
}
}))
if ((modules.registries != null) && !equals(opts.registries, modules.registries)) {
if (opts.forceNewModules) {
await purgeModulesDirsOfImporters(opts, projects)
return { purged: true }
}
throw new PnpmError('REGISTRIES_MISMATCH', `This modules directory was created using the following registries configuration: ${JSON.stringify(modules.registries)}. The current configuration is ${JSON.stringify(opts.registries)}. To recreate the modules directory using the new settings, run "pnpm install${opts.global ? ' -g' : ''}".`)
}
if (purged && (rootProject == null)) {
await purgeModulesDirsOfImporter(opts, {
modulesDir: path.join(opts.lockfileDir, opts.modulesDir),
rootDir: opts.lockfileDir,
})
}
return { purged }
}
async function purgeModulesDirsOfImporter (
opts: {
confirmModulesPurge?: boolean
virtualStoreDir: string
},
importer: {
modulesDir: string
rootDir: string
}
) {
return purgeModulesDirsOfImporters(opts, [importer])
}
async function purgeModulesDirsOfImporters (
opts: {
confirmModulesPurge?: boolean
virtualStoreDir: string
},
importers: Array<{
modulesDir: string
rootDir: string
}>
) {
if (opts.confirmModulesPurge ?? true) {
const confirmed = await enquirer.prompt({
type: 'confirm',
name: 'question',
message: importers.length === 1
? `The modules directory at "${importers[0].modulesDir}" will be removed and reinstalled from scratch. Proceed?`
: 'The modules directories will be removed and reinstalled from scratch. Proceed?',
initial: true,
})
if (!confirmed) {
throw new PnpmError('ABORTED_REMOVE_MODULES_DIR', 'Aborted removal of modules directory')
}
}
for (const importer of importers) {
logger.info({
message: `Recreating ${importer.modulesDir}`,
prefix: importer.rootDir,
})
try {
// We don't remove the actual modules directory, just the contents of it.
// 1. we will need the directory anyway.
// 2. in some setups, pnpm won't even have permission to remove the modules directory.
await removeContentsOfDir(importer.modulesDir, opts.virtualStoreDir)
} catch (err: any) { // eslint-disable-line
if (err.code !== 'ENOENT') throw err
}
}
}
async function removeContentsOfDir (dir: string, virtualStoreDir: string) {
const items = await fs.readdir(dir)
for (const item of items) {
// The non-pnpm related hidden files are kept
if (
item.startsWith('.') &&
item !== '.bin' &&
item !== '.modules.yaml' &&
!dirsAreEqual(path.join(dir, item), virtualStoreDir)
) {
continue
}
await rimraf(path.join(dir, item))
}
}
function dirsAreEqual (dir1: string, dir2: string) {
return path.relative(dir1, dir2) === ''
}
function stringifyIncludedDeps (included: IncludedDependencies) {
return DEPENDENCIES_FIELDS.filter((depsField) => included[depsField]).join(', ')
}
export interface PnpmSingleContext {
currentLockfile: Lockfile
currentLockfileIsUpToDate: boolean
existsCurrentLockfile: boolean
existsWantedLockfile: boolean
existsNonEmptyWantedLockfile: boolean
extraBinPaths: string[]
extraNodePaths: string[]
lockfileHadConflicts: boolean
hoistedDependencies: HoistedDependencies
hoistedModulesDir: string
hoistPattern: string[] | undefined
manifest: ProjectManifest
modulesDir: string
importerId: string
prefix: string
include: IncludedDependencies
modulesFile: Modules | null
pendingBuilds: string[]
publicHoistPattern: string[] | undefined
registries: Registries
rootModulesDir: string
lockfileDir: string
virtualStoreDir: string
skipped: Set<string>
storeDir: string
wantedLockfile: Lockfile
wantedLockfileIsModified: boolean
}
export async function getContextForSingleImporter (
manifest: ProjectManifest,
opts: {
autoInstallPeers: boolean
excludeLinksFromLockfile: boolean
force: boolean
forceNewModules?: boolean
forceSharedLockfile: boolean
confirmModulesPurge?: boolean
extraBinPaths: string[]
extendNodePath?: boolean
lockfileDir: string
nodeLinker: 'isolated' | 'hoisted' | 'pnp'
modulesDir?: string
readPackageHook?: ReadPackageHook
include?: IncludedDependencies
dir: string
registries: Registries
storeDir: string
useLockfile: boolean
useGitBranchLockfile?: boolean
mergeGitBranchLockfiles?: boolean
virtualStoreDir?: string
hoistPattern?: string[] | undefined
forceHoistPattern?: boolean
publicHoistPattern?: string[] | undefined
forcePublicHoistPattern?: boolean
},
alreadyPurged: boolean = false
): Promise<PnpmSingleContext> {
const {
currentHoistPattern,
currentPublicHoistPattern,
hoistedDependencies,
projects,
include,
modules,
pendingBuilds,
registries,
skipped,
rootModulesDir,
} = await readProjectsContext(
[
{
rootDir: opts.dir,
},
],
{
lockfileDir: opts.lockfileDir,
modulesDir: opts.modulesDir,
}
)
const storeDir = opts.storeDir
const importer = projects[0]
const modulesDir = importer.modulesDir
const importerId = importer.id
const virtualStoreDir = pathAbsolute(opts.virtualStoreDir ?? 'node_modules/.pnpm', opts.lockfileDir)
if ((modules != null) && !alreadyPurged) {
const { purged } = await validateModules(modules, projects, {
currentHoistPattern,
currentPublicHoistPattern,
forceNewModules: opts.forceNewModules === true,
include: opts.include,
lockfileDir: opts.lockfileDir,
modulesDir: opts.modulesDir ?? 'node_modules',
registries: opts.registries,
storeDir: opts.storeDir,
virtualStoreDir,
confirmModulesPurge: opts.confirmModulesPurge && !isCI,
forceHoistPattern: opts.forceHoistPattern,
hoistPattern: opts.hoistPattern,
forcePublicHoistPattern: opts.forcePublicHoistPattern,
publicHoistPattern: opts.publicHoistPattern,
})
if (purged) {
return getContextForSingleImporter(manifest, opts, true)
}
}
await fs.mkdir(storeDir, { recursive: true })
const extraBinPaths = [
...opts.extraBinPaths || [],
]
const hoistedModulesDir = path.join(virtualStoreDir, 'node_modules')
if (opts.hoistPattern?.length) {
extraBinPaths.unshift(path.join(hoistedModulesDir, '.bin'))
}
const hoistPattern = currentHoistPattern ?? opts.hoistPattern
const ctx: PnpmSingleContext = {
extraBinPaths,
extraNodePaths: getExtraNodePaths({ extendNodePath: opts.extendNodePath, nodeLinker: opts.nodeLinker, hoistPattern, virtualStoreDir }),
hoistedDependencies,
hoistedModulesDir,
hoistPattern,
importerId,
include: opts.include ?? include,
lockfileDir: opts.lockfileDir,
manifest: await opts.readPackageHook?.(manifest) ?? manifest,
modulesDir,
modulesFile: modules,
pendingBuilds,
prefix: opts.dir,
publicHoistPattern: currentPublicHoistPattern ?? opts.publicHoistPattern,
registries: {
...opts.registries,
...registries,
},
rootModulesDir,
skipped,
storeDir,
virtualStoreDir,
...await readLockfiles({
autoInstallPeers: opts.autoInstallPeers,
excludeLinksFromLockfile: opts.excludeLinksFromLockfile,
force: opts.force,
forceSharedLockfile: opts.forceSharedLockfile,
frozenLockfile: false,
lockfileDir: opts.lockfileDir,
projects: [{ id: importerId, rootDir: opts.dir }],
registry: opts.registries.default,
useLockfile: opts.useLockfile,
useGitBranchLockfile: opts.useGitBranchLockfile,
mergeGitBranchLockfiles: opts.mergeGitBranchLockfiles,
virtualStoreDir,
}),
}
packageManifestLogger.debug({
initial: manifest,
prefix: opts.dir,
})
contextLogger.debug({
currentLockfileExists: ctx.existsCurrentLockfile,
storeDir: opts.storeDir,
virtualStoreDir,
})
return ctx
}
function getExtraNodePaths (
{ extendNodePath = true, hoistPattern, nodeLinker, virtualStoreDir }: {
extendNodePath?: boolean
hoistPattern?: string[]
nodeLinker: 'isolated' | 'hoisted' | 'pnp'
virtualStoreDir: string
}
) {
if (extendNodePath && nodeLinker === 'isolated' && hoistPattern?.length) {
return [path.join(virtualStoreDir, 'node_modules')]
}
return []
}