-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRouteListCommand.imba
More file actions
59 lines (46 loc) · 1.88 KB
/
RouteListCommand.imba
File metadata and controls
59 lines (46 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { Command } from '../Command'
import { Prop } from '@formidablejs/console'
import type Application from '../../Application'
export class RouteListCommand < Command
get signature
'route:list {--method} {--legacy}'
get description
'List all registered routes'
get props
{
method: Prop.string().multiple().nullable().description('Filter the routes by method')
legacy: Prop.boolean().default(false).description('Display routes in legacy mode')
}
# @returns {Application}
get app
self.constructor.ctx
def handle
const list = []
const methods = self.option('method') ? (Array.isArray(self.option('method')) ? self.option('method') : [self.option('method')]) : ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD', 'TRACE', 'CONNECT']
let normalizedMethods = []
for method in methods
normalizedMethods.push(method.toUpperCase!)
let routes = []
for route in self.app.routes!
if normalizedMethods.includes(route.method.toUpperCase!)
routes.push(route)
if self.option('legacy')
self.table(routes)
else
for route of routes
let color
if route.method == 'post'
color = 'green'
elif route.method == 'get'
color = 'blue'
elif route.method == 'delete'
color = 'red'
else
color = 'green'
const path = "<fg:{color}>{route.method.toUpperCase!}</fg:{color}>" + ' ' + (route.path.padStart((6 - route.method.length) + route.path.length, ' '))
const name = route.name || ''
const action = Array.isArray(route.action) ? "{route.action[0].name}@{route.action[1]}" : ''
const description = name != '' && action != '' ? name + ' › ' + action : (name != '' ? name : action != '' ? action : '')
list.push path + ' \x1b[2m' + (description == '' ? '' : ' ' + description).padStart((process.stdout.columns - path.length - 2) + ("<fg:{color}>".length * 2), "...") + '\x1b[0m'
self.write list.join("\n")
self.exit!