UNPKG

@swell/cli

Version:

Swell's command line interface/utility

121 lines (120 loc) 4.29 kB
import { input } from '@inquirer/prompts'; import { Args, Flags } from '@oclif/core'; import { CreateConfigCommand } from '../../create-config-command.js'; import { ConfigType } from '../../lib/apps/index.js'; import { toFileName } from '../../lib/create/index.js'; import { SCHEMAS } from '../../lib/create/schemas.js'; import { parseFields, toSettingLabel, } from '../../lib/create/setting.js'; export default class CreateSetting extends CreateConfigCommand { static args = { name: Args.string({ default: '', description: 'Setting name (e.g., api-config)', }), }; static description = 'Create a setting configuration in the settings folder.'; static examples = [ '$ swell create setting', '$ swell create setting api-config -y', '$ swell create setting api-config -f api_key:short_text,enabled:boolean -l "API Config" -y', ]; static helpMeta = { usageDirect: '<name> [...] -y', }; static flags = { label: Flags.string({ char: 'l', default: '', description: 'Display label (default: titleized name)', }), description: Flags.string({ char: 'd', default: '', description: 'Description', }), fields: Flags.string({ char: 'f', default: '', description: 'Fields as id:type pairs (e.g., api_key:short_text,enabled:boolean)', }), overwrite: Flags.boolean({ default: false, description: 'Overwrite existing file', }), yes: Flags.boolean({ char: 'y', description: 'Skip prompts, require all arguments', }), }; static summary = 'Create a setting configuration in the settings folder.'; createType = ConfigType.SETTING; async run() { const { args, flags } = await this.parse(CreateSetting); const confirmYes = Boolean(flags.yes); // NON-INTERACTIVE PATH if (confirmYes) { const argName = args.name; if (!argName) { this.error('Missing required argument for non-interactive mode: NAME\n\nExample: swell create setting api-config -y', { exit: 1 }); } const name = toFileName(argName); const label = flags.label || toSettingLabel(name); const description = flags.description || ''; const fieldsRaw = (flags.fields || ''); const fieldPairs = fieldsRaw ? fieldsRaw .split(',') .map((v) => v.trim()) .filter(Boolean) : []; const fileName = toFileName(name); const fileBody = { $schema: SCHEMAS.SETTING, description, fields: parseFields(fieldPairs), label, }; await this.createFile({ fileBody, fileName }, flags.overwrite, /* shouldConfirm */ false); return; } // INTERACTIVE PATH let name = args.name; if (!name) { name = await input({ default: 'my-setting', message: 'Setting name', }); } name = toFileName(name); let { label, description } = flags; const { fields: fieldsFlag, overwrite } = flags; if (!label) { label = await input({ default: toSettingLabel(name), message: 'Setting label', }); } if (!description) { description = await input({ default: '', message: 'Describe what this setting is for', }); } const fieldsRaw = (fieldsFlag || ''); const fieldPairs = fieldsRaw ? fieldsRaw .split(',') .map((v) => v.trim()) .filter(Boolean) : []; const fileName = toFileName(name); const fileBody = { $schema: SCHEMAS.SETTING, description, fields: parseFields(fieldPairs), label, }; await this.createFile({ fileBody, fileName }, overwrite); } }