/
installDeps.ts
297 lines (279 loc) · 9.54 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
import path from 'path'
import {
readProjectManifestOnly,
tryReadProjectManifest,
} from '@pnpm/cli-utils'
import { Config } from '@pnpm/config'
import PnpmError from '@pnpm/error'
import { filterPkgsBySelectorObjects } from '@pnpm/filter-workspace-packages'
import findWorkspacePackages, { arrayOfWorkspacePackagesToMap } from '@pnpm/find-workspace-packages'
import { rebuildProjects } from '@pnpm/plugin-commands-rebuild'
import { createOrConnectStoreController, CreateStoreControllerOptions } from '@pnpm/store-connection-manager'
import { IncludedDependencies, Project } from '@pnpm/types'
import {
install,
mutateModules,
MutateModulesOptions,
MutatedProject,
WorkspacePackages,
} from '@pnpm/core'
import logger from '@pnpm/logger'
import { sequenceGraph } from '@pnpm/sort-packages'
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 } 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'
| 'autoInstallPeers'
| 'bail'
| 'bin'
| 'cliOptions'
| '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'
> & CreateStoreControllerOptions & {
argv: {
original: string[]
}
allowNew?: boolean
frozenLockfileIfExists?: boolean
include?: IncludedDependencies
includeDirect?: IncludedDependencies
latest?: boolean
update?: boolean
updateMatching?: (pkgName: string) => boolean
updatePackageManifest?: boolean
useBetaCli?: boolean
recursive?: boolean
workspace?: boolean
} & Partial<Pick<Config, 'pnpmHomeDir'>>
export default async function handler (
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
}
}
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 (!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,
})
}
await recursive(allProjects,
params,
{
...opts,
forceHoistPattern,
forcePublicHoistPattern,
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)
}
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: MutateModulesOptions = {
...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 currentInput = [...params]
if (opts.update) {
const ignoredPackages = (manifest.pnpm?.updateConfig?.ignoreDependencies ?? [])
currentInput = [...ignoredPackages.map(pkg => `!${pkg}`), ...currentInput]
}
const updateMatch = opts.update && (currentInput.length > 0) ? createMatcher(currentInput) : null
if (updateMatch != null) {
currentInput = matchDependencies(updateMatch, manifest, includeDirect)
if (currentInput.length === 0 && 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 (!currentInput || (currentInput.length === 0)) {
currentInput = updateToLatestSpecsFromManifest(manifest, includeDirect)
} else {
currentInput = createLatestSpecs(currentInput, manifest)
}
}
if (opts.workspace) {
if (!currentInput || (currentInput.length === 0)) {
currentInput = updateToWorkspacePackagesFromManifest(manifest, includeDirect, workspacePackages)
} else {
currentInput = createWorkspaceSpecs(currentInput, workspacePackages)
}
}
if (currentInput?.length) {
const mutatedProject: MutatedProject = {
allowNew: opts.allowNew,
binsDir: opts.bin,
dependencySelectors: currentInput,
manifest,
mutation: 'installSome',
peer: opts.savePeer,
pinnedVersion: getPinnedVersion(opts),
rootDir: opts.dir,
targetDependenciesField: getSaveType(opts),
}
const [updatedImporter] = await mutateModules([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,
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 } }
}