|
| 1 | +import { Listr } from 'listr2'; |
| 2 | +import { join } from 'path'; |
| 3 | +import Command from '../../lib/command'; |
| 4 | +import Aliases from '../../common/constants/aliases'; |
| 5 | +import FileWorker from '../../common/file-worker'; |
| 6 | +import PromisifiedFs from '../../common/promisified-fs'; |
| 7 | + |
| 8 | +export default class Generate extends Command { |
| 9 | + public static description = 'Generate entities and migrations'; |
| 10 | + |
| 11 | + public static aliases = [Aliases.GENERATE]; |
| 12 | + |
| 13 | + public async execute(): Promise<void> { |
| 14 | + await this.serviceWorker.validateSchema(); |
| 15 | + |
| 16 | + const tasks = new Listr<{ encodedZip: string; generated: string }>([ |
| 17 | + { |
| 18 | + title: 'Validating schema', |
| 19 | + task: async (): Promise<void> => { |
| 20 | + await this.serviceWorker.validateSchema(); |
| 21 | + }, |
| 22 | + }, |
| 23 | + { |
| 24 | + title: 'Preparing the service code for upload', |
| 25 | + task: async (ctx): Promise<void> => { |
| 26 | + ctx.encodedZip = await FileWorker.zipFolder(); |
| 27 | + }, |
| 28 | + }, |
| 29 | + { |
| 30 | + title: 'Uploading service', |
| 31 | + task: async (ctx): Promise<void> => { |
| 32 | + const { encodedZip } = ctx; |
| 33 | + ctx.generated = await this.codestore.Service.generateEntities(encodedZip); |
| 34 | + }, |
| 35 | + }, |
| 36 | + { |
| 37 | + title: 'Saving generated code', |
| 38 | + task: async (ctx, task): Promise<void> => { |
| 39 | + const { generated } = ctx; |
| 40 | + const dataFolder = join(process.cwd(), 'src', 'data'); |
| 41 | + await PromisifiedFs.rimraf(join(dataFolder, 'entities')); |
| 42 | + await FileWorker.saveZipFromB64(generated, dataFolder); |
| 43 | + |
| 44 | + // eslint-disable-next-line no-param-reassign |
| 45 | + task.title = 'Generated code has been saved'; |
| 46 | + }, |
| 47 | + }, |
| 48 | + ]); |
| 49 | + |
| 50 | + await tasks.run(); |
| 51 | + } |
| 52 | +} |
0 commit comments