Expose auth repos in context; create routes commands
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
71
src/cli/directive/RouteDirective.ts
Normal file
71
src/cli/directive/RouteDirective.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import {Directive, OptionDefinition} from '../Directive'
|
||||
import {Inject, Injectable} from '../../di'
|
||||
import {Routing} from '../../service/Routing'
|
||||
import Table = require('cli-table')
|
||||
import {RouteHandler} from '../../http/routing/Route'
|
||||
|
||||
@Injectable()
|
||||
export class RouteDirective extends Directive {
|
||||
@Inject()
|
||||
protected readonly routing!: Routing
|
||||
|
||||
getDescription(): string {
|
||||
return 'Get information about a specific route'
|
||||
}
|
||||
|
||||
getKeywords(): string | string[] {
|
||||
return ['route']
|
||||
}
|
||||
|
||||
getOptions(): OptionDefinition[] {
|
||||
return [
|
||||
'{route} | the path of the route',
|
||||
'--method -m {value} | the HTTP method of the route',
|
||||
]
|
||||
}
|
||||
|
||||
async handle(): Promise<void> {
|
||||
const method: string | undefined = this.option('method')
|
||||
?.toLowerCase()
|
||||
?.trim()
|
||||
|
||||
const route: string = this.option('route')
|
||||
.toLowerCase()
|
||||
.trim()
|
||||
|
||||
this.routing.getCompiled()
|
||||
.filter(match => match.getRoute().trim() === route && (!method || match.getMethod() === method))
|
||||
.tap(matches => {
|
||||
if ( !matches.length ) {
|
||||
this.error('No matching routes found. (Use `./ex routes` to list)')
|
||||
process.exitCode = 1
|
||||
}
|
||||
})
|
||||
.each(match => {
|
||||
const pre = match.getMiddlewares()
|
||||
.where('stage', '=', 'pre')
|
||||
.map<[string, string]>(ware => [ware.stage, this.handlerToString(ware.handler)])
|
||||
|
||||
const post = match.getMiddlewares()
|
||||
.where('stage', '=', 'post')
|
||||
.map<[string, string]>(ware => [ware.stage, this.handlerToString(ware.handler)])
|
||||
|
||||
const maxLen = match.getMiddlewares().max(ware => this.handlerToString(ware.handler).length)
|
||||
|
||||
const table = new Table({
|
||||
head: ['Stage', 'Handler'],
|
||||
colWidths: [10, Math.max(maxLen, match.getDisplayableHandler().length) + 2],
|
||||
})
|
||||
|
||||
table.push(...pre.toArray())
|
||||
table.push(['handler', match.getDisplayableHandler()])
|
||||
table.push(...post.toArray())
|
||||
|
||||
this.info(`\nRoute: ${match}\n\n${table}`)
|
||||
})
|
||||
}
|
||||
|
||||
protected handlerToString(handler: RouteHandler): string {
|
||||
return typeof handler === 'string' ? handler : '(anonymous function)'
|
||||
}
|
||||
}
|
||||
33
src/cli/directive/RoutesDirective.ts
Normal file
33
src/cli/directive/RoutesDirective.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import {Directive} from '../Directive'
|
||||
import {Inject, Injectable} from '../../di'
|
||||
import {Routing} from '../../service/Routing'
|
||||
import Table = require('cli-table')
|
||||
|
||||
@Injectable()
|
||||
export class RoutesDirective extends Directive {
|
||||
@Inject()
|
||||
protected readonly routing!: Routing
|
||||
|
||||
getDescription(): string {
|
||||
return 'List routes registered in the application'
|
||||
}
|
||||
|
||||
getKeywords(): string | string[] {
|
||||
return ['routes']
|
||||
}
|
||||
|
||||
async handle(): Promise<void> {
|
||||
const maxRouteLength = this.routing.getCompiled().max(route => String(route).length)
|
||||
const maxHandlerLength = this.routing.getCompiled().max(route => route.getDisplayableHandler().length)
|
||||
const rows = this.routing.getCompiled().map<[string, string]>(route => [String(route), route.getDisplayableHandler()])
|
||||
|
||||
const table = new Table({
|
||||
head: ['Route', 'Handler'],
|
||||
colWidths: [maxRouteLength + 2, maxHandlerLength + 2],
|
||||
})
|
||||
|
||||
table.push(...rows.toArray())
|
||||
|
||||
this.info('\n' + table)
|
||||
}
|
||||
}
|
||||
@@ -34,6 +34,7 @@ export class ShellDirective extends Directive {
|
||||
async handle(): Promise<void> {
|
||||
const state: any = {
|
||||
app: this.app(),
|
||||
lib: await import('../../index'),
|
||||
make: (target: DependencyKey, ...parameters: any[]) => this.make(target, ...parameters),
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,8 @@ import {Directive} from '../Directive'
|
||||
import {ShellDirective} from '../directive/ShellDirective'
|
||||
import {TemplateDirective} from '../directive/TemplateDirective'
|
||||
import {RunDirective} from '../directive/RunDirective'
|
||||
import {RoutesDirective} from '../directive/RoutesDirective'
|
||||
import {RouteDirective} from '../directive/RouteDirective'
|
||||
|
||||
/**
|
||||
* Unit that takes the place of the final unit in the application that handles
|
||||
@@ -42,6 +44,8 @@ export class CommandLineApplication extends Unit {
|
||||
this.cli.registerDirective(ShellDirective)
|
||||
this.cli.registerDirective(TemplateDirective)
|
||||
this.cli.registerDirective(RunDirective)
|
||||
this.cli.registerDirective(RoutesDirective)
|
||||
this.cli.registerDirective(RouteDirective)
|
||||
|
||||
const argv = process.argv.slice(2)
|
||||
const match = this.cli.getDirectives()
|
||||
|
||||
Reference in New Issue
Block a user