diff --git a/commands/make/factory.ts b/commands/make/factory.ts new file mode 100644 index 0000000..c90daf1 --- /dev/null +++ b/commands/make/factory.ts @@ -0,0 +1,31 @@ +import { flags } from '@adonisjs/core/ace' +import { stubsRoot } from '../../stubs/main.js' +import MakeFactory from '@adonisjs/lucid/commands/make_factory' +import { COMMAND_PREFIX, MODULE_FLAG } from '../../src/constants.js' +import { checkModule } from '../../src/utils.js' + +/** + * Command to make a new Factory + */ +export default class MMakeFactory extends MakeFactory { + @flags.string(MODULE_FLAG) + declare module: string + + override async run() { + if (!this.module) { + return super.run() + } + + if (!checkModule(this.app, this.module)) { + this.kernel.exec(`${COMMAND_PREFIX}:module`, [this.module]) + } + + const codemods = await this.createCodemods() + await codemods.makeUsingStub(stubsRoot, 'make/factory/main.stub', { + flags: this.parsed.flags, + entity: this.app.generators.createEntity(this.model), + model: this.app.generators.createEntity(this.model), + pathAlias: `#${this.module}/models`, + }) + } +} diff --git a/commands/make/model.ts b/commands/make/model.ts new file mode 100644 index 0000000..3d7871c --- /dev/null +++ b/commands/make/model.ts @@ -0,0 +1,60 @@ +import { flags } from '@adonisjs/core/ace' +import { stubsRoot } from '../../stubs/main.js' +import MakeModel from '@adonisjs/lucid/commands/make_model' +import { COMMAND_PREFIX, MODULE_FLAG } from '../../src/constants.js' +import { checkModule } from '../../src/utils.js' + +export default class MMakeModel extends MakeModel { + @flags.string({ + ...MODULE_FLAG, + alias: undefined, + }) + declare module: string + + private async runMakeControllerModule() { + if (!this.controller || this.exitCode) { + return + } + + const makeController = await this.kernel.exec('make:controller', [ + this.name, + '--module', + this.module, + ]) + this.exitCode = makeController.exitCode + this.error = makeController.error + } + + private async runMakeFactoryModule() { + if (!this.factory || this.exitCode) { + return + } + + const makeFactory = await this.kernel.exec('make:factory', [this.name, '--module', this.module]) + this.exitCode = makeFactory.exitCode + this.error = makeFactory.error + } + + /** + * Execute command + */ + override async run(): Promise { + if (!this.module) { + return super.run() + } + + if (!checkModule(this.app, this.module)) { + this.kernel.exec(`${COMMAND_PREFIX}:module`, [this.module]) + } + + const codemods = await this.createCodemods() + await codemods.makeUsingStub(stubsRoot, 'make/model/main.stub', { + flags: this.parsed.flags, + entity: this.app.generators.createEntity(this.name), + }) + + await this['runMakeMigration']() + await this.runMakeControllerModule() + await this.runMakeFactoryModule() + } +} diff --git a/package.json b/package.json index 58b9b9f..7818f30 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "@adonisjs/assembler": "^7.8.2", "@adonisjs/core": "^6.12.0", "@adonisjs/eslint-config": "2.0.0-beta.7", + "@adonisjs/lucid": "^21.7.0", "@adonisjs/prettier-config": "^1.4.0", "@adonisjs/tsconfig": "^1.3.0", "@japa/assert": "^3.0.0", diff --git a/stubs/make/factory/main.stub b/stubs/make/factory/main.stub new file mode 100644 index 0000000..69d16dd --- /dev/null +++ b/stubs/make/factory/main.stub @@ -0,0 +1,19 @@ +{{#var factoryName = generators.factoryName(entity.name)}} +{{#var factoryFileName = generators.factoryFileName(entity.name)}} +{{#var modelName = generators.modelName(model.name)}} +{{#var modelFileName = generators.modelFileName(model.name)}} +{{#var modelImportPath = generators.importPath(pathAlias, model.path, modelFileName.replace(/\.ts$/, ''))}} +{{#var moduleDirectoryName = flags.module}} +{{{ + exports({ + to: app.makePath('app', moduleDirectoryName, 'factories', factoryFileName) + }) +}}} +import factory from '@adonisjs/lucid/factories' +import {{ modelName }} from '{{ modelImportPath }}' + +export const {{ factoryName }} = factory + .define({{ modelName }}, async ({ faker }) => { + return {} + }) + .build() \ No newline at end of file diff --git a/stubs/make/model/main.stub b/stubs/make/model/main.stub new file mode 100644 index 0000000..a5062d4 --- /dev/null +++ b/stubs/make/model/main.stub @@ -0,0 +1,21 @@ +{{#var modelName = generators.modelName(entity.name)}} +{{#var modelFileName = generators.modelFileName(entity.name)}} +{{#var moduleDirectoryName = flags.module}} +{{{ + exports({ + to: app.makePath('app', moduleDirectoryName, 'models', modelFileName) + }) +}}} +import { DateTime } from 'luxon' +import { BaseModel, column } from '@adonisjs/lucid/orm' + +export default class {{ modelName }} extends BaseModel { + @column({ isPrimary: true }) + declare id: number + + @column.dateTime({ autoCreate: true }) + declare createdAt: DateTime + + @column.dateTime({ autoCreate: true, autoUpdate: true }) + declare updatedAt: DateTime +} \ No newline at end of file