-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
upgrade_command.ts
113 lines (104 loc) 路 3.16 KB
/
upgrade_command.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
import { Command } from "../command.ts";
import { ValidationError } from "../../flags/_errors.ts";
import type { Provider, Versions } from "./provider.ts";
import { EnumType } from "../types/enum.ts";
export interface UpgradeCommandOptions<
P extends Provider = Provider,
V extends P | Array<P> = P | Array<P>,
> {
provider: V;
main?: string;
importMap?: string;
args?: Array<string>;
}
export class UpgradeCommand extends Command {
private readonly providers: ReadonlyArray<Provider>;
constructor(
{ provider, main, args, importMap }: UpgradeCommandOptions,
) {
super();
this.providers = Array.isArray(provider) ? provider : [provider];
if (!this.providers.length) {
throw new Error(`No upgrade provider defined!`);
}
this
.description(() =>
`Upgrade ${this.getMainCommand().getName()} executable to latest or given version.`
)
.type("provider", new EnumType(this.getProviderNames()))
.option(
"-r, --registry <name:provider>",
`The registry name from which to upgrade.`,
{
default: this.getProvider().name,
hidden: this.providers.length < 2,
value: (registry) => this.getProvider(registry),
},
)
.option(
"-l, --list-versions",
"Show available versions.",
{
action: async ({ registry }) => {
await registry.listVersions(
this.getMainCommand().getName(),
this.getVersion(),
);
Deno.exit(0);
},
},
)
.option(
"--version <version:string:version>",
"The version to upgrade to.",
{ default: "latest" },
)
.option(
"-f, --force",
"Replace current installation even if not out-of-date.",
)
.complete("version", () => this.getAllVersions())
.action(async ({ registry, version: targetVersion, force }) => {
const name: string = this.getMainCommand().getName();
const currentVersion: string | undefined = this.getVersion();
if (
force || !currentVersion ||
await registry.isOutdated(name, currentVersion, targetVersion)
) {
await registry.upgrade({
name,
main,
importMap,
from: currentVersion,
to: targetVersion,
args,
});
}
});
}
public async getAllVersions(): Promise<Array<string>> {
const { versions } = await this.getVersions();
return versions;
}
public async getLatestVersion(): Promise<string> {
const { latest } = await this.getVersions();
return latest;
}
public getVersions(): Promise<Versions> {
return this.getProvider().getVersions(
this.getMainCommand().getName(),
);
}
private getProvider(name?: string): Provider {
const provider = name
? this.providers.find((provider) => provider.name === name)
: this.providers[0];
if (!provider) {
throw new ValidationError(`Unknown provider "${name}"`);
}
return provider;
}
private getProviderNames(): Array<string> {
return this.providers.map((provider) => provider.name);
}
}