/
installDeps.ts
338 lines (323 loc) · 11.1 KB
/
installDeps.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
import path from 'path'
import {
readProjectManifestOnly,
tryReadProjectManifest,
} from '@pnpm/cli-utils'
import { type Config } from '@pnpm/config'
import { PnpmError } from '@pnpm/error'
import { filterPkgsBySelectorObjects } from '@pnpm/filter-workspace-packages'
import { arrayOfWorkspacePackagesToMap, findWorkspacePackages } from '@pnpm/find-workspace-packages'
import { type Lockfile } from '@pnpm/lockfile-types'
import { rebuildProjects } from '@pnpm/plugin-commands-rebuild'
import { createOrConnectStoreController, type CreateStoreControllerOptions } from '@pnpm/store-connection-manager'
import { type IncludedDependencies, type Project, type ProjectsGraph } from '@pnpm/types'
import {
install,
mutateModulesInSingleProject,
type MutateModulesOptions,
type WorkspacePackages,
} from '@pnpm/core'
import { logger } from '@pnpm/logger'
import { sequenceGraph } from '@pnpm/sort-packages'
import { createPkgGraph } from '@pnpm/workspace.pkgs-graph'
import isSubdir from 'is-subdir'
import { getOptionsFromRootManifest } from './getOptionsFromRootManifest'
import { getPinnedVersion } from './getPinnedVersion'
import { getSaveType } from './getSaveType'
import { getNodeExecPath } from './nodeExecPath'
import { recursive, createMatcher, matchDependencies, makeIgnorePatterns, type UpdateDepsMatcher } from './recursive'
import { updateToLatestSpecsFromManifest, createLatestSpecs } from './updateToLatestSpecsFromManifest'
import { createWorkspaceSpecs, updateToWorkspacePackagesFromManifest } from './updateWorkspaceDependencies'
const OVERWRITE_UPDATE_OPTIONS = {
allowNew: true,
update: false,
}
export type InstallDepsOptions = Pick<Config,
| 'allProjects'
| 'allProjectsGraph'
| 'autoInstallPeers'
| 'bail'
| 'bin'
| 'cliOptions'
| 'dedupePeerDependents'
| 'depth'
| 'dev'
| 'engineStrict'
| 'global'
| 'globalPnpmfile'
| 'hooks'
| 'ignorePnpmfile'
| 'ignoreScripts'
| 'linkWorkspacePackages'
| 'lockfileDir'
| 'lockfileOnly'
| 'pnpmfile'
| 'production'
| 'rawLocalConfig'
| 'registries'
| 'save'
| 'saveDev'
| 'saveExact'
| 'saveOptional'
| 'savePeer'
| 'savePrefix'
| 'saveProd'
| 'saveWorkspaceProtocol'
| 'lockfileIncludeTarballUrl'
| 'scriptsPrependNodePath'
| 'scriptShell'
| 'selectedProjectsGraph'
| 'sideEffectsCache'
| 'sideEffectsCacheReadonly'
| 'sort'
| 'sharedWorkspaceLockfile'
| 'shellEmulator'
| 'tag'
| 'optional'
| 'workspaceConcurrency'
| 'workspaceDir'
| 'extraEnv'
| 'ignoreWorkspaceCycles'
> & CreateStoreControllerOptions & {
argv: {
original: string[]
}
allowNew?: boolean
forceFullResolution?: boolean
frozenLockfileIfExists?: boolean
include?: IncludedDependencies
includeDirect?: IncludedDependencies
latest?: boolean
/**
* If specified, the installation will only be performed for comparison of the
* wanted lockfile. The wanted lockfile will not be updated on disk and no
* modules will be linked.
*
* The given callback is passed the wanted lockfile before installation and
* after. This allows functions to reasonably determine whether the wanted
* lockfile will change on disk after installation. The lockfile arguments
* passed to this callback should not be mutated.
*/
lockfileCheck?: (prev: Lockfile, next: Lockfile) => void
update?: boolean
updateMatching?: (pkgName: string) => boolean
updatePackageManifest?: boolean
useBetaCli?: boolean
recursive?: boolean
dedupe?: boolean
workspace?: boolean
includeOnlyPackageFiles?: boolean
} & Partial<Pick<Config, 'pnpmHomeDir'>>
export async function installDeps (
opts: InstallDepsOptions,
params: string[]
) {
if (opts.workspace) {
if (opts.latest) {
throw new PnpmError('BAD_OPTIONS', 'Cannot use --latest with --workspace simultaneously')
}
if (!opts.workspaceDir) {
throw new PnpmError('WORKSPACE_OPTION_OUTSIDE_WORKSPACE', '--workspace can only be used inside a workspace')
}
if (!opts.linkWorkspacePackages && !opts.saveWorkspaceProtocol) {
if (opts.rawLocalConfig['save-workspace-protocol'] === false) {
throw new PnpmError('BAD_OPTIONS', 'This workspace has link-workspace-packages turned off, \
so dependencies are linked from the workspace only when the workspace protocol is used. \
Either set link-workspace-packages to true or don\'t use the --no-save-workspace-protocol option \
when running add/update with the --workspace option')
} else {
opts.saveWorkspaceProtocol = true
}
}
// @ts-expect-error
opts['preserveWorkspaceProtocol'] = !opts.linkWorkspacePackages
}
const includeDirect = opts.includeDirect ?? {
dependencies: true,
devDependencies: true,
optionalDependencies: true,
}
const forceHoistPattern = typeof opts.rawLocalConfig['hoist-pattern'] !== 'undefined' ||
typeof opts.rawLocalConfig['hoist'] !== 'undefined'
const forcePublicHoistPattern = typeof opts.rawLocalConfig['shamefully-hoist'] !== 'undefined' ||
typeof opts.rawLocalConfig['public-hoist-pattern'] !== 'undefined'
const allProjects = opts.allProjects ?? (
opts.workspaceDir ? await findWorkspacePackages(opts.workspaceDir, opts) : []
)
if (opts.workspaceDir) {
const selectedProjectsGraph = opts.selectedProjectsGraph ?? selectProjectByDir(allProjects, opts.dir)
if (selectedProjectsGraph != null) {
const sequencedGraph = sequenceGraph(selectedProjectsGraph)
// Check and warn if there are cyclic dependencies
if (!opts.ignoreWorkspaceCycles && !sequencedGraph.safe) {
const cyclicDependenciesInfo = sequencedGraph.cycles.length > 0
? `: ${sequencedGraph.cycles.map(deps => deps.join(', ')).join('; ')}`
: ''
logger.warn({
message: `There are cyclic workspace dependencies${cyclicDependenciesInfo}`,
prefix: opts.workspaceDir,
})
}
let allProjectsGraph!: ProjectsGraph
if (opts.dedupePeerDependents) {
allProjectsGraph = opts.allProjectsGraph ?? createPkgGraph(allProjects, {
linkWorkspacePackages: Boolean(opts.linkWorkspacePackages),
}).graph
} else {
allProjectsGraph = selectedProjectsGraph
if (!allProjectsGraph[opts.workspaceDir]) {
allProjectsGraph = {
...allProjectsGraph,
...selectProjectByDir(allProjects, opts.workspaceDir),
}
}
}
await recursive(allProjects,
params,
{
...opts,
forceHoistPattern,
forcePublicHoistPattern,
allProjectsGraph,
selectedProjectsGraph,
workspaceDir: opts.workspaceDir,
},
opts.update ? 'update' : (params.length === 0 ? 'install' : 'add')
)
return
}
}
// `pnpm install ""` is going to be just `pnpm install`
params = params.filter(Boolean)
const dir = opts.dir || process.cwd()
let workspacePackages!: WorkspacePackages
if (opts.workspaceDir) {
workspacePackages = arrayOfWorkspacePackagesToMap(allProjects) as WorkspacePackages
}
let { manifest, writeProjectManifest } = await tryReadProjectManifest(opts.dir, opts)
if (manifest === null) {
if (opts.update === true || params.length === 0) {
throw new PnpmError('NO_PKG_MANIFEST', `No package.json found in ${opts.dir}`)
}
manifest = {}
}
const store = await createOrConnectStoreController(opts)
const installOpts: Omit<MutateModulesOptions, 'allProjects'> = {
...opts,
...getOptionsFromRootManifest(manifest),
forceHoistPattern,
forcePublicHoistPattern,
// In case installation is done in a multi-package repository
// The dependencies should be built first,
// so ignoring scripts for now
ignoreScripts: !!workspacePackages || opts.ignoreScripts,
linkWorkspacePackagesDepth: opts.linkWorkspacePackages === 'deep' ? Infinity : opts.linkWorkspacePackages ? 0 : -1,
sideEffectsCacheRead: opts.sideEffectsCache ?? opts.sideEffectsCacheReadonly,
sideEffectsCacheWrite: opts.sideEffectsCache,
storeController: store.ctrl,
storeDir: store.dir,
workspacePackages,
}
if (opts.global && opts.pnpmHomeDir != null) {
const nodeExecPath = await getNodeExecPath()
if (isSubdir(opts.pnpmHomeDir, nodeExecPath)) {
installOpts['nodeExecPath'] = nodeExecPath
}
}
let updateMatch: UpdateDepsMatcher | null
if (opts.update) {
if (params.length === 0) {
const ignoreDeps = manifest.pnpm?.updateConfig?.ignoreDependencies
if (ignoreDeps?.length) {
params = makeIgnorePatterns(ignoreDeps)
}
}
updateMatch = params.length ? createMatcher(params) : null
} else {
updateMatch = null
}
if (updateMatch != null) {
params = matchDependencies(updateMatch, manifest, includeDirect)
if (params.length === 0) {
if (opts.latest) return
if (opts.depth === 0) {
throw new PnpmError('NO_PACKAGE_IN_DEPENDENCIES',
'None of the specified packages were found in the dependencies.')
}
}
}
if (opts.update && opts.latest) {
if (!params || (params.length === 0)) {
params = updateToLatestSpecsFromManifest(manifest, includeDirect)
} else {
params = createLatestSpecs(params, manifest)
}
}
if (opts.workspace) {
if (!params || (params.length === 0)) {
params = updateToWorkspacePackagesFromManifest(manifest, includeDirect, workspacePackages)
} else {
params = createWorkspaceSpecs(params, workspacePackages)
}
}
if (params?.length) {
const mutatedProject = {
allowNew: opts.allowNew,
binsDir: opts.bin,
dependencySelectors: params,
manifest,
mutation: 'installSome' as const,
peer: opts.savePeer,
pinnedVersion: getPinnedVersion(opts),
rootDir: opts.dir,
targetDependenciesField: getSaveType(opts),
}
const updatedImporter = await mutateModulesInSingleProject(mutatedProject, installOpts)
if (opts.save !== false) {
await writeProjectManifest(updatedImporter.manifest)
}
return
}
const updatedManifest = await install(manifest, installOpts)
if (opts.update === true && opts.save !== false) {
await writeProjectManifest(updatedManifest)
}
if (opts.linkWorkspacePackages && opts.workspaceDir) {
const { selectedProjectsGraph } = await filterPkgsBySelectorObjects(allProjects, [
{
excludeSelf: true,
includeDependencies: true,
parentDir: dir,
},
], {
workspaceDir: opts.workspaceDir,
})
await recursive(allProjects, [], {
...opts,
...OVERWRITE_UPDATE_OPTIONS,
allProjectsGraph: opts.allProjectsGraph!,
selectedProjectsGraph,
workspaceDir: opts.workspaceDir, // Otherwise TypeScript doesn't understand that is not undefined
}, 'install')
if (opts.ignoreScripts) return
await rebuildProjects(
[
{
buildIndex: 0,
manifest: await readProjectManifestOnly(opts.dir, opts),
rootDir: opts.dir,
},
], {
...opts,
pending: true,
storeController: store.ctrl,
storeDir: store.dir,
}
)
}
}
function selectProjectByDir (projects: Project[], searchedDir: string) {
const project = projects.find(({ dir }) => path.relative(dir, searchedDir) === '')
if (project == null) return undefined
return { [searchedDir]: { dependencies: [], package: project } }
}