-
-
Notifications
You must be signed in to change notification settings - Fork 932
/
lockfileToDepGraph.ts
259 lines (247 loc) · 8.98 KB
/
lockfileToDepGraph.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
import path from 'path'
import { WANTED_LOCKFILE } from '@pnpm/constants'
import {
progressLogger,
} from '@pnpm/core-loggers'
import {
Lockfile,
PackageSnapshot,
} from '@pnpm/lockfile-file'
import {
nameVerFromPkgSnapshot,
packageIdFromSnapshot,
pkgSnapshotToResolution,
} from '@pnpm/lockfile-utils'
import { logger } from '@pnpm/logger'
import { IncludedDependencies } from '@pnpm/modules-yaml'
import { packageIsInstallable } from '@pnpm/package-is-installable'
import { PatchFile, Registries } from '@pnpm/types'
import {
FetchPackageToStoreFunction,
PackageFilesResponse,
StoreController,
} from '@pnpm/store-controller-types'
import * as dp from '@pnpm/dependency-path'
import pathExists from 'path-exists'
import equals from 'ramda/src/equals'
const brokenModulesLogger = logger('_broken_node_modules')
export interface DependenciesGraphNode {
alias?: string // this is populated in HoistedDepGraphOnly
hasBundledDependencies: boolean
modules: string
name: string
fetchingFiles: () => Promise<PackageFilesResponse>
finishing: () => Promise<void>
dir: string
children: Record<string, string>
optionalDependencies: Set<string>
optional: boolean
depPath: string // this option is only needed for saving pendingBuild when running with --ignore-scripts flag
isBuilt?: boolean
requiresBuild: boolean
prepare: boolean
hasBin: boolean
filesIndexFile: string
patchFile?: PatchFile
}
export interface DependenciesGraph {
[depPath: string]: DependenciesGraphNode
}
export interface LockfileToDepGraphOptions {
engineStrict: boolean
force: boolean
importerIds: string[]
include: IncludedDependencies
lockfileDir: string
nodeVersion: string
pnpmVersion: string
patchedDependencies?: Record<string, PatchFile>
registries: Registries
sideEffectsCacheRead: boolean
skipped: Set<string>
storeController: StoreController
storeDir: string
virtualStoreDir: string
}
export interface DirectDependenciesByImporterId {
[importerId: string]: { [alias: string]: string }
}
export interface DepHierarchy {
[depPath: string]: Record<string, DepHierarchy>
}
export interface LockfileToDepGraphResult {
directDependenciesByImporterId: DirectDependenciesByImporterId
graph: DependenciesGraph
hierarchy?: DepHierarchy
hoistedLocations?: Record<string, string[]>
symlinkedDirectDependenciesByImporterId?: DirectDependenciesByImporterId
prevGraph?: DependenciesGraph
pkgLocationsByDepPath?: Record<string, string[]>
}
export async function lockfileToDepGraph (
lockfile: Lockfile,
currentLockfile: Lockfile | null,
opts: LockfileToDepGraphOptions
): Promise<LockfileToDepGraphResult> {
const currentPackages = currentLockfile?.packages ?? {}
const graph: DependenciesGraph = {}
const directDependenciesByImporterId: DirectDependenciesByImporterId = {}
if (lockfile.packages != null) {
const pkgSnapshotByLocation = {}
await Promise.all(
Object.entries(lockfile.packages).map(async ([depPath, pkgSnapshot]) => {
if (opts.skipped.has(depPath)) return
// TODO: optimize. This info can be already returned by pkgSnapshotToResolution()
const { name: pkgName, version: pkgVersion } = nameVerFromPkgSnapshot(depPath, pkgSnapshot)
const modules = path.join(opts.virtualStoreDir, dp.depPathToFilename(depPath), 'node_modules')
const packageId = packageIdFromSnapshot(depPath, pkgSnapshot, opts.registries)
const pkg = {
name: pkgName,
version: pkgVersion,
engines: pkgSnapshot.engines,
cpu: pkgSnapshot.cpu,
os: pkgSnapshot.os,
libc: pkgSnapshot.libc,
}
if (!opts.force &&
packageIsInstallable(packageId, pkg, {
engineStrict: opts.engineStrict,
lockfileDir: opts.lockfileDir,
nodeVersion: opts.nodeVersion,
optional: pkgSnapshot.optional === true,
pnpmVersion: opts.pnpmVersion,
}) === false
) {
opts.skipped.add(depPath)
return
}
const dir = path.join(modules, pkgName)
if (
currentPackages[depPath] && equals(currentPackages[depPath].dependencies, lockfile.packages![depPath].dependencies) &&
equals(currentPackages[depPath].optionalDependencies, lockfile.packages![depPath].optionalDependencies)
) {
if (await pathExists(dir)) {
return
}
brokenModulesLogger.debug({
missing: dir,
})
}
const resolution = pkgSnapshotToResolution(depPath, pkgSnapshot, opts.registries)
progressLogger.debug({
packageId,
requester: opts.lockfileDir,
status: 'resolved',
})
let fetchResponse!: ReturnType<FetchPackageToStoreFunction>
try {
fetchResponse = opts.storeController.fetchPackage({
force: false,
lockfileDir: opts.lockfileDir,
pkg: {
id: packageId,
resolution,
},
expectedPkg: {
name: pkgName,
version: pkgVersion,
},
})
if (fetchResponse instanceof Promise) fetchResponse = await fetchResponse
} catch (err: any) { // eslint-disable-line
if (pkgSnapshot.optional) return
throw err
}
graph[dir] = {
children: {},
depPath,
dir,
fetchingFiles: fetchResponse.files,
filesIndexFile: fetchResponse.filesIndexFile,
finishing: fetchResponse.finishing,
hasBin: pkgSnapshot.hasBin === true,
hasBundledDependencies: pkgSnapshot.bundledDependencies != null,
modules,
name: pkgName,
optional: !!pkgSnapshot.optional,
optionalDependencies: new Set(Object.keys(pkgSnapshot.optionalDependencies ?? {})),
prepare: pkgSnapshot.prepare === true,
requiresBuild: pkgSnapshot.requiresBuild === true,
patchFile: opts.patchedDependencies?.[`${pkgName}@${pkgVersion}`],
}
pkgSnapshotByLocation[dir] = pkgSnapshot
})
)
const ctx = {
force: opts.force,
graph,
lockfileDir: opts.lockfileDir,
pkgSnapshotsByDepPaths: lockfile.packages,
registries: opts.registries,
sideEffectsCacheRead: opts.sideEffectsCacheRead,
skipped: opts.skipped,
storeController: opts.storeController,
storeDir: opts.storeDir,
virtualStoreDir: opts.virtualStoreDir,
}
for (const [dir, node] of Object.entries(graph)) {
const pkgSnapshot = pkgSnapshotByLocation[dir]
const allDeps = {
...pkgSnapshot.dependencies,
...(opts.include.optionalDependencies ? pkgSnapshot.optionalDependencies : {}),
}
const peerDeps = pkgSnapshot.peerDependencies ? new Set(Object.keys(pkgSnapshot.peerDependencies)) : null
node.children = getChildrenPaths(ctx, allDeps, peerDeps, '.')
}
for (const importerId of opts.importerIds) {
const projectSnapshot = lockfile.importers[importerId]
const rootDeps = {
...(opts.include.devDependencies ? projectSnapshot.devDependencies : {}),
...(opts.include.dependencies ? projectSnapshot.dependencies : {}),
...(opts.include.optionalDependencies ? projectSnapshot.optionalDependencies : {}),
}
directDependenciesByImporterId[importerId] = getChildrenPaths(ctx, rootDeps, null, importerId)
}
}
return { graph, directDependenciesByImporterId }
}
function getChildrenPaths (
ctx: {
graph: DependenciesGraph
force: boolean
registries: Registries
virtualStoreDir: string
storeDir: string
skipped: Set<string>
pkgSnapshotsByDepPaths: Record<string, PackageSnapshot>
lockfileDir: string
sideEffectsCacheRead: boolean
storeController: StoreController
},
allDeps: { [alias: string]: string },
peerDeps: Set<string> | null,
importerId: string
) {
const children: { [alias: string]: string } = {}
for (const [alias, ref] of Object.entries(allDeps)) {
const childDepPath = dp.refToAbsolute(ref, alias, ctx.registries)
if (childDepPath === null) {
children[alias] = path.resolve(ctx.lockfileDir, importerId, ref.slice(5))
continue
}
const childRelDepPath = dp.refToRelative(ref, alias) as string
const childPkgSnapshot = ctx.pkgSnapshotsByDepPaths[childRelDepPath]
if (ctx.graph[childRelDepPath]) {
children[alias] = ctx.graph[childRelDepPath].dir
} else if (childPkgSnapshot) {
if (ctx.skipped.has(childRelDepPath)) continue
const pkgName = nameVerFromPkgSnapshot(childRelDepPath, childPkgSnapshot).name
children[alias] = path.join(ctx.virtualStoreDir, dp.depPathToFilename(childRelDepPath), 'node_modules', pkgName)
} else if (ref.indexOf('file:') === 0) {
children[alias] = path.resolve(ctx.lockfileDir, ref.slice(5))
} else if (!ctx.skipped.has(childRelDepPath) && ((peerDeps == null) || !peerDeps.has(alias))) {
throw new Error(`${childRelDepPath} not found in ${WANTED_LOCKFILE}`)
}
}
return children
}