Fix route CLI commands

This commit is contained in:
2022-01-20 00:54:55 -06:00
parent dc16dfdb81
commit 32050cb2ce
3 changed files with 84 additions and 24 deletions

View File

@@ -2,6 +2,7 @@ import {Directive, OptionDefinition} from '../Directive'
import {Inject, Injectable} from '../../di'
import {Routing} from '../../service/Routing'
import Table = require('cli-table')
import {HTTPMethod} from '../../http/lifecycle/Request'
@Injectable()
export class RouteDirective extends Directive {
@@ -32,34 +33,40 @@ export class RouteDirective extends Directive {
.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
const matched = this.routing.getCompiled()
.filter(match => {
if ( !method ) {
return match.getRoute().trim() === route
}
return (
(match.getRoute().trim() === route && match.getMethods().includes(method as HTTPMethod))
|| match.match(method as HTTPMethod, route)
)
})
.each(match => {
const pre = match.getPreflight()
.map<[string, string]>(ware => [ware.stage, this.handlerToString(ware.handler)])
.some(match => {
const displays = match.getDisplays()
.map<[string, string]>(ware => [ware.stage, ware.display])
const post = match.getMiddlewares()
.where('stage', '=', 'post')
.map<[string, string]>(ware => [ware.stage, this.handlerToString(ware.handler)])
if ( displays.isEmpty() ) {
return
}
const maxLen = match.getMiddlewares().max(ware => this.handlerToString(ware.handler).length)
const maxLen = displays.max(x => x[1].length)
const table = new Table({
head: ['Stage', 'Handler'],
colWidths: [10, Math.max(maxLen, match.getDisplayableHandler().length) + 2],
colWidths: [10, maxLen + 2],
})
table.push(...pre.toArray())
table.push(['handler', match.getDisplayableHandler()])
table.push(...post.toArray())
displays.each(x => table.push(x))
this.info(`\nRoute: ${match}\n\n${table}`)
})*/
return true
})
if ( !matched ) {
this.error('No matching routes found.')
}
}
}

View File

@@ -17,9 +17,14 @@ export class RoutesDirective extends Directive {
}
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 compiled = this.routing.getCompiled()
const maxRouteLength = compiled.strings().max('length')
const maxHandlerLength = compiled.mapCall('getHandlerDisplay')
.whereDefined()
.max('length')
const rows = compiled.map(route => [String(route), route.getHandlerDisplay()])
const table = new Table({
head: ['Route', 'Handler'],
@@ -28,6 +33,6 @@ export class RoutesDirective extends Directive {
table.push(...rows.toArray())
this.info('\n' + table)*/
this.info('\n' + table)
}
}