import {Directive, OptionDefinition} from '../../cli' import {Injectable} from '../../di' import {stringToPascal} from '../../util' import {templateMigration} from '../template/migration' import {CLIDirective} from '../../cli/decorators' /** * CLI directive that creates migration classes from template. */ @Injectable() @CLIDirective() export class CreateMigrationDirective extends Directive { getDescription(): string { return 'create a new migration' } getKeywords(): string | string[] { return ['create-migration', 'make-migration'] } getOptions(): OptionDefinition[] { return [ '{description} | Description of what the migration does', ] } getHelpText(): string { return [ 'Creates a new migration file in `src/app/migrations`.', 'To use, specify a string describing what the migration does. For example:', './ex create-migration "Add version column to sessions table"', ].join('\n\n') } async handle(): Promise { const description = this.option('description') const className = `${stringToPascal(description)}Migration` const fileName = `${(new Date()).toISOString()}_${className}.migration.ts` const path = this.app().path('..', 'src', 'app', 'migrations', fileName) // Create the migrations directory, if it doesn't already exist await path.concat('..').mkdir() // Render the template const rendered = await templateMigration.render(className, className, path) await path.write(rendered) this.success(`Created migration: ${className}`) } }