-
-
Notifications
You must be signed in to change notification settings - Fork 938
/
remove.ts
209 lines (201 loc) · 6.46 KB
/
remove.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
import {
docsUrl,
readDepNameCompletions,
readProjectManifest,
} from '@pnpm/cli-utils'
import { CompletionFunc } from '@pnpm/command'
import { FILTERING, OPTIONS, UNIVERSAL_OPTIONS } from '@pnpm/common-cli-options-help'
import { Config, types as allTypes } from '@pnpm/config'
import PnpmError from '@pnpm/error'
import findWorkspacePackages, { arrayOfWorkspacePackagesToMap } from '@pnpm/find-workspace-packages'
import { getAllDependenciesFromManifest } from '@pnpm/manifest-utils'
import { createOrConnectStoreController, CreateStoreControllerOptions } from '@pnpm/store-connection-manager'
import { DependenciesField } from '@pnpm/types'
import {
mutateModules,
} from '@pnpm/core'
import pick from 'ramda/src/pick'
import without from 'ramda/src/without'
import renderHelp from 'render-help'
import getOptionsFromRootManifest from './getOptionsFromRootManifest'
import getSaveType from './getSaveType'
import recursive from './recursive'
class RemoveMissingDepsError extends PnpmError {
constructor (
opts: {
availableDependencies: string[]
nonMatchedDependencies: string[]
targetDependenciesField?: DependenciesField
}
) {
let message = 'Cannot remove '
message += `${opts.nonMatchedDependencies.map(dep => `'${dep}'`).join(', ')}: `
if (opts.availableDependencies.length > 0) {
message += `no such ${opts.nonMatchedDependencies.length > 1 ? 'dependencies' : 'dependency'} `
message += `found${opts.targetDependenciesField ? ` in '${opts.targetDependenciesField}'` : ''}`
const hint = `Available dependencies: ${opts.availableDependencies.join(', ')}`
super('CANNOT_REMOVE_MISSING_DEPS', message, { hint })
return
}
message += opts.targetDependenciesField
? `project has no '${opts.targetDependenciesField}'`
: 'project has no dependencies of any kind'
super('CANNOT_REMOVE_MISSING_DEPS', message)
}
}
export function rcOptionsTypes () {
return pick([
'cache-dir',
'global-dir',
'global-pnpmfile',
'global',
'lockfile-dir',
'lockfile-directory',
'lockfile-only',
'lockfile',
'node-linker',
'package-import-method',
'pnpmfile',
'reporter',
'save-dev',
'save-optional',
'save-prod',
'shared-workspace-lockfile',
'store',
'store-dir',
'strict-peer-dependencies',
'virtual-store-dir',
], allTypes)
}
export const cliOptionsTypes = () => ({
...rcOptionsTypes(),
...pick(['force'], allTypes),
recursive: Boolean,
})
export function help () {
return renderHelp({
aliases: ['rm', 'uninstall', 'un'],
description: 'Removes packages from `node_modules` and from the project\'s `package.json`.',
descriptionLists: [
{
title: 'Options',
list: [
{
description: 'Remove from every package found in subdirectories \
or from every workspace package, when executed inside a workspace. \
For options that may be used with `-r`, see "pnpm help recursive"',
name: '--recursive',
shortAlias: '-r',
},
{
description: 'Remove the dependency only from "devDependencies"',
name: '--save-dev',
shortAlias: '-D',
},
{
description: 'Remove the dependency only from "optionalDependencies"',
name: '--save-optional',
shortAlias: '-O',
},
{
description: 'Remove the dependency only from "dependencies"',
name: '--save-prod',
shortAlias: '-P',
},
OPTIONS.globalDir,
...UNIVERSAL_OPTIONS,
],
},
FILTERING,
],
url: docsUrl('remove'),
usages: ['pnpm remove <pkg>[@<version>]...'],
})
}
// Unlike npm, pnpm does not treat "r" as an alias of "remove".
// This way we avoid the confusion about whether "pnpm r" means remove, run, or recursive.
export const commandNames = ['remove', 'uninstall', 'rm', 'un', 'uni']
export const completion: CompletionFunc = async (cliOpts, params) => {
return readDepNameCompletions(cliOpts.dir as string)
}
export async function handler (
opts: CreateStoreControllerOptions & Pick<Config,
| 'allProjects'
| 'bail'
| 'bin'
| 'dev'
| 'engineStrict'
| 'globalPnpmfile'
| 'hooks'
| 'ignorePnpmfile'
| 'linkWorkspacePackages'
| 'lockfileDir'
| 'optional'
| 'pnpmfile'
| 'production'
| 'rawLocalConfig'
| 'registries'
| 'rootProjectManifest'
| 'saveDev'
| 'saveOptional'
| 'saveProd'
| 'selectedProjectsGraph'
| 'workspaceDir'
> & {
recursive?: boolean
},
params: string[]
) {
if (params.length === 0) throw new PnpmError('MUST_REMOVE_SOMETHING', 'At least one dependency name should be specified for removal')
const include = {
dependencies: opts.production !== false,
devDependencies: opts.dev !== false,
optionalDependencies: opts.optional !== false,
}
if (opts.recursive && (opts.allProjects != null) && (opts.selectedProjectsGraph != null) && opts.workspaceDir) {
await recursive(opts.allProjects, params, { ...opts, include, selectedProjectsGraph: opts.selectedProjectsGraph, workspaceDir: opts.workspaceDir }, 'remove')
return
}
const store = await createOrConnectStoreController(opts)
const removeOpts = Object.assign(opts, {
...getOptionsFromRootManifest(opts.rootProjectManifest ?? {}),
storeController: store.ctrl,
storeDir: store.dir,
include,
})
removeOpts['workspacePackages'] = opts.workspaceDir
? arrayOfWorkspacePackagesToMap(await findWorkspacePackages(opts.workspaceDir, opts))
: undefined
const targetDependenciesField = getSaveType(opts)
const {
manifest: currentManifest,
writeProjectManifest,
} = await readProjectManifest(opts.dir, opts)
const availableDependencies = Object.keys(
targetDependenciesField === undefined
? getAllDependenciesFromManifest(currentManifest)
: currentManifest[targetDependenciesField] ?? {}
)
const nonMatchedDependencies = without(availableDependencies, params)
if (nonMatchedDependencies.length !== 0) {
throw new RemoveMissingDepsError({
availableDependencies,
nonMatchedDependencies,
targetDependenciesField,
})
}
const [mutationResult] = await mutateModules(
[
{
binsDir: opts.bin,
dependencyNames: params,
manifest: currentManifest,
mutation: 'uninstallSome',
rootDir: opts.dir,
targetDependenciesField,
},
],
removeOpts
)
await writeProjectManifest(mutationResult.manifest)
}