Skip to content

Commit

Permalink
Merge pull request #10 from AthennaIO/develop
Browse files Browse the repository at this point in the history
feat: add support for exception handling
  • Loading branch information
jlenon7 authored Apr 24, 2022
2 parents cf8c9ac + e854f34 commit cfc4236
Show file tree
Hide file tree
Showing 38 changed files with 2,249 additions and 346 deletions.
1,744 changes: 1,735 additions & 9 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/artisan",
"version": "1.1.0",
"version": "1.1.1",
"description": "",
"license": "MIT",
"author": "João Lenon <[email protected]>",
Expand Down Expand Up @@ -40,6 +40,7 @@
"husky": "3.0.9",
"jest": "27.1.0",
"lint-staged": "9.4.3",
"nodemon": "2.0.15",
"prettier": "2.0.5",
"ts-jest": "27.0.5",
"ts-loader": "9.2.3",
Expand Down
26 changes: 26 additions & 0 deletions src/Artisan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import columnify from 'columnify'
import chalkRainbow from 'chalk-rainbow'

import { Path } from '@secjs/utils'
import { Log } from '@athenna/logger'
import { Config } from '@athenna/config'
import { version } from '../package.json'
import { Command } from 'src/Commands/Command'
Expand All @@ -25,13 +26,37 @@ export class Artisan {
*/
private readonly commander: Commander

/**
* Artisan error handler.
*
* @private
*/
private errorHandler: (...args: any[]) => any | Promise<any>

/**
* Creates a new instance of Artisan
*
* @return {Artisan}
*/
public constructor() {
this.commander = new Commander()
this.setErrorHandler(error =>
Log.channel('console').error(JSON.stringify(error, null, 2)),
)
}

/**
* Set Artisan error handler.
*/
setErrorHandler(handler: (...args: any[]) => any | Promise<any>) {
this.errorHandler = handler
}

/**
* Get Artisan error handler.
*/
getErrorHandler() {
return this.errorHandler
}

/**
Expand Down Expand Up @@ -182,6 +207,7 @@ export class Artisan {
Path.switchEnvVerify()

this.setVersion(Config.get('app.version'))

await this.commander.parseAsync(process.argv)

Path.switchEnvVerify()
Expand Down
31 changes: 18 additions & 13 deletions src/Commands/List/Make.ts → src/Commands/Build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@
* file that was distributed with this source code.
*/

import { Artisan } from 'src/Facades/Artisan'
import { Path } from '@secjs/utils'
import { Command } from 'src/Commands/Command'
import { Commander } from 'src/Contracts/Commander'

export class Make extends Command {
export class Build extends Command {
/**
* The name and signature of the console command.
*/
protected signature = 'list:make'
protected signature = 'build'

/**
* The console command description.
*/
protected description = 'List all make commands.'
protected description = 'Compile project from Typescript to Javascript.'

/**
* Set additional flags in the commander instance.
Expand All @@ -37,16 +37,21 @@ export class Make extends Command {
*
* @return {Promise<void>}
*/
async handle(): Promise<void> {
this.simpleLog('[ LISTING MAKE ]', 'rmNewLineStart', 'bold', 'green')
async handle(_: any, commander: any): Promise<void> {
this.simpleLog('[ BUILDING PROJECT ]', 'rmNewLineStart', 'bold', 'green')

const commands =
'Commands:' +
Artisan.listCommands(true, 'make')
.split('\n')
.map(line => ` ${line}`)
.join('\n')
const rimrafPath = Path.noBuild().pwd('node_modules/.bin/rimraf')
let tscPath = Path.noBuild().pwd('node_modules/.bin/tsc')

this.simpleLog(commands)
const tscArgs = commander.args.join(' ')

if (tscArgs) {
tscPath = tscPath.concat(` ${tscArgs}`)
}

await this.execCommand(`${rimrafPath} dist`)
await this.execCommand(tscPath)

this.success('Built successfully.')
}
}
5 changes: 2 additions & 3 deletions src/Commands/Eslint/Fix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { parse } from 'path'
import { Path } from '@secjs/utils'
import { Command } from 'src/Commands/Command'
import { Commander } from 'src/Contracts/Commander'
import { EslintException } from 'src/Exceptions/EslintException'

export class Fix extends Command {
/**
Expand Down Expand Up @@ -67,9 +68,7 @@ export class Fix extends Command {
}
} catch (error) {
if (!options.quiet) {
this.error(
`Failed to lint ${options.resource} ({yellow} "${name}"). Please check your eslint configurations.`,
)
throw new EslintException(options.resource, name)
}
}
}
Expand Down
17 changes: 11 additions & 6 deletions src/Commands/List/Eslint.ts → src/Commands/List.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ import { Artisan } from 'src/Facades/Artisan'
import { Command } from 'src/Commands/Command'
import { Commander } from 'src/Contracts/Commander'

export class Eslint extends Command {
export class List extends Command {
/**
* The name and signature of the console command.
*/
protected signature = 'list:eslint'
protected signature = 'list <alias>'

/**
* The console command description.
*/
protected description = 'List all eslint commands.'
protected description = 'List all commands available of the alias.'

/**
* Set additional flags in the commander instance.
Expand All @@ -37,12 +37,17 @@ export class Eslint extends Command {
*
* @return {Promise<void>}
*/
async handle(): Promise<void> {
this.simpleLog('[ LISTING ESLINT ]', 'rmNewLineStart', 'bold', 'green')
async handle(alias: string): Promise<void> {
this.simpleLog(
`[ LISTING ${alias.toUpperCase()} ]`,
'rmNewLineStart',
'bold',
'green',
)

const commands =
'Commands:' +
Artisan.listCommands(true, 'eslint')
Artisan.listCommands(true, alias)
.split('\n')
.map(line => ` ${line}`)
.join('\n')
Expand Down
52 changes: 0 additions & 52 deletions src/Commands/List/Route.ts

This file was deleted.

17 changes: 4 additions & 13 deletions src/Commands/Make/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
* 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 { Commander } from 'src/Contracts/Commander'
import { TemplateHelper } from 'src/Utils/TemplateHelper'
import { Command as AbstractCommand } from 'src/Commands/Command'
import { AlreadyExistFileException } from 'src/Exceptions/AlreadyExistFileException'
import { TemplateNotFoundException } from 'src/Exceptions/TemplateNotFoundException'

export class Command extends AbstractCommand {
/**
Expand Down Expand Up @@ -59,11 +60,7 @@ export class Command extends AbstractCommand {
const template = TemplateHelper.getTemplate('__name__Command', options)

if (!template) {
this.error(
`Template for extension ({yellow} "${options.extension}") has not been found.`,
)

return
throw new TemplateNotFoundException(options.extension)
}

const replacedName = TemplateHelper.replaceTemplateName(name, template.base)
Expand All @@ -74,13 +71,7 @@ export class Command extends AbstractCommand {
)

if (existsSync(path)) {
this.error(
`The command ({yellow} "${
parse(path).name
}") already exists. Try using another name.`,
)

return
throw new AlreadyExistFileException('command', path)
}

const command = await new File(path, content).create()
Expand Down
17 changes: 4 additions & 13 deletions src/Commands/Make/Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
* 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'
import { AlreadyExistFileException } from 'src/Exceptions/AlreadyExistFileException'
import { TemplateNotFoundException } from 'src/Exceptions/TemplateNotFoundException'

export class Controller extends Command {
/**
Expand Down Expand Up @@ -54,11 +55,7 @@ export class Controller extends Command {
const template = TemplateHelper.getTemplate('__name__Controller', options)

if (!template) {
this.error(
`Template for extension ({yellow} "${options.extension}") has not been found.`,
)

return
throw new TemplateNotFoundException(options.extension)
}

const replacedName = TemplateHelper.replaceTemplateName(name, template.base)
Expand All @@ -69,13 +66,7 @@ export class Controller extends Command {
)

if (existsSync(path)) {
this.error(
`The controller ({yellow} "${
parse(path).name
}") already exists. Try using another name.`,
)

return
throw new AlreadyExistFileException('controller', path)
}

const controller = await new File(path, content).create()
Expand Down
17 changes: 4 additions & 13 deletions src/Commands/Make/Exception.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
* 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'
import { AlreadyExistFileException } from 'src/Exceptions/AlreadyExistFileException'
import { TemplateNotFoundException } from 'src/Exceptions/TemplateNotFoundException'

export class Exception extends Command {
/**
Expand Down Expand Up @@ -54,11 +55,7 @@ export class Exception extends Command {
const template = TemplateHelper.getTemplate('__name__Exception', options)

if (!template) {
this.error(
`Template for extension ({yellow} "${options.extension}") has not been found.`,
)

return
throw new TemplateNotFoundException(options.extension)
}

const replacedName = TemplateHelper.replaceTemplateName(name, template.base)
Expand All @@ -69,13 +66,7 @@ export class Exception extends Command {
)

if (existsSync(path)) {
this.error(
`The exception ({yellow} "${
parse(path).name
}") already exists. Try using another name.`,
)

return
throw new AlreadyExistFileException('exception', path)
}

const exception = await new File(path, content).create()
Expand Down
Loading

0 comments on commit cfc4236

Please sign in to comment.