-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathupgrade_command.ts
200 lines (187 loc) · 5.75 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
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
import { bold, brightBlue } from "@std/fmt/colors";
import { ValidationError } from "../_errors.ts";
import { exit } from "@cliffy/internal/runtime/exit";
import { Command } from "../command.ts";
import { EnumType } from "../types/enum.ts";
import { createLogger } from "./logger.ts";
import type { Provider, Versions } from "./provider.ts";
import { Spinner } from "./spinner.ts";
import {
type RuntimeOptions,
type RuntimeOptionsMap,
upgrade,
} from "./upgrade.ts";
export interface UpgradeCommandOptions<
TProvider extends Provider = Provider,
> extends RuntimeOptions {
provider: TProvider | Array<TProvider>;
runtime?: RuntimeOptionsMap;
spinner?: boolean;
}
/**
* The `UpgradeCommand` adds an upgrade functionality to the cli to be able to
* seamlessly upgrade the cli to the latest or a specific version from a
* provided registry with any supported runtime.
* Currently supported runtimes are: `deno`, `node` and `bun`.
*
* @example Upgrade command example.
*
* ```
* import { Command } from "@cliffy/command";
* import { UpgradeCommand } from "@cliffy/command/upgrade";
* import { DenoLandProvider } from "@cliffy/command/upgrade/provider/deno-land";
* import { GithubProvider } from "@cliffy/command/upgrade/provider/github";
* import { JsrProvider } from "@cliffy/command/upgrade/provider/jsr";
* import { NestLandProvider } from "@cliffy/command/upgrade/provider/nest-land";
* import { NpmProvider } from "@cliffy/command/upgrade/provider/npm";
*
* await new Command()
* .name("my-cli")
* .version("0.2.1")
* .command(
* "upgrade",
* new UpgradeCommand({
* provider: [
* new JsrProvider({ scope: "examples" }),
* new NpmProvider({ scope: "examples" }),
* new DenoLandProvider(),
* new NestLandProvider(),
* new GithubProvider({ repository: "examples/my-cli" }),
* ],
* }),
* )
* .parse();
* ```
*/
export class UpgradeCommand extends Command {
private readonly providers: ReadonlyArray<Provider>;
constructor(
{ provider, spinner: withSpinner = true, ...options }:
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.`
)
.noGlobals()
.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(),
);
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.",
)
.option(
"-v, --verbose",
"Log verbose output.",
)
.option("--no-spinner", "Disable spinner.", {
hidden: !withSpinner,
})
.complete("version", () => this.getAllVersions())
.action(
async (
{
registry: provider,
version,
force,
verbose,
spinner: spinnerEnabled,
},
) => {
const name: string = this.getMainCommand().getName();
const currentVersion: string | undefined = this.getVersion();
const spinner = withSpinner && spinnerEnabled
? new Spinner({
message: brightBlue(
`Upgrading ${bold(name)} from version ${
bold(currentVersion ?? "")
} to ${bold(version)}...`,
),
})
: undefined;
const logger = createLogger({ spinner, verbose });
spinner?.start();
provider.setLogger(logger);
try {
await upgrade({
name,
to: version,
from: currentVersion,
force,
provider,
verbose,
logger,
...options,
});
} catch (error: unknown) {
logger.error(
!verbose && error instanceof Error ? error.message : error,
);
spinner?.stop();
exit(1);
} finally {
spinner?.stop();
}
},
);
}
public async getAllVersions(): Promise<Array<string>> {
const { versions } = await this.getVersions();
return versions;
}
public async hasRequiredPermissions(): Promise<boolean> {
return await this.getProvider().hasRequiredPermissions();
}
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);
}
}