-
Notifications
You must be signed in to change notification settings - Fork 24
/
update.ts
157 lines (140 loc) · 4.75 KB
/
update.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
import select from '@inquirer/select'
import {Args, Command, Flags, Interfaces, ux} from '@oclif/core'
import {printTable} from '@oclif/table'
import {got} from 'got'
import {basename} from 'node:path'
import {sort} from 'semver'
import {Updater} from '../update.js'
export default class UpdateCommand extends Command {
static args = {
channel: Args.string({optional: true}),
}
static description = 'update the <%= config.bin %> CLI'
static examples = [
{
command: '<%= config.bin %> <%= command.id %> stable',
description: 'Update to the stable channel:',
},
{
command: '<%= config.bin %> <%= command.id %> --version 1.0.0',
description: 'Update to a specific version:',
},
{
command: '<%= config.bin %> <%= command.id %> --interactive',
description: 'Interactively select version:',
},
{
command: '<%= config.bin %> <%= command.id %> --available',
description: 'See available versions:',
},
]
static flags = {
autoupdate: Flags.boolean({hidden: true}),
available: Flags.boolean({
char: 'a',
description: 'See available versions.',
exclusive: ['version', 'interactive'],
}),
force: Flags.boolean({
description: 'Force a re-download of the requested version.',
exclusive: ['interactive', 'available'],
}),
interactive: Flags.boolean({
char: 'i',
description: 'Interactively select version to install. This is ignored if a channel is provided.',
exclusive: ['version'],
}),
verbose: Flags.boolean({
char: 'b',
dependsOn: ['available'],
description: 'Show more details about the available versions.',
}),
version: Flags.string({
char: 'v',
description: 'Install a specific version.',
exclusive: ['interactive'],
}),
}
async run(): Promise<void> {
const {args, flags} = await this.parse(UpdateCommand)
const updater = new Updater(this.config)
if (flags.available) {
const {distTags, index, localVersions} = await lookupVersions(updater, this.config)
const data = Object.keys(index).map((version) => {
const location = localVersions.find((l) => basename(l).startsWith(version)) || index[version]
const channel =
distTags[version] === 'latest'
? 'stable'
: distTags[version] === 'latest-rc'
? 'stable-rc'
: distTags[version]
return {
channel,
downloaded: location.includes('http') ? '' : 'true',
location,
version: this.config.version === version ? `${ux.colorize('yellowBright', version)} (current)` : version,
}
})
printTable({
borderStyle: 'vertical-with-outline',
columns: flags.verbose
? ['version', 'channel', 'downloaded', 'location']
: ['version', 'channel', 'downloaded'],
data,
headerOptions: {
formatter: 'capitalCase',
},
overflow: 'wrap',
})
return
}
if (args.channel && flags.version) {
this.error('You cannot specify both a version and a channel.')
}
return updater.runUpdate({
autoUpdate: flags.autoupdate,
channel: args.channel,
force: flags.force,
version: flags.interactive ? await promptForVersion(updater, this.config) : flags.version,
})
}
}
const lookupVersions = async (updater: Updater, config: Interfaces.Config) => {
ux.action.start('Looking up versions')
const [index, localVersions, distTags] = await Promise.all([
updater.fetchVersionIndex(),
updater.findLocalVersions(),
fetchDistTags(config),
])
ux.action.stop(`Found ${Object.keys(index).length} versions`)
return {
distTags,
index,
localVersions,
}
}
const fetchDistTags = async (config: Interfaces.Config) => {
const distTags = config.pjson.oclif.update?.disableNpmLookup
? {}
: await got
.get(`${config.npmRegistry ?? 'https://registry.npmjs.org'}/${config.pjson.name}`)
.json<{
'dist-tags': Record<string, string>
}>()
.then((r) => r['dist-tags'])
// Invert the distTags object so we can look up the channel by version
return Object.fromEntries(Object.entries(distTags ?? {}).map(([k, v]) => [v, k]))
}
const displayName = (value: string, distTags: Record<string, string>) =>
`${value} ${distTags[value] ? `(${distTags[value]})` : ''}`
const promptForVersion = async (updater: Updater, config: Interfaces.Config): Promise<string> => {
const {distTags, index} = await lookupVersions(updater, config)
return select({
choices: sort(Object.keys(index))
.reverse()
.map((v) => ({name: displayName(v, distTags), value: v})),
loop: false,
message: 'Select a version to update to',
pageSize: 10,
})
}