import * as dntShim from "../../../../../../_dnt.shims.js"; import { Command } from "../command.js"; import { ValidationError } from "../../flags/_errors.js"; import type { Provider, Versions } from "./provider.js"; import { EnumType } from "../types/enum.js"; export interface UpgradeCommandOptions< TProvider extends Provider = Provider, TProviders extends TProvider | Array = | TProvider | Array, > { provider: TProviders; main?: string; importMap?: string; args?: Array; } export class UpgradeCommand extends Command { private readonly providers: ReadonlyArray; 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.` ) .noGlobals() .type("provider", new EnumType(this.getProviderNames())) .option( "-r, --registry ", `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(), ); dntShim.Deno.exit(0); }, }, ) .option( "--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> { const { versions } = await this.getVersions(); return versions; } public async getLatestVersion(): Promise { const { latest } = await this.getVersions(); return latest; } public getVersions(): Promise { 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 { return this.providers.map((provider) => provider.name); } }