/
outdated.ts
315 lines (286 loc) · 8.4 KB
/
outdated.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
import {
docsUrl,
readDepNameCompletions,
readProjectManifestOnly,
TABLE_OPTIONS,
} from '@pnpm/cli-utils'
import colorizeSemverDiff from '@pnpm/colorize-semver-diff'
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 {
outdatedDepsOfProjects,
OutdatedPackage,
} from '@pnpm/outdated'
import semverDiff from '@pnpm/semver-diff'
import { table } from '@zkochan/table'
import chalk from 'chalk'
import pick from 'ramda/src/pick'
import sortWith from 'ramda/src/sortWith'
import renderHelp from 'render-help'
import stripAnsi from 'strip-ansi'
import wrapAnsi from 'wrap-ansi'
import {
DEFAULT_COMPARATORS,
OutdatedWithVersionDiff,
} from './utils'
import outdatedRecursive from './recursive'
export function rcOptionsTypes () {
return {
...pick([
'depth',
'dev',
'global-dir',
'global',
'long',
'optional',
'production',
], allTypes),
compatible: Boolean,
table: Boolean,
}
}
export const cliOptionsTypes = () => ({
...rcOptionsTypes(),
recursive: Boolean,
})
export const shorthands = {
D: '--dev',
P: '--production',
}
export const commandNames = ['outdated']
export function help () {
return renderHelp({
description: `Check for outdated packages. The check can be limited to a subset of the installed packages by providing arguments (patterns are supported).
Examples:
pnpm outdated
pnpm outdated --long
pnpm outdated gulp-* @babel/core`,
descriptionLists: [
{
title: 'Options',
list: [
{
description: 'Print only versions that satisfy specs in package.json',
name: '--compatible',
},
{
description: 'By default, details about the outdated packages (such as a link to the repo) are not displayed. \
To display the details, pass this option.',
name: '--long',
},
{
description: 'Check for outdated dependencies in every package found in subdirectories \
or in 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: 'Prints the outdated packages in a list. Good for small consoles',
name: '--no-table',
},
{
description: 'Check only "dependencies" and "optionalDependencies"',
name: '--prod',
shortAlias: '-P',
},
{
description: 'Check only "devDependencies"',
name: '--dev',
shortAlias: '-D',
},
{
description: 'Don\'t check "optionalDependencies"',
name: '--no-optional',
},
OPTIONS.globalDir,
...UNIVERSAL_OPTIONS,
],
},
FILTERING,
],
url: docsUrl('outdated'),
usages: ['pnpm outdated [<pkg> ...]'],
})
}
export const completion: CompletionFunc = async (cliOpts) => {
return readDepNameCompletions(cliOpts.dir as string)
}
export type OutdatedCommandOptions = {
compatible?: boolean
long?: boolean
recursive?: boolean
table?: boolean
} & Pick<Config,
| 'allProjects'
| 'alwaysAuth'
| 'ca'
| 'cacheDir'
| 'cert'
| 'dev'
| 'dir'
| 'engineStrict'
| 'fetchRetries'
| 'fetchRetryFactor'
| 'fetchRetryMaxtimeout'
| 'fetchRetryMintimeout'
| 'fetchTimeout'
| 'global'
| 'httpProxy'
| 'httpsProxy'
| 'key'
| 'localAddress'
| 'lockfileDir'
| 'networkConcurrency'
| 'noProxy'
| 'offline'
| 'optional'
| 'production'
| 'rawConfig'
| 'registries'
| 'selectedProjectsGraph'
| 'strictSsl'
| 'tag'
| 'userAgent'
> & Partial<Pick<Config, 'userConfig'>>
export async function handler (
opts: OutdatedCommandOptions,
params: string[] = []
) {
const include = {
dependencies: opts.production !== false,
devDependencies: opts.dev !== false,
optionalDependencies: opts.optional !== false,
}
if (opts.recursive && (opts.selectedProjectsGraph != null)) {
const pkgs = Object.values(opts.selectedProjectsGraph).map((wsPkg) => wsPkg.package)
return outdatedRecursive(pkgs, params, { ...opts, include })
}
const manifest = await readProjectManifestOnly(opts.dir, opts)
const packages = [
{
dir: opts.dir,
manifest,
},
]
const [outdatedPackages] = await outdatedDepsOfProjects(packages, params, {
...opts,
fullMetadata: opts.long,
ignoreDependencies: new Set(manifest?.pnpm?.updateConfig?.ignoreDependencies ?? []),
include,
retry: {
factor: opts.fetchRetryFactor,
maxTimeout: opts.fetchRetryMaxtimeout,
minTimeout: opts.fetchRetryMintimeout,
retries: opts.fetchRetries,
},
timeout: opts.fetchTimeout,
})
if (outdatedPackages.length === 0) return { output: '', exitCode: 0 }
if (opts.table !== false) {
return { output: renderOutdatedTable(outdatedPackages, opts), exitCode: 1 }
} else {
return { output: renderOutdatedList(outdatedPackages, opts), exitCode: 1 }
}
}
function renderOutdatedTable (outdatedPackages: readonly OutdatedPackage[], opts: { long?: boolean }) {
const columnNames = [
'Package',
'Current',
'Latest',
]
const columnFns = [
renderPackageName,
renderCurrent,
renderLatest,
]
if (opts.long) {
columnNames.push('Details')
columnFns.push(renderDetails)
}
// Avoid the overhead of allocating a new array caused by calling `array.map()`
for (let i = 0; i < columnNames.length; i++)
columnNames[i] = chalk.blueBright(columnNames[i])
return table([
columnNames,
...sortOutdatedPackages(outdatedPackages)
.map((outdatedPkg) => columnFns.map((fn) => fn(outdatedPkg))),
], TABLE_OPTIONS)
}
function renderOutdatedList (outdatedPackages: readonly OutdatedPackage[], opts: { long?: boolean }) {
return sortOutdatedPackages(outdatedPackages)
.map((outdatedPkg) => {
let info = `${chalk.bold(renderPackageName(outdatedPkg))}
${renderCurrent(outdatedPkg)} ${chalk.grey('=>')} ${renderLatest(outdatedPkg)}`
if (opts.long) {
const details = renderDetails(outdatedPkg)
if (details) {
info += `\n${details}`
}
}
return info
})
.join('\n\n') + '\n'
}
function sortOutdatedPackages (outdatedPackages: readonly OutdatedPackage[]) {
return sortWith(
DEFAULT_COMPARATORS,
outdatedPackages.map(toOutdatedWithVersionDiff)
)
}
export function getCellWidth (data: string[][], columnNumber: number, maxWidth: number) {
const maxCellWidth = data.reduce((cellWidth, row) => {
const cellLines = stripAnsi(row[columnNumber]).split('\n')
const currentCellWidth = cellLines.reduce((lineWidth, line) => {
return Math.max(lineWidth, line.length)
}, 0)
return Math.max(cellWidth, currentCellWidth)
}, 0)
return Math.min(maxWidth, maxCellWidth)
}
export function toOutdatedWithVersionDiff<T> (outdated: T & OutdatedPackage): T & OutdatedWithVersionDiff {
if (outdated.latestManifest != null) {
return {
...outdated,
...semverDiff(outdated.wanted, outdated.latestManifest.version),
}
}
return {
...outdated,
change: 'unknown',
}
}
export function renderPackageName ({ belongsTo, packageName }: OutdatedPackage) {
switch (belongsTo) {
case 'devDependencies': return `${packageName} ${chalk.dim('(dev)')}`
case 'optionalDependencies': return `${packageName} ${chalk.dim('(optional)')}`
default: return packageName
}
}
export function renderCurrent ({ current, wanted }: OutdatedPackage) {
const output = current ?? 'missing'
if (current === wanted) return output
return `${output} (wanted ${wanted})`
}
export function renderLatest (outdatedPkg: OutdatedWithVersionDiff): string {
const { latestManifest, change, diff } = outdatedPkg
if (latestManifest == null) return ''
if (change === null || (diff == null)) {
return latestManifest.deprecated
? chalk.redBright.bold('Deprecated')
: latestManifest.version
}
return colorizeSemverDiff({ change, diff })
}
export function renderDetails ({ latestManifest }: OutdatedPackage) {
if (latestManifest == null) return ''
const outputs = []
if (latestManifest.deprecated) {
outputs.push(wrapAnsi(chalk.redBright(latestManifest.deprecated), 40))
}
if (latestManifest.homepage) {
outputs.push(chalk.underline(latestManifest.homepage))
}
return outputs.join('\n')
}