Skip to content

Commit

Permalink
feat(commands): add make:seeder command
Browse files Browse the repository at this point in the history
  • Loading branch information
jlenon7 committed Sep 2, 2022
1 parent f70978b commit ffc0994
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/Commands/Make/Seeder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { Path } from '@secjs/utils'
import { Artisan, Command, TemplateHelper } from '@athenna/artisan'

export class MakeSeeder extends Command {
/**
* The name and signature of the console command.
*
* @return {string}
*/
get signature() {
return 'make:seeder <name>'
}

/**
* The console command description.
*
* @return {string}
*/
get description() {
return 'Make a new seeder file.'
}

/**
* Set additional flags in the commander instance.
* This method is executed when registering your command.
*
* @param {import('commander').Command} commander
* @return {import('commander').Command}
*/
addFlags(commander) {
return commander.option(
'--no-lint',
'Do not run eslint in the command.',
true,
)
}

/**
* Execute the console command.
*
* @param {string} name
* @param {any} options
* @return {Promise<void>}
*/
async handle(name, options) {
const resource = 'Seeder'
const subPath = Path.seeders()

this.simpleLog(
`[ MAKING ${resource.toUpperCase()} ]\n`,
'rmNewLineStart',
'bold',
'green',
)

const file = await TemplateHelper.getResourceFile(name, resource, subPath)

this.success(`${resource} ({yellow} "${file.name}") successfully created.`)

if (options.lint) {
await Artisan.call(`eslint:fix ${file.path} --resource ${resource}`)
}
}
}
1 change: 1 addition & 0 deletions src/Helpers/DatabaseCommandsLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export class DatabaseCommandsLoader {
static loadCommands() {
return [
import('#src/Commands/Make/Model'),
import('#src/Commands/Make/Seeder'),
import('#src/Commands/Make/Migration'),
]
}
Expand Down
10 changes: 10 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1824,3 +1824,13 @@ export class DatabaseCommandsLoader {
*/
static loadTemplates(): any[]
}

export class Seeder {
/**
* Run the database seeders.
*
* @return {void|Promise<void>}
*/
run(): void | Promise<void>
}

1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export * from './Factories/ModelFactory.js'
export * from './Factories/DriverFactory.js'
export * from './Factories/ConnectionFactory.js'

export * from './Helpers/Seeder.js'
export * from './Helpers/DatabaseCommandsLoader.js'

export * from './Models/Model.js'
Expand Down
10 changes: 10 additions & 0 deletions templates/__name__Seeder.js.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Seeder } from '@athenna/database'

export class <%= namePascal %>Seeder extends Seeder {
/**
* Run the database seeders.
*
* @return {Promise<void>}
*/
async run() {}
}
55 changes: 55 additions & 0 deletions tests/Unit/Commands/Make/SeederTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* @athenna/artisan
*
* (c) João Lenon <lenon@athenna.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import { test } from '@japa/runner'
import { Config, File, Folder, Path } from '@secjs/utils'

import { Artisan } from '@athenna/artisan'
import { Kernel } from '#tests/Stubs/app/Console/Kernel'
import { LoggerProvider } from '@athenna/logger/providers/LoggerProvider'
import { ArtisanProvider } from '@athenna/artisan/providers/ArtisanProvider'

test.group('MakeSeederTest', group => {
group.each.setup(async () => {
await new Folder(Path.stubs('app')).copy(Path.app())
await new Folder(Path.stubs('configs')).copy(Path.config())

await new Config().safeLoad(Path.config('app.js'))
await new Config().safeLoad(Path.config('logging.js'))
await new Config().safeLoad(Path.config('database.js'))

new LoggerProvider().register()
new ArtisanProvider().register()

const kernel = new Kernel()

await kernel.registerCommands()
await kernel.registerErrorHandler()
await kernel.registerCustomTemplates()
})

group.each.teardown(async () => {
await Folder.safeRemove(Path.app())
await Folder.safeRemove(Path.config())
await Folder.safeRemove(Path.database())
})

test('should be able to create a seeder file', async ({ assert }) => {
await Artisan.call('make:seeder User')

const path = Path.seeders('UserSeeder.js')

assert.isTrue(await File.exists(path))
}).timeout(60000)

test('should throw an error when the file already exists', async ({ assert }) => {
await Artisan.call('make:seeder User')
await Artisan.call('make:seeder User')
}).timeout(60000)
})

0 comments on commit ffc0994

Please sign in to comment.