Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions commands/make/factory.ts
Original file line number Diff line number Diff line change
@@ -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`,
})
}
}
60 changes: 60 additions & 0 deletions commands/make/model.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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()
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
19 changes: 19 additions & 0 deletions stubs/make/factory/main.stub
Original file line number Diff line number Diff line change
@@ -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()
21 changes: 21 additions & 0 deletions stubs/make/model/main.stub
Original file line number Diff line number Diff line change
@@ -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
}