Skip to content

Commit

Permalink
feat: display route closure listArgs when listing routes
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Dec 15, 2024
1 parent ddf0c5c commit 880b5b7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
"@adonisjs/fold": "^10.1.3",
"@adonisjs/hash": "^9.0.5",
"@adonisjs/health": "^2.0.0",
"@adonisjs/http-server": "^7.3.0",
"@adonisjs/http-server": "^7.4.0",
"@adonisjs/logger": "^6.0.5",
"@adonisjs/repl": "^4.0.1",
"@antfu/install-pkg": "^0.5.0",
Expand Down
6 changes: 4 additions & 2 deletions providers/edge_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ export default class EdgeServiceProvider {
* explicit handler
*/
BriskRoute.macro('render', function (this: BriskRoute, template, data) {
return this.setHandler(({ view }) => {
function rendersTemplate({ view }: HttpContext) {
return view.render(template, data)
})
}
Object.defineProperty(rendersTemplate, 'listArgs', { value: template, writable: false })
return this.setHandler(rendersTemplate)
})

edge.use(pluginEdgeDumper(dumper))
Expand Down
16 changes: 12 additions & 4 deletions src/cli_formatters/routes_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type SerializedRoute = {
methods: string[]
middleware: string[]
handler:
| { type: 'closure'; name: string }
| { type: 'closure'; name: string; args?: string }
| { type: 'controller'; moduleNameOrPath: string; method: string }
}

Expand Down Expand Up @@ -186,6 +186,7 @@ export class RoutesListFormatter {
return {
type: 'closure' as const,
name: handler.name || 'closure',
args: 'listArgs' in handler ? String(handler.listArgs) : undefined,
}
}

Expand Down Expand Up @@ -253,9 +254,16 @@ export class RoutesListFormatter {
* Formats action name for the ansi list and table
*/
#formatAction(route: SerializedRoute) {
return route.handler.type === 'controller'
? `${this.#colors.cyan(route.handler.method)}`
: ` ${this.#colors.cyan(route.handler.name)}`
if (route.handler.type === 'controller') {
return `${this.#colors.cyan(route.handler.method)}`
}

const functionName = ` ${this.#colors.cyan(route.handler.name)}`
if (route.handler.args) {
return ` ${functionName}(${route.handler.args})`
}

return functionName
}

/**
Expand Down
6 changes: 6 additions & 0 deletions tests/cli_formatters/routes_list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ test.group('Formatters | List routes | toJSON', () => {
handler: {
type: 'closure',
name: 'closure',
args: undefined,
},
middleware: [],
},
Expand All @@ -136,6 +137,7 @@ test.group('Formatters | List routes | toJSON', () => {
handler: {
type: 'closure',
name: 'closure',
args: undefined,
},
middleware: [],
},
Expand Down Expand Up @@ -270,6 +272,7 @@ test.group('Formatters | List routes | toJSON', () => {
handler: {
type: 'closure',
name: 'closure',
args: undefined,
},
middleware: [],
},
Expand All @@ -280,6 +283,7 @@ test.group('Formatters | List routes | toJSON', () => {
handler: {
type: 'closure',
name: 'closure',
args: undefined,
},
middleware: [],
},
Expand Down Expand Up @@ -616,6 +620,7 @@ test.group('Formatters | List routes | filters', () => {
handler: {
type: 'closure',
name: 'closure',
args: undefined,
},
middleware: [],
},
Expand All @@ -626,6 +631,7 @@ test.group('Formatters | List routes | filters', () => {
handler: {
type: 'closure',
name: 'closure',
args: undefined,
},
middleware: [],
},
Expand Down

0 comments on commit 880b5b7

Please sign in to comment.