/
update.ts
91 lines (80 loc) · 2.67 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
import select from '@inquirer/select'
import {Args, Command, Flags, ux} from '@oclif/core'
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.',
}),
force: Flags.boolean({
description: 'Force a re-download of the requested version.',
}),
interactive: Flags.boolean({
char: 'i',
description: 'Interactively select version to install. This is ignored if a channel is provided.',
exclusive: ['version'],
}),
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 [index, localVersions] = await Promise.all([updater.fetchVersionIndex(), updater.findLocalVersions()])
const allVersions = sort(Object.keys(index)).reverse()
const table = allVersions.map((version) => {
const location = localVersions.find((l) => basename(l).startsWith(version)) || index[version]
return {location, version}
})
ux.table(table, {location: {}, version: {}})
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) : flags.version,
})
}
}
const promptForVersion = async (updater: Updater): Promise<string> =>
select({
choices: sort(Object.keys(await updater.fetchVersionIndex()))
.reverse()
.map((v) => ({value: v})),
loop: false,
message: 'Select a version to update to',
})