generated from AthennaIO/Template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(commands): add make:seeder command
- Loading branch information
Showing
6 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |