|
| 1 | +import { isNonEmptyStringAndNotWhitespace } from '@sindresorhus/is'; |
| 2 | +import { Command, Option } from 'clipanion'; |
| 3 | +import prettyMilliseconds from 'pretty-ms'; |
| 4 | +import { type InstallToolType, uninstallTool } from '../install-tool'; |
| 5 | +import { ResolverMap } from '../tools'; |
| 6 | +import { logger } from '../utils'; |
| 7 | +import { MissingVersion } from '../utils/codes'; |
| 8 | +import { command, isToolIgnored } from './utils'; |
| 9 | + |
| 10 | +@command('containerbase-cli') |
| 11 | +export class UninstallToolCommand extends Command { |
| 12 | + static override paths = [['uninstall', 'tool'], ['ut']]; |
| 13 | + |
| 14 | + static override usage = Command.Usage({ |
| 15 | + description: 'Uninstalls a tool from the container.', |
| 16 | + examples: [ |
| 17 | + ['Uninstalls node 14.17.0', '$0 uninstall tool node 14.17.0'], |
| 18 | + // ['Uninstalls latest pnpm version', '$0 uninstall tool pnpm'], |
| 19 | + ], |
| 20 | + }); |
| 21 | + |
| 22 | + name = Option.String(); |
| 23 | + |
| 24 | + dryRun = Option.Boolean('-d,--dry-run', false); |
| 25 | + |
| 26 | + version = Option.String({ required: false }); |
| 27 | + |
| 28 | + protected type: InstallToolType | undefined; |
| 29 | + |
| 30 | + override async execute(): Promise<number | void> { |
| 31 | + const start = Date.now(); |
| 32 | + |
| 33 | + if (await isToolIgnored(this.name)) { |
| 34 | + logger.info({ tool: this.name }, 'tool ignored'); |
| 35 | + return 0; |
| 36 | + } |
| 37 | + |
| 38 | + let version = this.version; |
| 39 | + |
| 40 | + const type = ResolverMap[this.name] ?? this.type; |
| 41 | + |
| 42 | + // TODO: support uninstall all versions |
| 43 | + if (!isNonEmptyStringAndNotWhitespace(version)) { |
| 44 | + logger.error(`No version found for ${this.name}`); |
| 45 | + return MissingVersion; |
| 46 | + } |
| 47 | + |
| 48 | + version = version.replace(/^v/, ''); // trim optional 'v' prefix |
| 49 | + |
| 50 | + let error = false; |
| 51 | + logger.info(`Uninstalling ${type ?? 'tool'} ${this.name}@${version}...`); |
| 52 | + try { |
| 53 | + return await uninstallTool(this.name, version, this.dryRun, type); |
| 54 | + } catch (err) { |
| 55 | + error = true; |
| 56 | + logger.debug(err); |
| 57 | + if (err instanceof Error) { |
| 58 | + logger.error(err.message); |
| 59 | + } |
| 60 | + return 1; |
| 61 | + /* v8 ignore next -- coverage bug */ |
| 62 | + } finally { |
| 63 | + if (error) { |
| 64 | + logger.fatal( |
| 65 | + `Uninstall ${this.type ?? 'tool'} ${this.name} failed in ${prettyMilliseconds(Date.now() - start)}.`, |
| 66 | + ); |
| 67 | + } else { |
| 68 | + logger.info( |
| 69 | + `Uninstall ${this.type ?? 'tool'} ${this.name} succeeded in ${prettyMilliseconds(Date.now() - start)}.`, |
| 70 | + ); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +@command('uninstall-tool') |
| 77 | +export class UninstallToolShortCommand extends UninstallToolCommand { |
| 78 | + static override paths = [Command.Default]; |
| 79 | + static override usage = Command.Usage({ |
| 80 | + description: 'Uninstalls a tool from the container.', |
| 81 | + examples: [ |
| 82 | + ['Uninstalls node v14.17.0', '$0 node 14.17.0'], |
| 83 | + // ['Uninstalls all pnpm versions', '$0 pnpm'], |
| 84 | + ], |
| 85 | + }); |
| 86 | +} |
0 commit comments