/
index.ts
894 lines (846 loc) · 29.5 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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
import { promises as fs } from 'fs'
import path from 'path'
import { buildModules } from '@pnpm/build-modules'
import { calcDepState, type DepsStateCache } from '@pnpm/calc-dep-state'
import {
LAYOUT_VERSION,
WANTED_LOCKFILE,
} from '@pnpm/constants'
import {
packageManifestLogger,
progressLogger,
stageLogger,
statsLogger,
summaryLogger,
} from '@pnpm/core-loggers'
import { PnpmError } from '@pnpm/error'
import {
filterLockfileByImportersAndEngine,
} from '@pnpm/filter-lockfile'
import { hoist } from '@pnpm/hoist'
import {
runLifecycleHooksConcurrently,
makeNodeRequireOption,
} from '@pnpm/lifecycle'
import { linkBins, linkBinsOfPackages } from '@pnpm/link-bins'
import {
getLockfileImporterId,
type Lockfile,
readCurrentLockfile,
readWantedLockfile,
writeLockfiles,
writeCurrentLockfile,
type PatchFile,
} from '@pnpm/lockfile-file'
import { writePnpFile } from '@pnpm/lockfile-to-pnp'
import {
extendProjectsWithTargetDirs,
nameVerFromPkgSnapshot,
satisfiesPackageManifest,
} from '@pnpm/lockfile-utils'
import {
type LogBase,
logger,
streamParser,
} from '@pnpm/logger'
import { prune } from '@pnpm/modules-cleaner'
import {
type IncludedDependencies,
writeModulesManifest,
} from '@pnpm/modules-yaml'
import { type HoistingLimits } from '@pnpm/real-hoist'
import { readPackageJsonFromDir } from '@pnpm/read-package-json'
import { readProjectManifestOnly, safeReadProjectManifestOnly } from '@pnpm/read-project-manifest'
import {
type PackageFilesResponse,
type StoreController,
} from '@pnpm/store-controller-types'
import { symlinkDependency } from '@pnpm/symlink-dependency'
import { type DependencyManifest, type HoistedDependencies, type ProjectManifest, type Registries, DEPENDENCIES_FIELDS } from '@pnpm/types'
import * as dp from '@pnpm/dependency-path'
import pLimit from 'p-limit'
import pathAbsolute from 'path-absolute'
import equals from 'ramda/src/equals'
import isEmpty from 'ramda/src/isEmpty'
import omit from 'ramda/src/omit'
import pick from 'ramda/src/pick'
import pickBy from 'ramda/src/pickBy'
import props from 'ramda/src/props'
import union from 'ramda/src/union'
import realpathMissing from 'realpath-missing'
import { linkHoistedModules } from './linkHoistedModules'
import {
type DirectDependenciesByImporterId,
type DependenciesGraph,
type DependenciesGraphNode,
type LockfileToDepGraphOptions,
lockfileToDepGraph,
} from './lockfileToDepGraph'
import { lockfileToHoistedDepGraph } from './lockfileToHoistedDepGraph'
import { linkDirectDeps, type LinkedDirectDep } from '@pnpm/pkg-manager.direct-dep-linker'
export type { HoistingLimits }
export type ReporterFunction = (logObj: LogBase) => void
export interface Project {
binsDir: string
buildIndex: number
manifest: ProjectManifest
modulesDir: string
id: string
pruneDirectDependencies?: boolean
rootDir: string
}
export interface HeadlessOptions {
autoInstallPeers?: boolean
childConcurrency?: number
currentLockfile?: Lockfile
currentEngine: {
nodeVersion: string
pnpmVersion: string
}
dedupeDirectDeps?: boolean
enablePnp?: boolean
engineStrict: boolean
excludeLinksFromLockfile?: boolean
extraBinPaths?: string[]
extraEnv?: Record<string, string>
extraNodePaths?: string[]
preferSymlinkedExecutables?: boolean
hoistingLimits?: HoistingLimits
externalDependencies?: Set<string>
ignoreDepScripts: boolean
ignoreScripts: boolean
ignorePackageManifest?: boolean
include: IncludedDependencies
selectedProjectDirs: string[]
allProjects: Record<string, Project>
prunedAt?: string
hoistedDependencies: HoistedDependencies
hoistPattern?: string[]
publicHoistPattern?: string[]
currentHoistedLocations?: Record<string, string[]>
lockfileDir: string
modulesDir?: string
virtualStoreDir?: string
patchedDependencies?: Record<string, PatchFile>
scriptsPrependNodePath?: boolean | 'warn-only'
scriptShell?: string
shellEmulator?: boolean
storeController: StoreController
sideEffectsCacheRead: boolean
sideEffectsCacheWrite: boolean
symlink?: boolean
force: boolean
storeDir: string
rawConfig: object
unsafePerm: boolean
userAgent: string
registries: Registries
reporter?: ReporterFunction
packageManager: {
name: string
version: string
}
pruneStore: boolean
pruneVirtualStore?: boolean
wantedLockfile?: Lockfile
ownLifecycleHooksStdio?: 'inherit' | 'pipe'
pendingBuilds: string[]
resolveSymlinksInInjectedDirs?: boolean
skipped: Set<string>
enableModulesDir?: boolean
nodeLinker?: 'isolated' | 'hoisted' | 'pnp'
useGitBranchLockfile?: boolean
useLockfile?: boolean
}
export async function headlessInstall (opts: HeadlessOptions) {
const reporter = opts.reporter
if ((reporter != null) && typeof reporter === 'function') {
streamParser.on('data', reporter)
}
const lockfileDir = opts.lockfileDir
const wantedLockfile = opts.wantedLockfile ?? await readWantedLockfile(lockfileDir, {
ignoreIncompatible: false,
useGitBranchLockfile: opts.useGitBranchLockfile,
// mergeGitBranchLockfiles is intentionally not supported in headless
mergeGitBranchLockfiles: false,
})
if (wantedLockfile == null) {
throw new Error(`Headless installation requires a ${WANTED_LOCKFILE} file`)
}
const depsStateCache: DepsStateCache = {}
const relativeModulesDir = opts.modulesDir ?? 'node_modules'
const rootModulesDir = await realpathMissing(path.join(lockfileDir, relativeModulesDir))
const virtualStoreDir = pathAbsolute(opts.virtualStoreDir ?? path.join(relativeModulesDir, '.pnpm'), lockfileDir)
const currentLockfile = opts.currentLockfile ?? await readCurrentLockfile(virtualStoreDir, { ignoreIncompatible: false })
const hoistedModulesDir = path.join(virtualStoreDir, 'node_modules')
const publicHoistedModulesDir = rootModulesDir
const selectedProjects = Object.values(pick(opts.selectedProjectDirs, opts.allProjects))
if (!opts.ignorePackageManifest) {
const _satisfiesPackageManifest = satisfiesPackageManifest.bind(null, {
autoInstallPeers: opts.autoInstallPeers,
excludeLinksFromLockfile: opts.excludeLinksFromLockfile,
})
for (const { id, manifest, rootDir } of selectedProjects) {
if (!_satisfiesPackageManifest(wantedLockfile.importers[id], manifest)) {
throw new PnpmError('OUTDATED_LOCKFILE',
`Cannot install with "frozen-lockfile" because ${WANTED_LOCKFILE} is not up to date with ` +
path.relative(lockfileDir, path.join(rootDir, 'package.json')), {
hint: 'Note that in CI environments this setting is true by default. If you still need to run install in such cases, use "pnpm install --no-frozen-lockfile"',
})
}
}
}
const scriptsOpts = {
optional: false,
extraBinPaths: opts.extraBinPaths,
extraEnv: opts.extraEnv,
rawConfig: opts.rawConfig,
resolveSymlinksInInjectedDirs: opts.resolveSymlinksInInjectedDirs,
scriptsPrependNodePath: opts.scriptsPrependNodePath,
scriptShell: opts.scriptShell,
shellEmulator: opts.shellEmulator,
stdio: opts.ownLifecycleHooksStdio ?? 'inherit',
storeController: opts.storeController,
unsafePerm: opts.unsafePerm || false,
}
const skipped = opts.skipped || new Set<string>()
if (opts.nodeLinker !== 'hoisted') {
if (currentLockfile != null && !opts.ignorePackageManifest) {
await prune(
selectedProjects,
{
currentLockfile,
dryRun: false,
hoistedDependencies: opts.hoistedDependencies,
hoistedModulesDir: (opts.hoistPattern == null) ? undefined : hoistedModulesDir,
include: opts.include,
lockfileDir,
pruneStore: opts.pruneStore,
pruneVirtualStore: opts.pruneVirtualStore,
publicHoistedModulesDir: (opts.publicHoistPattern == null) ? undefined : publicHoistedModulesDir,
registries: opts.registries,
skipped,
storeController: opts.storeController,
virtualStoreDir,
wantedLockfile,
}
)
} else {
statsLogger.debug({
prefix: lockfileDir,
removed: 0,
})
}
}
stageLogger.debug({
prefix: lockfileDir,
stage: 'importing_started',
})
const filterOpts = {
include: opts.include,
registries: opts.registries,
skipped,
}
const initialImporterIds = (opts.ignorePackageManifest === true || opts.nodeLinker === 'hoisted')
? Object.keys(wantedLockfile.importers)
: selectedProjects.map(({ id }) => id)
const { lockfile: filteredLockfile, selectedImporterIds: importerIds } = filterLockfileByImportersAndEngine(wantedLockfile, initialImporterIds, {
...filterOpts,
currentEngine: opts.currentEngine,
engineStrict: opts.engineStrict,
failOnMissingDependencies: true,
includeIncompatiblePackages: opts.force,
lockfileDir,
})
if (opts.excludeLinksFromLockfile) {
for (const { id, manifest } of selectedProjects) {
if (filteredLockfile.importers[id]) {
for (const depType of DEPENDENCIES_FIELDS) {
filteredLockfile.importers[id][depType] = {
...filteredLockfile.importers[id][depType],
...Object.entries(manifest[depType] ?? {})
.filter(([_, spec]) => spec.startsWith('link:'))
.reduce((acc, [depName, spec]) => {
acc[depName] = `link:${path.relative(opts.lockfileDir, spec.substring(5))}`
return acc
}, {} as Record<string, string>),
}
}
}
}
}
// Update selectedProjects to add missing projects. importerIds will have the updated ids, found from deeply linked workspace projects
const initialImporterIdSet = new Set(initialImporterIds)
const missingIds = importerIds.filter((importerId) => !initialImporterIdSet.has(importerId))
if (missingIds.length > 0) {
for (const project of Object.values(opts.allProjects)) {
if (missingIds.includes(project.id)) {
selectedProjects.push(project)
}
}
}
const lockfileToDepGraphOpts = {
...opts,
importerIds,
lockfileDir,
skipped,
virtualStoreDir,
nodeVersion: opts.currentEngine.nodeVersion,
pnpmVersion: opts.currentEngine.pnpmVersion,
} as LockfileToDepGraphOptions
const {
directDependenciesByImporterId,
graph,
hierarchy,
hoistedLocations,
pkgLocationsByDepPath,
prevGraph,
symlinkedDirectDependenciesByImporterId,
} = await (
opts.nodeLinker === 'hoisted'
? lockfileToHoistedDepGraph(
filteredLockfile,
currentLockfile,
lockfileToDepGraphOpts
)
: lockfileToDepGraph(
filteredLockfile,
opts.force ? null : currentLockfile,
lockfileToDepGraphOpts
)
)
if (opts.enablePnp) {
const importerNames = Object.fromEntries(
selectedProjects.map(({ manifest, id }) => [id, manifest.name ?? id])
)
await writePnpFile(filteredLockfile, {
importerNames,
lockfileDir,
virtualStoreDir,
registries: opts.registries,
})
}
const depNodes = Object.values(graph)
statsLogger.debug({
added: depNodes.length,
prefix: lockfileDir,
})
function warn (message: string) {
logger.info({
message,
prefix: lockfileDir,
})
}
let newHoistedDependencies!: HoistedDependencies
if (opts.nodeLinker === 'hoisted' && hierarchy && prevGraph) {
await linkHoistedModules(opts.storeController, graph, prevGraph, hierarchy, {
depsStateCache,
force: opts.force,
ignoreScripts: opts.ignoreScripts,
lockfileDir: opts.lockfileDir,
preferSymlinkedExecutables: opts.preferSymlinkedExecutables,
sideEffectsCacheRead: opts.sideEffectsCacheRead,
})
stageLogger.debug({
prefix: lockfileDir,
stage: 'importing_done',
})
await symlinkDirectDependencies({
directDependenciesByImporterId: symlinkedDirectDependenciesByImporterId!,
dedupe: Boolean(opts.dedupeDirectDeps),
filteredLockfile,
lockfileDir,
projects: selectedProjects,
registries: opts.registries,
symlink: opts.symlink,
})
} else if (opts.enableModulesDir !== false) {
await Promise.all(depNodes.map(async (depNode) => fs.mkdir(depNode.modules, { recursive: true })))
await Promise.all([
opts.symlink === false
? Promise.resolve()
: linkAllModules(depNodes, {
lockfileDir,
optional: opts.include.optionalDependencies,
}),
linkAllPkgs(opts.storeController, depNodes, {
force: opts.force,
depGraph: graph,
depsStateCache,
ignoreScripts: opts.ignoreScripts,
lockfileDir: opts.lockfileDir,
sideEffectsCacheRead: opts.sideEffectsCacheRead,
}),
])
stageLogger.debug({
prefix: lockfileDir,
stage: 'importing_done',
})
if (opts.ignorePackageManifest !== true && (opts.hoistPattern != null || opts.publicHoistPattern != null)) {
// It is important to keep the skipped packages in the lockfile which will be saved as the "current lockfile".
// pnpm is comparing the current lockfile to the wanted one and they should match.
// But for hoisting, we need a version of the lockfile w/o the skipped packages, so we're making a copy.
const hoistLockfile = {
...filteredLockfile,
packages: omit(Array.from(skipped), filteredLockfile.packages),
}
newHoistedDependencies = await hoist({
extraNodePath: opts.extraNodePaths,
lockfile: hoistLockfile,
importerIds,
preferSymlinkedExecutables: opts.preferSymlinkedExecutables,
privateHoistedModulesDir: hoistedModulesDir,
privateHoistPattern: opts.hoistPattern ?? [],
publicHoistedModulesDir,
publicHoistPattern: opts.publicHoistPattern ?? [],
virtualStoreDir,
})
} else {
newHoistedDependencies = {}
}
await linkAllBins(graph, {
extraNodePaths: opts.extraNodePaths,
optional: opts.include.optionalDependencies,
preferSymlinkedExecutables: opts.preferSymlinkedExecutables,
warn,
})
if ((currentLockfile != null) && !equals(importerIds.sort(), Object.keys(filteredLockfile.importers).sort())) {
Object.assign(filteredLockfile.packages!, currentLockfile.packages)
}
/** Skip linking and due to no project manifest */
if (!opts.ignorePackageManifest) {
await symlinkDirectDependencies({
dedupe: Boolean(opts.dedupeDirectDeps),
directDependenciesByImporterId,
filteredLockfile,
lockfileDir,
projects: selectedProjects,
registries: opts.registries,
symlink: opts.symlink,
})
}
}
if (opts.ignoreScripts) {
for (const { id, manifest } of selectedProjects) {
if (opts.ignoreScripts && ((manifest?.scripts) != null) &&
(manifest.scripts.preinstall ?? manifest.scripts.prepublish ??
manifest.scripts.install ??
manifest.scripts.postinstall ??
manifest.scripts.prepare)
) {
opts.pendingBuilds.push(id)
}
}
// we can use concat here because we always only append new packages, which are guaranteed to not be there by definition
opts.pendingBuilds = opts.pendingBuilds
.concat(
depNodes
.filter(({ requiresBuild }) => requiresBuild)
.map(({ depPath }) => depPath)
)
}
if (!opts.ignoreScripts || Object.keys(opts.patchedDependencies ?? {}).length > 0) {
const directNodes = new Set<string>()
for (const id of union(importerIds, ['.'])) {
Object
.values(directDependenciesByImporterId[id] ?? {})
.filter((loc) => graph[loc])
.forEach((loc) => {
directNodes.add(loc)
})
}
const extraBinPaths = [...opts.extraBinPaths ?? []]
if (opts.hoistPattern != null) {
extraBinPaths.unshift(path.join(virtualStoreDir, 'node_modules/.bin'))
}
let extraEnv: Record<string, string> | undefined = opts.extraEnv
if (opts.enablePnp) {
extraEnv = {
...extraEnv,
...makeNodeRequireOption(path.join(opts.lockfileDir, '.pnp.cjs')),
}
}
await buildModules(graph, Array.from(directNodes), {
childConcurrency: opts.childConcurrency,
extraBinPaths,
extraEnv,
depsStateCache,
ignoreScripts: opts.ignoreScripts || opts.ignoreDepScripts,
hoistedLocations,
lockfileDir,
optional: opts.include.optionalDependencies,
preferSymlinkedExecutables: opts.preferSymlinkedExecutables,
rawConfig: opts.rawConfig,
rootModulesDir: virtualStoreDir,
scriptsPrependNodePath: opts.scriptsPrependNodePath,
scriptShell: opts.scriptShell,
shellEmulator: opts.shellEmulator,
sideEffectsCacheWrite: opts.sideEffectsCacheWrite,
storeController: opts.storeController,
unsafePerm: opts.unsafePerm,
userAgent: opts.userAgent,
})
}
const projectsToBeBuilt = extendProjectsWithTargetDirs(selectedProjects, wantedLockfile, {
pkgLocationsByDepPath,
virtualStoreDir,
})
if (opts.enableModulesDir !== false) {
/** Skip linking and due to no project manifest */
if (!opts.ignorePackageManifest) {
await Promise.all(selectedProjects.map(async (project) => {
if (opts.nodeLinker === 'hoisted' || opts.publicHoistPattern?.length && path.relative(opts.lockfileDir, project.rootDir) === '') {
await linkBinsOfImporter(project, {
extraNodePaths: opts.extraNodePaths,
preferSymlinkedExecutables: opts.preferSymlinkedExecutables,
})
} else {
const directPkgDirs = Object.values(directDependenciesByImporterId[project.id])
await linkBinsOfPackages(
(
await Promise.all(
directPkgDirs.map(async (dir) => ({
location: dir,
manifest: await safeReadProjectManifestOnly(dir),
}))
)
)
.filter(({ manifest }) => manifest != null) as Array<{ location: string, manifest: DependencyManifest }>,
project.binsDir,
{
extraNodePaths: opts.extraNodePaths,
preferSymlinkedExecutables: opts.preferSymlinkedExecutables,
}
)
}
}))
}
const injectedDeps: Record<string, string[]> = {}
for (const project of projectsToBeBuilt) {
if (project.targetDirs.length > 0) {
injectedDeps[project.id] = project.targetDirs.map((targetDir) => path.relative(opts.lockfileDir, targetDir))
}
}
await writeModulesManifest(rootModulesDir, {
hoistedDependencies: newHoistedDependencies,
hoistPattern: opts.hoistPattern,
included: opts.include,
injectedDeps,
layoutVersion: LAYOUT_VERSION,
hoistedLocations,
nodeLinker: opts.nodeLinker,
packageManager: `${opts.packageManager.name}@${opts.packageManager.version}`,
pendingBuilds: opts.pendingBuilds,
publicHoistPattern: opts.publicHoistPattern,
prunedAt: opts.pruneVirtualStore === true || opts.prunedAt == null
? new Date().toUTCString()
: opts.prunedAt,
registries: opts.registries,
skipped: Array.from(skipped),
storeDir: opts.storeDir,
virtualStoreDir,
}, {
makeModulesDir: Object.keys(filteredLockfile.packages ?? {}).length > 0,
})
if (opts.useLockfile) {
// We need to write the wanted lockfile as well.
// Even though it will only be changed if the workspace will have new projects with no dependencies.
await writeLockfiles({
wantedLockfileDir: opts.lockfileDir,
currentLockfileDir: virtualStoreDir,
wantedLockfile,
currentLockfile: filteredLockfile,
})
} else {
await writeCurrentLockfile(virtualStoreDir, filteredLockfile)
}
}
// waiting till package requests are finished
await Promise.all(depNodes.map(({ finishing }) => finishing))
summaryLogger.debug({ prefix: lockfileDir })
await opts.storeController.close()
if (!opts.ignoreScripts && !opts.ignorePackageManifest) {
await runLifecycleHooksConcurrently(
['preinstall', 'install', 'postinstall', 'prepare'],
projectsToBeBuilt,
opts.childConcurrency ?? 5,
scriptsOpts
)
}
if ((reporter != null) && typeof reporter === 'function') {
streamParser.removeListener('data', reporter)
}
}
type SymlinkDirectDependenciesOpts = Pick<HeadlessOptions, 'registries' | 'symlink' | 'lockfileDir'> & {
filteredLockfile: Lockfile
dedupe: boolean
directDependenciesByImporterId: DirectDependenciesByImporterId
projects: Project[]
}
async function symlinkDirectDependencies (
{
filteredLockfile,
dedupe,
directDependenciesByImporterId,
lockfileDir,
projects,
registries,
symlink,
}: SymlinkDirectDependenciesOpts
) {
projects.forEach(({ rootDir, manifest }) => {
// Even though headless installation will never update the package.json
// this needs to be logged because otherwise install summary won't be printed
packageManifestLogger.debug({
prefix: rootDir,
updated: manifest,
})
})
if (symlink !== false) {
const importerManifestsByImporterId = {} as { [id: string]: ProjectManifest }
for (const { id, manifest } of projects) {
importerManifestsByImporterId[id] = manifest
}
const projectsToLink = Object.fromEntries(await Promise.all(
projects.map(async ({ rootDir, id, modulesDir }) => ([id, {
dir: rootDir,
modulesDir,
dependencies: await getRootPackagesToLink(filteredLockfile, {
importerId: id,
importerModulesDir: modulesDir,
lockfileDir,
projectDir: rootDir,
importerManifestsByImporterId,
registries,
rootDependencies: directDependenciesByImporterId[id],
}),
}]))
))
await linkDirectDeps(projectsToLink, { dedupe: Boolean(dedupe) })
}
}
async function linkBinsOfImporter (
{ manifest, modulesDir, binsDir, rootDir }: {
binsDir: string
manifest: ProjectManifest
modulesDir: string
rootDir: string
},
{ extraNodePaths, preferSymlinkedExecutables }: { extraNodePaths?: string[], preferSymlinkedExecutables?: boolean } = {}
) {
const warn = (message: string) => {
logger.info({ message, prefix: rootDir })
}
return linkBins(modulesDir, binsDir, {
extraNodePaths,
allowExoticManifests: true,
preferSymlinkedExecutables,
projectManifest: manifest,
warn,
})
}
async function getRootPackagesToLink (
lockfile: Lockfile,
opts: {
registries: Registries
projectDir: string
importerId: string
importerModulesDir: string
importerManifestsByImporterId: { [id: string]: ProjectManifest }
lockfileDir: string
rootDependencies: { [alias: string]: string }
}
): Promise<LinkedDirectDep[]> {
const projectSnapshot = lockfile.importers[opts.importerId]
const allDeps = {
...projectSnapshot.devDependencies,
...projectSnapshot.dependencies,
...projectSnapshot.optionalDependencies,
}
return (await Promise.all(
Object.entries(allDeps)
.map(async ([alias, ref]) => {
if (ref.startsWith('link:')) {
const isDev = Boolean(projectSnapshot.devDependencies?.[alias])
const isOptional = Boolean(projectSnapshot.optionalDependencies?.[alias])
const packageDir = path.join(opts.projectDir, ref.slice(5))
const linkedPackage = await (async () => {
const importerId = getLockfileImporterId(opts.lockfileDir, packageDir)
if (opts.importerManifestsByImporterId[importerId]) {
return opts.importerManifestsByImporterId[importerId]
}
try {
// TODO: cover this case with a test
return await readProjectManifestOnly(packageDir) as DependencyManifest
} catch (err: any) { // eslint-disable-line
if (err['code'] !== 'ERR_PNPM_NO_IMPORTER_MANIFEST_FOUND') throw err
return { name: alias, version: '0.0.0' }
}
})() as DependencyManifest
return {
alias,
name: linkedPackage.name,
version: linkedPackage.version,
dir: packageDir,
id: ref,
isExternalLink: true,
dependencyType: isDev && 'dev' ||
isOptional && 'optional' ||
'prod',
}
}
const dir = opts.rootDependencies[alias]
// Skipping linked packages
if (!dir) {
return
}
const isDev = Boolean(projectSnapshot.devDependencies?.[alias])
const isOptional = Boolean(projectSnapshot.optionalDependencies?.[alias])
const depPath = dp.refToRelative(ref, alias)
if (depPath === null) return
const pkgSnapshot = lockfile.packages?.[depPath]
if (pkgSnapshot == null) return // this won't ever happen. Just making typescript happy
const pkgId = pkgSnapshot.id ?? dp.refToAbsolute(ref, alias, opts.registries) ?? undefined
const pkgInfo = nameVerFromPkgSnapshot(depPath, pkgSnapshot)
return {
alias,
isExternalLink: false,
name: pkgInfo.name,
version: pkgInfo.version,
dependencyType: isDev && 'dev' || isOptional && 'optional' || 'prod',
dir,
id: pkgId,
}
})
))
.filter(Boolean) as LinkedDirectDep[]
}
const limitLinking = pLimit(16)
async function linkAllPkgs (
storeController: StoreController,
depNodes: DependenciesGraphNode[],
opts: {
depGraph: DependenciesGraph
depsStateCache: DepsStateCache
force: boolean
ignoreScripts: boolean
lockfileDir: string
sideEffectsCacheRead: boolean
}
) {
return Promise.all(
depNodes.map(async (depNode) => {
let filesResponse!: PackageFilesResponse
try {
filesResponse = await depNode.fetchingFiles()
} catch (err: any) { // eslint-disable-line
if (depNode.optional) return
throw err
}
let sideEffectsCacheKey: string | undefined
if (opts.sideEffectsCacheRead && filesResponse.sideEffects && !isEmpty(filesResponse.sideEffects)) {
sideEffectsCacheKey = calcDepState(opts.depGraph, opts.depsStateCache, depNode.dir, {
isBuilt: !opts.ignoreScripts && depNode.requiresBuild,
patchFileHash: depNode.patchFile?.hash,
})
}
const { importMethod, isBuilt } = await storeController.importPackage(depNode.dir, {
filesResponse,
force: opts.force,
requiresBuild: depNode.requiresBuild || depNode.patchFile != null,
sideEffectsCacheKey,
})
if (importMethod) {
progressLogger.debug({
method: importMethod,
requester: opts.lockfileDir,
status: 'imported',
to: depNode.dir,
})
}
depNode.isBuilt = isBuilt
const selfDep = depNode.children[depNode.name]
if (selfDep) {
const pkg = opts.depGraph[selfDep]
if (!pkg) return
const targetModulesDir = path.join(depNode.modules, depNode.name, 'node_modules')
await limitLinking(async () => symlinkDependency(pkg.dir, targetModulesDir, depNode.name))
}
})
)
}
async function linkAllBins (
depGraph: DependenciesGraph,
opts: {
extraNodePaths?: string[]
optional: boolean
preferSymlinkedExecutables?: boolean
warn: (message: string) => void
}
) {
return Promise.all(
Object.values(depGraph)
.map(async (depNode) => limitLinking(async () => {
const childrenToLink: Record<string, string> = opts.optional
? depNode.children
: pickBy((_, childAlias) => !depNode.optionalDependencies.has(childAlias), depNode.children)
const binPath = path.join(depNode.dir, 'node_modules/.bin')
const pkgSnapshots = props<string, DependenciesGraphNode>(Object.values(childrenToLink), depGraph)
if (pkgSnapshots.includes(undefined as any)) { // eslint-disable-line
await linkBins(depNode.modules, binPath, {
extraNodePaths: opts.extraNodePaths,
preferSymlinkedExecutables: opts.preferSymlinkedExecutables,
warn: opts.warn,
})
} else {
const pkgs = await Promise.all(
pkgSnapshots
.filter(({ hasBin }) => hasBin)
.map(async ({ dir }) => ({
location: dir,
manifest: await readPackageJsonFromDir(dir) as DependencyManifest,
}))
)
await linkBinsOfPackages(pkgs, binPath, {
extraNodePaths: opts.extraNodePaths,
preferSymlinkedExecutables: opts.preferSymlinkedExecutables,
})
}
// link also the bundled dependencies` bins
if (depNode.hasBundledDependencies) {
const bundledModules = path.join(depNode.dir, 'node_modules')
await linkBins(bundledModules, binPath, {
extraNodePaths: opts.extraNodePaths,
preferSymlinkedExecutables: opts.preferSymlinkedExecutables,
warn: opts.warn,
})
}
}))
)
}
async function linkAllModules (
depNodes: DependenciesGraphNode[],
opts: {
optional: boolean
lockfileDir: string
}
) {
await Promise.all(
depNodes
.map(async (depNode) => {
const childrenToLink: Record<string, string> = opts.optional
? depNode.children
: pickBy((_, childAlias) => !depNode.optionalDependencies.has(childAlias), depNode.children)
await Promise.all(
Object.entries(childrenToLink)
.map(async ([alias, pkgDir]) => {
// if (!pkg.installable && pkg.optional) return
if (alias === depNode.name) {
return
}
await limitLinking(() => symlinkDependency(pkgDir, depNode.modules, alias))
})
)
})
)
}