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 { @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 { const method: string | undefined = this.option('method') ?.toLowerCase() ?.trim() const route: string = this.option('route') .toLowerCase() .trim() 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) ) }) .some(match => { const displays = match.getDisplays() .map<[string, string]>(ware => [ware.stage, ware.display]) if ( displays.isEmpty() ) { return } const maxLen = displays.max(x => x[1].length) const table = new Table({ head: ['Stage', 'Handler'], colWidths: [10, maxLen + 2], }) displays.each(x => table.push(x)) this.info(`\nRoute: ${match}\n\n${table}`) return true }) if ( !matched ) { this.error('No matching routes found.') } } }