-
-
Notifications
You must be signed in to change notification settings - Fork 937
/
filterLockfileByImportersAndEngine.ts
169 lines (161 loc) · 5.37 KB
/
filterLockfileByImportersAndEngine.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
import { WANTED_LOCKFILE } from '@pnpm/constants'
import { LockfileMissingDependencyError } from '@pnpm/error'
import {
Lockfile,
PackageSnapshots,
} from '@pnpm/lockfile-types'
import { nameVerFromPkgSnapshot } from '@pnpm/lockfile-utils'
import pnpmLogger from '@pnpm/logger'
import packageIsInstallable from '@pnpm/package-is-installable'
import { DependenciesField } from '@pnpm/types'
import * as dp from 'dependency-path'
import unnest from 'ramda/src/unnest'
import filterImporter from './filterImporter'
const logger = pnpmLogger('lockfile')
export default function filterByImportersAndEngine (
lockfile: Lockfile,
importerIds: string[],
opts: {
currentEngine: {
nodeVersion: string
pnpmVersion: string
}
engineStrict: boolean
include: { [dependenciesField in DependenciesField]: boolean }
includeIncompatiblePackages?: boolean
failOnMissingDependencies: boolean
lockfileDir: string
skipped: Set<string>
}
): Lockfile {
const importerDeps = importerIds
.map((importerId) => lockfile.importers[importerId])
.map((importer) => ({
...(opts.include.dependencies ? importer.dependencies : {}),
...(opts.include.devDependencies ? importer.devDependencies : {}),
...(opts.include.optionalDependencies ? importer.optionalDependencies : {}),
}))
.map(Object.entries)
const directDepPaths = unnest(importerDeps)
.map(([pkgName, ref]) => dp.refToRelative(ref, pkgName))
.filter((nodeId) => nodeId !== null) as string[]
const packages = (lockfile.packages != null)
? pickPkgsWithAllDeps(lockfile.packages, directDepPaths, {
currentEngine: opts.currentEngine,
engineStrict: opts.engineStrict,
failOnMissingDependencies: opts.failOnMissingDependencies,
include: opts.include,
includeIncompatiblePackages: opts.includeIncompatiblePackages === true,
lockfileDir: opts.lockfileDir,
skipped: opts.skipped,
})
: {}
const importers = importerIds.reduce((acc, importerId) => {
acc[importerId] = filterImporter(lockfile.importers[importerId], opts.include)
if (acc[importerId].optionalDependencies != null) {
for (const depName of Object.keys(acc[importerId].optionalDependencies ?? {})) {
const depPath = dp.refToRelative(acc[importerId].optionalDependencies![depName], depName)
if (depPath && !packages[depPath]) {
delete acc[importerId].optionalDependencies![depName]
}
}
}
return acc
}, { ...lockfile.importers })
return {
...lockfile,
importers,
packages,
}
}
function pickPkgsWithAllDeps (
pkgSnapshots: PackageSnapshots,
depPaths: string[],
opts: {
currentEngine: {
nodeVersion: string
pnpmVersion: string
}
engineStrict: boolean
failOnMissingDependencies: boolean
include: { [dependenciesField in DependenciesField]: boolean }
includeIncompatiblePackages: boolean
lockfileDir: string
skipped: Set<string>
}
) {
const pickedPackages = {} as PackageSnapshots
pkgAllDeps({ pkgSnapshots, pickedPackages }, depPaths, true, opts)
return pickedPackages
}
function pkgAllDeps (
ctx: {
pkgSnapshots: PackageSnapshots
pickedPackages: PackageSnapshots
},
depPaths: string[],
parentIsInstallable: boolean,
opts: {
currentEngine: {
nodeVersion: string
pnpmVersion: string
}
engineStrict: boolean
failOnMissingDependencies: boolean
include: { [dependenciesField in DependenciesField]: boolean }
includeIncompatiblePackages: boolean
lockfileDir: string
skipped: Set<string>
}
) {
for (const depPath of depPaths) {
if (ctx.pickedPackages[depPath]) continue
const pkgSnapshot = ctx.pkgSnapshots[depPath]
if (!pkgSnapshot && !depPath.startsWith('link:')) {
if (opts.failOnMissingDependencies) {
throw new LockfileMissingDependencyError(depPath)
}
logger.debug(`No entry for "${depPath}" in ${WANTED_LOCKFILE}`)
continue
}
let installable!: boolean
if (!parentIsInstallable) {
installable = false
if (!ctx.pickedPackages[depPath] && pkgSnapshot.optional === true) {
opts.skipped.add(depPath)
}
} else {
const pkg = {
...nameVerFromPkgSnapshot(depPath, pkgSnapshot),
cpu: pkgSnapshot.cpu,
engines: pkgSnapshot.engines,
os: pkgSnapshot.os,
libc: pkgSnapshot.libc,
}
// TODO: depPath is not the package ID. Should be fixed
installable = opts.includeIncompatiblePackages || packageIsInstallable(pkgSnapshot.id ?? depPath, pkg, {
engineStrict: opts.engineStrict,
lockfileDir: opts.lockfileDir,
nodeVersion: opts.currentEngine.nodeVersion,
optional: pkgSnapshot.optional === true,
pnpmVersion: opts.currentEngine.pnpmVersion,
}) !== false
if (!installable) {
if (!ctx.pickedPackages[depPath] && pkgSnapshot.optional === true) {
opts.skipped.add(depPath)
}
} else {
opts.skipped.delete(depPath)
}
}
ctx.pickedPackages[depPath] = pkgSnapshot
const nextRelDepPaths = Object.entries(
{
...pkgSnapshot.dependencies,
...(opts.include.optionalDependencies ? pkgSnapshot.optionalDependencies : {}),
})
.map(([pkgName, ref]) => dp.refToRelative(ref, pkgName))
.filter((nodeId) => nodeId !== null) as string[]
pkgAllDeps(ctx, nextRelDepPaths, installable, opts)
}
}