UNPKG

@swell/cli

Version:

Swell's command line interface/utility

92 lines (91 loc) 2.77 kB
/** * Helper module for the `info` command. Encapsulates the logic for * displaying the app's information. */ import { camelize } from 'inflection'; import style from './style.js'; function transformDate(date) { return date ? new Date(date).toUTCString() : ''; } function transformBoolean(bool) { return bool ? 'Yes' : 'No'; } export class FieldDefinitions { app; versions; constructor(app, versions) { this.app = app; this.versions = versions; } get client() { return { label: 'Test store', transform: (client) => client && `${client.name} (${style.storeId(client.id)})`, }; } get dateCreated() { return { label: 'Created at', transform: transformDate }; } get dateUpdated() { return { label: 'Updated at', transform: transformDate }; } get description() { return { label: 'Description' }; } get installed() { return { label: 'Installed', transform: transformBoolean }; } get name() { return { label: 'Name' }; } get publicId() { return { label: 'Public ID', transform: (publicId) => { if (publicId) return publicId; // replace _ as the first char any number of times in private_id return this.app.private_id?.replace(/^_+/, '') || ''; }, }; } get published() { return { label: 'Published', transform: transformBoolean }; } get version() { return { label: 'Version', transform: (_version) => { if (this.versions.length === 0) { return this.app.version || 'N/A'; } return [ style.appConfigValue(this.versions[0].version), '(latest)', this.versions.length > 1 ? `| ${this.versions.length} (total)` : '', ].join(' '); }, }; } /** * Returns a FieldDefinition object for the given field. * * The function uses the field name to find its value in the app object. If * the FieldDefinition has a transform function for the required field, * it will be called to transform the value before returning the * FieldDefinition. * * @param field The field to get the definition for * * @returns The FieldDefinition object */ getFieldDefinition(field) { const fieldDef = this[camelize(field, true)]; let value = this.app[field]; if (fieldDef.transform) { value = fieldDef.transform(value); } fieldDef.value = value || ''; return fieldDef; } }