UNPKG

@swell/cli

Version:

Swell's command line interface/utility

169 lines (168 loc) 6.8 kB
import { input, select } from '@inquirer/prompts'; import { Args, Flags } from '@oclif/core'; import { capitalize, singularize } from 'inflection'; import { CreateConfigCommand } from '../../create-config-command.js'; import { ConfigType } from '../../lib/apps/index.js'; import { toFileName, toNotificationLabel } from '../../lib/create/index.js'; export default class CreateNotification extends CreateConfigCommand { static args = { model: Args.string({ description: 'data model to trigger the notification by', }), name: Args.string({ description: 'internal name for the notification', }), }; static description = `This command assists in generating notifications configuration files. This CLI command will prompt for values when flags are not present.`; static examples = [ { command: 'swell create notification', description: 'Create notification configuration interactively.', }, { command: 'swell create notification subscriptions confirmed --event subscription.created --once --s "Your subscription has been confirmed" -d "Send a confirmation email when subscription is created"', description: 'Specify the model, name, event, subject, and description.', }, { command: 'swell create notification orders paid --event paid"', description: 'Specify the event to trigger the notification.', }, ]; static flags = { admin: Flags.boolean({ char: 'a', description: 'indicate when the notification is sent to store admins only', }), description: Flags.string({ char: 'd', default: '', description: 'notification description', }), event: Flags.string({ char: 'e', default: '', description: 'data model event to trigger the notification event, i.e. created', }), once: Flags.boolean({ char: 'o', description: 'indicate the notification should be sent only once when conditions are met, instead of repeating', }), overwrite: Flags.boolean({ description: 'overwrite existing notification configuration file', }), subject: Flags.string({ char: 's', default: '', description: 'notification email subject', }), }; static summary = 'Initialize notification files and configuration with properties in the notifications folder.'; createType = ConfigType.NOTIFICATION; async run() { const { args, flags } = await this.parse(CreateNotification); if (args.model && args.name) { await this.writeNotificationFile({ admin: flags.admin, description: flags.description, event: flags.event, model: args.model, name: `${singularize(args.model)}-${args.name.replace(singularize(args.model), '')}`, once: flags.once, subject: flags.subject, }, false); } else { let { admin } = flags; if (admin === undefined) { const choice = await select({ choices: [ { name: 'Admins', value: 'admin' }, { name: 'Customers', value: 'customer' }, ], message: 'Who will be notified?', }); if (choice === 'admin') { admin = true; } } const collections = await this.getCollectionOptions(); const model = await select({ choices: collections.map((collectionName) => ({ name: collectionName, value: collectionName, })), loop: false, message: 'Which model will trigger the notification?', pageSize: 25, }); const event = await input({ default: 'created', message: 'Event to trigger the notification', }); const answers = { description: await input({ default: `Notify ${admin ? 'admin' : 'customer'} when ${singularize(model)}...`, message: 'Describe what the notification does', }), name: await input({ default: `${singularize(model)}-${event}`, message: 'Name of the notification', }), once: await select({ choices: [ { name: 'Every time conditions are met', value: false, }, { name: 'Only the first time conditions are met', value: true, }, ], message: 'When should we send this notification?', }), subject: await input({ default: `Something happened with ${model}`, message: 'Subject of the notification email', }), }; const { description, name, once, subject } = answers; await this.writeNotificationFile({ admin, description, event, model, name, once, subject }, false); } } async writeNotificationFile({ admin, description, event, model, name, once, subject, }, overwrite) { const label = [name].map((val) => capitalize(val).trim()).join(' '); const fileName = toFileName(label); const noteLabel = toNotificationLabel(name); const fileBody = { collection: model, conditions: {}, event: event || 'created', label: noteLabel, method: 'email', subject: subject || `${noteLabel}...`, }; fileBody.description = description; if (admin) { fileBody.admin = true; } else { fileBody.contact = 'account.email'; } // Note since we are using events, we don't need 'new' option if (!once) { fileBody.repeat = true; } fileBody.query = { expand: [], }; fileBody.sample = {}; const jsonCreated = await this.createFile({ fileBody, fileName, }, overwrite); if (jsonCreated) { await this.createFile({ extension: 'tpl', fileBody: '\n', fileName }, overwrite, false); } } }