/
recursive.ts
168 lines (149 loc) · 4.6 KB
/
recursive.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
import { TABLE_OPTIONS } from '@pnpm/cli-utils'
import {
outdatedDepsOfProjects,
OutdatedPackage,
} from '@pnpm/outdated'
import {
DependenciesField,
IncludedDependencies,
ProjectManifest,
} from '@pnpm/types'
import { table } from '@zkochan/table'
import chalk from 'chalk'
import isEmpty from 'ramda/src/isEmpty'
import sortWith from 'ramda/src/sortWith'
import {
getCellWidth,
OutdatedCommandOptions,
renderCurrent,
renderDetails,
renderLatest,
renderPackageName,
toOutdatedWithVersionDiff,
} from './outdated'
import { DEFAULT_COMPARATORS } from './utils'
const DEP_PRIORITY: Record<DependenciesField, number> = {
dependencies: 1,
devDependencies: 2,
optionalDependencies: 0,
}
const COMPARATORS = [
...DEFAULT_COMPARATORS,
(o1: OutdatedInWorkspace, o2: OutdatedInWorkspace) =>
DEP_PRIORITY[o1.belongsTo] - DEP_PRIORITY[o2.belongsTo],
]
interface OutdatedInWorkspace extends OutdatedPackage {
belongsTo: DependenciesField
current?: string
dependentPkgs: Array<{ location: string, manifest: ProjectManifest }>
latest?: string
packageName: string
wanted: string
}
export default async (
pkgs: Array<{ dir: string, manifest: ProjectManifest }>,
params: string[],
opts: OutdatedCommandOptions & { include: IncludedDependencies }
) => {
const outdatedMap = {} as Record<string, OutdatedInWorkspace>
const rootManifest = pkgs.find(({ dir }) => dir === opts.lockfileDir ?? opts.dir)
const outdatedPackagesByProject = await outdatedDepsOfProjects(pkgs, params, {
...opts,
fullMetadata: opts.long,
ignoreDependencies: new Set(rootManifest?.manifest?.pnpm?.updateConfig?.ignoreDependencies ?? []),
retry: {
factor: opts.fetchRetryFactor,
maxTimeout: opts.fetchRetryMaxtimeout,
minTimeout: opts.fetchRetryMintimeout,
retries: opts.fetchRetries,
},
timeout: opts.fetchTimeout,
})
for (let i = 0; i < outdatedPackagesByProject.length; i++) {
const { dir, manifest } = pkgs[i]
outdatedPackagesByProject[i].forEach((outdatedPkg) => {
const key = JSON.stringify([outdatedPkg.packageName, outdatedPkg.current, outdatedPkg.belongsTo])
if (!outdatedMap[key]) {
outdatedMap[key] = { ...outdatedPkg, dependentPkgs: [] }
}
outdatedMap[key].dependentPkgs.push({ location: dir, manifest })
})
}
if (isEmpty(outdatedMap)) return { output: '', exitCode: 0 }
if (opts.table !== false) {
return { output: renderOutdatedTable(outdatedMap, opts), exitCode: 1 }
}
return { output: renderOutdatedList(outdatedMap, opts), exitCode: 1 }
}
function renderOutdatedTable (outdatedMap: Record<string, OutdatedInWorkspace>, opts: { long?: boolean }) {
const columnNames = [
'Package',
'Current',
'Latest',
'Dependents',
]
const columnFns = [
renderPackageName,
renderCurrent,
renderLatest,
dependentPackages,
]
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])
const data = [
columnNames,
...sortOutdatedPackages(Object.values(outdatedMap))
.map((outdatedPkg) => columnFns.map((fn) => fn(outdatedPkg))),
]
return table(data, {
...TABLE_OPTIONS,
columns: {
...TABLE_OPTIONS.columns,
// Dependents column:
3: {
width: getCellWidth(data, 3, 30),
wrapWord: true,
},
},
})
}
function renderOutdatedList (outdatedMap: Record<string, OutdatedInWorkspace>, opts: { long?: boolean }) {
return sortOutdatedPackages(Object.values(outdatedMap))
.map((outdatedPkg) => {
let info = `${chalk.bold(renderPackageName(outdatedPkg))}
${renderCurrent(outdatedPkg)} ${chalk.grey('=>')} ${renderLatest(outdatedPkg)}`
const dependents = dependentPackages(outdatedPkg)
if (dependents) {
info += `\n${chalk.bold(
outdatedPkg.dependentPkgs.length > 1
? 'Dependents:'
: 'Dependent:'
)} ${dependents}`
}
if (opts.long) {
const details = renderDetails(outdatedPkg)
if (details) {
info += `\n${details}`
}
}
return info
})
.join('\n\n') + '\n'
}
function dependentPackages ({ dependentPkgs }: OutdatedInWorkspace) {
return dependentPkgs
.map(({ manifest, location }) => manifest.name ?? location)
.sort()
.join(', ')
}
function sortOutdatedPackages (outdatedPackages: readonly OutdatedInWorkspace[]) {
return sortWith(
COMPARATORS,
outdatedPackages.map(toOutdatedWithVersionDiff)
)
}