/
index.ts
220 lines (208 loc) · 7.04 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
import { ENGINE_NAME } from '@pnpm/constants'
import { skippedOptionalDependencyLogger } from '@pnpm/core-loggers'
import { runPostinstallHooks } from '@pnpm/lifecycle'
import linkBins, { linkBinsOfPackages } from '@pnpm/link-bins'
import logger from '@pnpm/logger'
import { fromDir as readPackageFromDir } from '@pnpm/read-package-json'
import { StoreController } from '@pnpm/store-controller-types'
import { DependencyManifest, PackageManifest } from '@pnpm/types'
import graphSequencer = require('graph-sequencer')
import path = require('path')
import R = require('ramda')
import runGroups from 'run-groups'
export default async (
depGraph: DependenciesGraph,
rootDepPaths: string[],
opts: {
childConcurrency?: number,
depsToBuild?: Set<string>,
extraBinPaths?: string[],
lockfileDir: string,
optional: boolean,
rawConfig: object,
unsafePerm: boolean,
userAgent: string,
sideEffectsCacheWrite: boolean,
storeController: StoreController,
rootNodeModulesDir: string,
},
) => {
const warn = (message: string) => logger.warn({ message, prefix: opts.lockfileDir })
// postinstall hooks
const nodesToBuild = new Set<string>()
getSubgraphToBuild(depGraph, rootDepPaths, nodesToBuild, new Set<string>())
const onlyFromBuildGraph = R.filter((depPath: string) => nodesToBuild.has(depPath))
const nodesToBuildArray = Array.from(nodesToBuild)
const graph = new Map(
nodesToBuildArray
.map((depPath) => [depPath, onlyFromBuildGraph(R.values(depGraph[depPath].children))]) as Array<[string, string[]]>,
)
const graphSequencerResult = graphSequencer({
graph,
groups: [nodesToBuildArray],
})
const chunks = graphSequencerResult.chunks as string[][]
const buildDepOpts = { ...opts, warn }
const groups = chunks.map((chunk) => {
chunk = chunk.filter((depPath) => depGraph[depPath].requiresBuild && !depGraph[depPath].isBuilt)
if (opts.depsToBuild) {
chunk = chunk.filter((depPath) => opts.depsToBuild!.has(depPath))
}
return chunk.map((depPath: string) =>
async () => buildDependency(depPath, depGraph, buildDepOpts)
)
})
await runGroups(opts.childConcurrency || 4, groups)
}
async function buildDependency (
depPath: string,
depGraph: DependenciesGraph,
opts: {
extraBinPaths?: string[],
lockfileDir: string,
optional: boolean,
rawConfig: object,
rootNodeModulesDir: string,
sideEffectsCacheWrite: boolean,
storeController: StoreController,
unsafePerm: boolean,
warn: (message: string) => void,
}
) {
const depNode = depGraph[depPath]
try {
await linkBinsOfDependencies(depNode, depGraph, opts)
const hasSideEffects = await runPostinstallHooks({
depPath,
extraBinPaths: opts.extraBinPaths,
optional: depNode.optional,
pkgRoot: depNode.peripheralLocation,
prepare: depNode.prepare,
rawConfig: opts.rawConfig,
rootNodeModulesDir: opts.rootNodeModulesDir,
unsafePerm: opts.unsafePerm || false,
})
if (hasSideEffects && opts.sideEffectsCacheWrite) {
try {
await opts.storeController.upload(depNode.peripheralLocation, {
engine: ENGINE_NAME,
packageId: depNode.packageId,
})
} catch (err) {
if (err.statusCode === 403) {
logger.warn({
message: `The store server disabled upload requests, could not upload ${depNode.packageId}`,
prefix: opts.lockfileDir,
})
} else {
logger.warn({
error: err,
message: `An error occurred while uploading ${depNode.packageId}`,
prefix: opts.lockfileDir,
})
}
}
}
} catch (err) {
if (depNode.optional) {
// TODO: add parents field to the log
const pkg = await readPackageFromDir(path.join(depNode.peripheralLocation)) as DependencyManifest
skippedOptionalDependencyLogger.debug({
details: err.toString(),
package: {
id: depNode.packageId,
name: pkg.name,
version: pkg.version,
},
prefix: opts.lockfileDir,
reason: 'build_failure',
})
return
}
throw err
}
}
function getSubgraphToBuild (
graph: DependenciesGraph,
entryNodes: string[],
nodesToBuild: Set<string>,
walked: Set<string>,
) {
let currentShouldBeBuilt = false
for (const depPath of entryNodes) {
if (!graph[depPath]) return // packages that are already in node_modules are skipped
if (nodesToBuild.has(depPath)) {
currentShouldBeBuilt = true
}
if (walked.has(depPath)) continue
walked.add(depPath)
const childShouldBeBuilt = getSubgraphToBuild(graph, R.values(graph[depPath].children), nodesToBuild, walked)
|| graph[depPath].requiresBuild
if (childShouldBeBuilt) {
nodesToBuild.add(depPath)
currentShouldBeBuilt = true
}
}
return currentShouldBeBuilt
}
export interface DependenciesGraphNode {
fetchingBundledManifest?: () => Promise<PackageManifest>,
hasBundledDependencies: boolean,
peripheralLocation: string,
children: {[alias: string]: string},
optional: boolean,
optionalDependencies: Set<string>,
packageId: string, // TODO: this option is currently only needed when running postinstall scripts but even there it should be not used
installable?: boolean,
isBuilt?: boolean,
requiresBuild?: boolean,
prepare: boolean,
hasBin: boolean,
}
export interface DependenciesGraph {
[depPath: string]: DependenciesGraphNode
}
export async function linkBinsOfDependencies (
depNode: DependenciesGraphNode,
depGraph: DependenciesGraph,
opts: {
optional: boolean,
warn: (message: string) => void,
},
) {
const childrenToLink = opts.optional
? depNode.children
: Object.keys(depNode.children)
.reduce((nonOptionalChildren, childAlias) => {
if (!depNode.optionalDependencies.has(childAlias)) {
nonOptionalChildren[childAlias] = depNode.children[childAlias]
}
return nonOptionalChildren
}, {})
const binPath = path.join(depNode.peripheralLocation, 'node_modules', '.bin')
const pkgs = await Promise.all(
Object.keys(childrenToLink)
.filter((alias) => {
const dep = depGraph[childrenToLink[alias]]
if (!dep) {
// TODO: Try to reproduce this issue with a test in supi
logger.debug({ message: `Failed to link bins of "${alias}" to "${binPath}". This is probably not an issue.` })
return false
}
return dep.hasBin && dep.installable !== false
})
.map(async (alias) => {
const dep = depGraph[childrenToLink[alias]]
return {
location: dep.peripheralLocation,
manifest: await dep.fetchingBundledManifest?.() || (await readPackageFromDir(dep.peripheralLocation) as DependencyManifest),
}
}),
)
await linkBinsOfPackages(pkgs, binPath, { warn: opts.warn })
// link also the bundled dependencies` bins
if (depNode.hasBundledDependencies) {
const bundledModules = path.join(depNode.peripheralLocation, 'node_modules')
await linkBins(bundledModules, binPath, { warn: opts.warn })
}
}