UNPKG

@swell/cli

Version:

Swell's command line interface/utility

101 lines (100 loc) 3.67 kB
import { Separator, input, select } from '@inquirer/prompts'; import { Args, Flags } from '@oclif/core'; import { CreateConfigCommand } from './create-config-command.js'; import { toCollectionLabel, toCollectionName } from './lib/create/index.js'; /** * A base class for Swell CLI Create [content|models] command input. * * This class extends the `CreateConfigCommand` class and adds: * * - promptCollection: Get collection name and description */ export class CreateCollectionCommand extends CreateConfigCommand { // base flags for all AppCommands static args = { collection: Args.string({ default: '', description: 'collection to create or apply to, i.e. products', }), }; // needs to be in a ES2021 format: https://github.com/oclif/oclif/issues/1100 static baseFlags = { 'app-path': Flags.string({ description: 'path to your app directory', }), }; static flags = { description: Flags.string({ char: 'd', default: '', description: 'the collection description', }), label: Flags.string({ char: 'l', default: '', description: 'the collection label', }), overwrite: Flags.boolean({ char: 'o', default: false, description: 'overwrite existing collection configuration file', }), }; async promptCollection() { // Parse using the concrete command class so child-command flags // (e.g., --events, --fields) are recognized during interactive flows. const klass = super.ctor; const { args, flags } = await this.parse(klass); let { description, label } = flags; let { collection } = args; let isStandardCollection = false; let collectionInput = collection; if (!collection) { const collections = await this.getCollectionOptions(); collectionInput = await select({ choices: [ { name: 'Create a new collection', value: '' }, new Separator(), ...collections.map((collectionName) => ({ name: collectionName, value: collectionName, })), ], message: 'Where will content be stored?', pageSize: 25, }); if (collectionInput) { isStandardCollection = true; } else { collectionInput = await input({ default: 'special-things', message: 'Collection name', }); } // we replace unwanted characters with underscores to make things easier // for the inflection library collection = toCollectionName(collectionInput); } if (!label && !isStandardCollection) { label = await input({ default: toCollectionLabel(collection), message: 'Collection label', }); label = toCollectionLabel(label); } description = description || (await input({ default: 'Special things for special people', message: isStandardCollection ? `Describe the purpose of fields being added to ${collection}` : 'Describe what this collection is for', })); return { collection, description, label, }; } }