|
| 1 | +/** |
| 2 | + * AbstractMakeCommand |
| 3 | + * ------------------------------------- |
| 4 | + * |
| 5 | + */ |
| 6 | +import * as _ from 'lodash'; |
| 7 | +import { writeTemplate } from './lib/template'; |
| 8 | +import { askFileName, buildFilePath, existsFile, parseName, updateTargets } from './lib/utils'; |
| 9 | + |
| 10 | +export interface MakeCommand { |
| 11 | + context: any; |
| 12 | + type: string; |
| 13 | + suffix: string; |
| 14 | + template: string; |
| 15 | + target: string; |
| 16 | + updateTargets: boolean; |
| 17 | + |
| 18 | + run(): Promise<void>; |
| 19 | + write(): Promise<void>; |
| 20 | +} |
| 21 | + |
| 22 | +export class AbstractMakeCommand { |
| 23 | + |
| 24 | + static command = 'make:command'; |
| 25 | + static description = 'description'; |
| 26 | + |
| 27 | + public context: any; |
| 28 | + public type = 'Type'; |
| 29 | + public suffix = 'Suffix'; |
| 30 | + public prefix = ''; |
| 31 | + public template = 'template.hbs'; |
| 32 | + public target = 'api/target/path'; |
| 33 | + public updateTargets = true; |
| 34 | + |
| 35 | + static async action(command: MakeCommand): Promise<void> { |
| 36 | + try { |
| 37 | + await command.run(); |
| 38 | + await command.write(); |
| 39 | + if (command.updateTargets) { |
| 40 | + await updateTargets(); |
| 41 | + } |
| 42 | + process.exit(0); |
| 43 | + } catch (e) { |
| 44 | + process.exit(1); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + constructor(context?: any) { |
| 49 | + this.context = _.cloneDeep(context); |
| 50 | + } |
| 51 | + |
| 52 | + public async run(): Promise<void> { |
| 53 | + this.context = await askFileName(this.context, this.type, this.suffix, this.prefix); |
| 54 | + } |
| 55 | + |
| 56 | + public async write(): Promise<void> { |
| 57 | + const filePath = buildFilePath(this.target, this.context.name); |
| 58 | + await existsFile(filePath, true); |
| 59 | + this.context.name = parseName(this.context.name, this.suffix); |
| 60 | + await writeTemplate(this.template, filePath, this.context); |
| 61 | + } |
| 62 | + |
| 63 | +} |
0 commit comments