@@ -2,6 +2,7 @@ import crypto from "node:crypto";
22import fs from "node:fs" ;
33import type http from "node:http" ;
44import path from "node:path" ;
5+ import pc from "picocolors" ;
56
67export function parseCookies (
78 req : http . IncomingMessage ,
@@ -72,6 +73,50 @@ export function getRoutes(stack: unknown[], basePath = "") {
7273 return routes ;
7374}
7475
76+ const METHOD_COLORS : Record < string , ( s : string ) => string > = {
77+ GET : pc . green ,
78+ POST : pc . blue ,
79+ PUT : pc . yellow ,
80+ PATCH : pc . yellow ,
81+ DELETE : pc . red ,
82+ HEAD : pc . magenta ,
83+ OPTIONS : pc . magenta ,
84+ } ;
85+
86+ export function printRoutes (
87+ routes : Array < { path : string ; methods : string [ ] } > ,
88+ ) {
89+ if ( routes . length === 0 ) return ;
90+
91+ const rows = routes
92+ . flatMap ( ( r ) => r . methods . map ( ( m ) => ( { method : m , path : r . path } ) ) )
93+ . sort (
94+ ( a , b ) =>
95+ a . method . localeCompare ( b . method ) || a . path . localeCompare ( b . path ) ,
96+ ) ;
97+
98+ const maxMethodLen = Math . max ( ...rows . map ( ( r ) => r . method . length ) ) ;
99+ const separator = pc . dim ( "─" . repeat ( 50 ) ) ;
100+
101+ const colorizeParams = ( p : string ) =>
102+ p . replace ( / ( : [ a - z A - Z _ ] \w * ) / g, ( match ) => pc . cyan ( match ) ) ;
103+
104+ console . log ( "" ) ;
105+ console . log (
106+ ` ${ pc . bold ( "Registered Routes" ) } ${ pc . dim ( `(${ rows . length } )` ) } ` ,
107+ ) ;
108+ console . log ( ` ${ separator } ` ) ;
109+
110+ for ( const { method, path } of rows ) {
111+ const colorize = METHOD_COLORS [ method ] || pc . white ;
112+ const methodStr = colorize ( pc . bold ( method . padEnd ( maxMethodLen ) ) ) ;
113+ console . log ( ` ${ methodStr } ${ colorizeParams ( path ) } ` ) ;
114+ }
115+
116+ console . log ( ` ${ separator } ` ) ;
117+ console . log ( "" ) ;
118+ }
119+
75120export function getQueries ( configFolder : string ) {
76121 const queriesFolder = path . join ( configFolder , "queries" ) ;
77122
0 commit comments