UNPKG

@swell/cli

Version:

Swell's command line interface/utility

88 lines (87 loc) 3.52 kB
import { checkbox } from '@inquirer/prompts'; import { Flags } from '@oclif/core'; import { CreateCollectionCommand } from '../../create-collection-command.js'; import { ConfigType } from '../../lib/apps/index.js'; import { toFileName } from '../../lib/create/index.js'; import { parseEvents, parseFields, } from '../../lib/create/model.js'; export default class CreateModel extends CreateCollectionCommand { static description = `This command assists in generating data model configuration files. CLI will prompt for values when flags are not present.`; static examples = [ { command: 'swell create model', description: 'Create data model configuration interactively.', }, { command: 'swell create model visitors -e created,updated,deleted.', description: 'Specify events of the data model.', }, { command: 'swell create model visitors -f first_name:string,last_name:string,photo:file,visit_count:int,visits:link', description: 'Specify fields of the data model.', }, { command: 'swell create model visitors -f first_name:string,last_name:string,photo:file,visit_count:int,visits:link -d "Visitors Tracking" -o -e created,updated,deleted', description: 'Scaffold a data model configuration with args and flags.', }, ]; static flags = { description: Flags.string({ char: 'd', default: '', description: 'data model description', }), events: Flags.string({ char: 'e', default: 'created,updated,deleted', description: 'list of data model events delimited by commas', }), fields: Flags.string({ char: 'f', default: '', description: 'list of data model fields delimited by commas', }), label: Flags.string({ char: 'l', default: '', description: 'data model label, i.e. Products', }), overwrite: Flags.boolean({ default: false, description: 'Overwrite existing data model configuration file.', }), }; static summary = 'Initialize a data model config file with properties in the models folder.'; createType = ConfigType.MODEL; async run() { const { flags } = await this.parse(CreateModel); const { collection, description, label } = await this.promptCollection(); const fileName = toFileName(collection); const fileBody = { collection, }; const { events, fields, overwrite } = flags; if (label) { fileBody.label = label; } fileBody.description = description; fileBody.fields = fields ? parseFields(fields.split(',')) : {}; if (fileBody.events) { fileBody.events = events ? parseEvents(events.split(',')) : {}; } else { const eventsInput = await checkbox({ choices: [ { name: 'created', value: 'created' }, { name: 'updated', value: 'updated' }, { name: 'deleted', value: 'deleted' }, ], message: 'Which events should be captured by default?', }); fileBody.events = parseEvents(eventsInput); } await this.createFile({ fileBody, fileName, }, overwrite); } }