import * as chalk from 'chalk'; import {command, flag, help, namespace, option, param} from 'oo-cli'; import {die} from '../../lib/die'; import {Rivendell} from '../../lib/Rivendell'; import {formatError} from '../../lib/formatError'; import {TerminalConfirm} from '../../lib/TerminalConfirm'; @namespace('directory') export class UninstallCommand { @param @help('The App ID') public appId!: string; @param @help('The Tracker ID of the account to install into') public trackerId!: string; @flag('no-prompt') @help('Use default answer to any prompt question (default is yes)') public noPrompt: boolean = false; @option('a') @help('The availability zone that will be targeted (default: us)') private availability: string = ''; @command @help('Uninstall an app from an account') public async uninstall() { try { const install = await Rivendell.fetchAppInstallation(this.appId, this.trackerId, this.availability); const appVersion = await Rivendell.fetchAppVersion( {appId: install.appId, version: install.version}, this.availability || 'us' ); const manifest = JSON.parse(appVersion.manifestJson); const hasOpalFunction = manifest.functions && Object.values(manifest.functions).some((fn: any) => fn.opal_tool === true); const hasOpalCategory = manifest.meta?.categories?.includes('Opal'); const isOpalApp = hasOpalFunction || hasOpalCategory; if (isOpalApp) { if (!await TerminalConfirm.ask( chalk.red(`The app contains Opal tools that may have been added to Opal. If the app is uninstalled, the tools will stop working properly in Opal. Please make sure the tools are removed from Opal first, before uninstalling the app. Are you sure you want to uninstall?`), this.noPrompt, true )) { die('Operation cancelled.'); } } await Rivendell.uninstall(install.id, this.availability); console.log(chalk.green(`Uninstalled ${install.appId} from ${install.trackerId} (${this.availability || 'us'})`)); } catch (e: any) { die(formatError(e)); } } }