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.
Merge pull request #9 from AthennaIO/develop
feat: add make:exception command
- Loading branch information
Showing
5 changed files
with
169 additions
and
1 deletion.
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@athenna/artisan", | ||
"version": "1.0.9", | ||
"version": "1.1.0", | ||
"description": "", | ||
"license": "MIT", | ||
"author": "João Lenon <[email protected]>", | ||
|
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,91 @@ | ||
/** | ||
* @athenna/artisan | ||
* | ||
* (c) João Lenon <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { parse } from 'path' | ||
import { existsSync } from 'fs' | ||
import { File, Path } from '@secjs/utils' | ||
import { Artisan } from 'src/Facades/Artisan' | ||
import { Command } from 'src/Commands/Command' | ||
import { Commander } from 'src/Contracts/Commander' | ||
import { TemplateHelper } from 'src/Utils/TemplateHelper' | ||
|
||
export class Exception extends Command { | ||
/** | ||
* The name and signature of the console command. | ||
*/ | ||
protected signature = 'make:exception <name>' | ||
|
||
/** | ||
* The console command description. | ||
*/ | ||
protected description = 'Make a new exception file.' | ||
|
||
/** | ||
* Set additional flags in the commander instance. | ||
* This method is executed when registering your command. | ||
* | ||
* @return {void} | ||
*/ | ||
public addFlags(commander: Commander): Commander { | ||
return commander | ||
.option( | ||
'-e, --extension <extension>', | ||
'Current extension available: ts', | ||
'ts', | ||
) | ||
.option('--no-lint', 'Do not run eslint in the exception', true) | ||
} | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return {Promise<void>} | ||
*/ | ||
async handle(name: string, options: any): Promise<void> { | ||
this.simpleLog('[ MAKING EXCEPTION ]', 'rmNewLineStart', 'bold', 'green') | ||
|
||
name = TemplateHelper.normalizeName(name, 'Exception') | ||
const template = TemplateHelper.getTemplate('__name__Exception', options) | ||
|
||
if (!template) { | ||
this.error( | ||
`Template for extension ({yellow} "${options.extension}") has not been found.`, | ||
) | ||
|
||
return | ||
} | ||
|
||
const replacedName = TemplateHelper.replaceTemplateName(name, template.base) | ||
const path = Path.app(`Exceptions/${replacedName}`) | ||
const content = TemplateHelper.replaceTemplateValues( | ||
name, | ||
template.getContentSync(), | ||
) | ||
|
||
if (existsSync(path)) { | ||
this.error( | ||
`The exception ({yellow} "${ | ||
parse(path).name | ||
}") already exists. Try using another name.`, | ||
) | ||
|
||
return | ||
} | ||
|
||
const exception = await new File(path, content).create() | ||
|
||
this.success( | ||
`Exception ({yellow} "${exception.name}") successfully created.`, | ||
) | ||
|
||
if (options.lint) { | ||
await Artisan.call(`eslint:fix ${exception.path} --resource Exception`) | ||
} | ||
} | ||
} |
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,29 @@ | ||
import { Exception } from '@athenna/core' | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Exception | ||
|-------------------------------------------------------------------------- | ||
| | ||
| The Exception class imported from `@athenna/core` allows defining | ||
| a status code, error code and help for every exception. | ||
| | ||
| @example | ||
| new <%= namePascal %>Exception('message', 500, 'E_RUNTIME_EXCEPTION', 'Restart computer.') | ||
| | ||
*/ | ||
|
||
export class <%= namePascal %>Exception extends Exception { | ||
/** | ||
* Return a new instance of <%= namePascal %>Exception. | ||
* | ||
* @param message | ||
* @param statusCode | ||
* @param code | ||
* @param help | ||
* @return <%= namePascal %>Exception | ||
*/ | ||
public constructor(message?: string, statusCode?: number, code?: string, help?: string) { | ||
super(message, statusCode, code, help) | ||
} | ||
} |
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,47 @@ | ||
/** | ||
* @athenna/artisan | ||
* | ||
* (c) João Lenon <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import '@athenna/ioc' | ||
|
||
import { existsSync } from 'fs' | ||
import { Config } from '@athenna/config' | ||
import { Folder, Path } from '@secjs/utils' | ||
import { Kernel } from 'tests/Stubs/Kernel' | ||
import { Artisan } from 'src/Facades/Artisan' | ||
import { ArtisanProvider } from 'src/Providers/ArtisanProvider' | ||
import { LoggerProvider } from '@athenna/logger/src/Providers/LoggerProvider' | ||
|
||
describe('\n MakeExceptionTest', () => { | ||
beforeEach(async () => { | ||
new Folder(Path.tests('Stubs/config')).loadSync().copySync(Path.config()) | ||
|
||
await Config.load() | ||
new LoggerProvider().register() | ||
new ArtisanProvider().register() | ||
await new Kernel().registerCommands() | ||
}) | ||
|
||
it('should be able to create a exception file', async () => { | ||
await Artisan.call('make:exception TestException') | ||
|
||
const path = Path.app('Exceptions/TestException.ts') | ||
|
||
expect(existsSync(path)).toBe(true) | ||
}, 60000) | ||
|
||
it('should throw an error when the file already exists', async () => { | ||
await Artisan.call('make:exception TestException -e ts') | ||
await Artisan.call('make:exception TestException -e ts') | ||
}, 60000) | ||
|
||
afterEach(async () => { | ||
await Folder.safeRemove(Path.config()) | ||
await Folder.safeRemove(Path.app()) | ||
}) | ||
}) |