UNPKG

@swell/cli

Version:

Swell's command line interface/utility

98 lines (97 loc) 3.76 kB
import { Args } from '@oclif/core'; import ora from 'ora'; import { getBorderCharacters, table } from 'table'; import { FieldDefinitions } from '../../lib/info.js'; import style from '../../lib/style.js'; import { RemoteAppCommand } from '../../remote-app-command.js'; // The app fields will be displayed in the order they are defined here const FIELDS_TO_DISPLAY = [ 'name', 'description', 'version', 'public_id', 'published', 'date_created', 'date_updated', 'client', ]; export default class AppInfo extends RemoteAppCommand { static args = { versions: Args.string({ description: 'list all app versions' }), }; static description = `The command shows the latest information about your app including the name, description, version, public ID, test store, and more. If the ${style.command('versions')} argument is passed, it will output all versions of the app instead.`; static examples = [ { command: 'swell app info', description: 'Show general information about the app.', }, { command: 'swell app info versions', description: 'Show all established versions of the app.', }, ]; static summary = 'Show information about your Swell app.'; async run() { const { args } = await this.parse(AppInfo); const { versions } = args; if (versions) { await this.showVersions(); return; } await this.showAppInfo(); } async showAppInfo() { const spinner = ora(); spinner.start('Retrieving app info...'); const app = await this.getAppWithConfig().catch(() => null); this.app = app || this.swellConfig.store; if (!this.app) { spinner.fail('App not found'); return; } if (spinner.isSpinning) { spinner.stop(); } // we pull versions to make sure that what is displayed is the latest const versions = app ? await this.getVersions() : []; // Output the app field information, e.g.: // Name: My App const fieldDefinitions = new FieldDefinitions(this.app, versions); for (const field of FIELDS_TO_DISPLAY) { const fieldDef = fieldDefinitions.getFieldDefinition(field); if (fieldDef.value) { this.log(style.appConfigName(`${fieldDef.label}:`), fieldDef.value); } } // dashboard url is built, not part of the app object // we display it at the end for better readability and simplicity if (this.app.client_id) { this.log(style.appConfigName('Dashboard:'), style.link(this.dashboardUrl({}, this.app.client_id))); } } async showVersions() { const spinner = ora(); spinner.start('Retrieving app versions...'); this.app = await this.getAppWithConfig().catch((error) => { spinner.fail(`Error fetching app info: ${JSON.stringify(this.app)}`); throw error; }); const versions = await this.handleRequestErrors(async () => this.getVersions(), () => spinner.fail('Error fetching app versions')); if (versions.length === 0) { spinner.warn('No versions found'); return; } spinner.succeed(`${this.app.name} app versions:\n`); const displayData = versions.map((appVersion) => [ style.appConfigValue(appVersion.version), appVersion.description, new Date(appVersion.date_created).toUTCString(), ]); this.log(table(displayData, { border: getBorderCharacters('void'), drawHorizontalLine: () => false, })); } }