From 880b5b7dcc2b271aef81987f6ef91dc49b4dce56 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Sun, 15 Dec 2024 08:04:58 +0530 Subject: [PATCH] feat: display route closure listArgs when listing routes --- package.json | 2 +- providers/edge_provider.ts | 6 ++++-- src/cli_formatters/routes_list.ts | 16 ++++++++++++---- tests/cli_formatters/routes_list.spec.ts | 6 ++++++ 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index e549bf16..ec88315f 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/providers/edge_provider.ts b/providers/edge_provider.ts index 787dc44a..c3330d85 100644 --- a/providers/edge_provider.ts +++ b/providers/edge_provider.ts @@ -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)) diff --git a/src/cli_formatters/routes_list.ts b/src/cli_formatters/routes_list.ts index 0a6b199e..80fcb03f 100644 --- a/src/cli_formatters/routes_list.ts +++ b/src/cli_formatters/routes_list.ts @@ -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 } } @@ -186,6 +186,7 @@ export class RoutesListFormatter { return { type: 'closure' as const, name: handler.name || 'closure', + args: 'listArgs' in handler ? String(handler.listArgs) : undefined, } } @@ -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 } /** diff --git a/tests/cli_formatters/routes_list.spec.ts b/tests/cli_formatters/routes_list.spec.ts index 53187af2..4328aa17 100644 --- a/tests/cli_formatters/routes_list.spec.ts +++ b/tests/cli_formatters/routes_list.spec.ts @@ -126,6 +126,7 @@ test.group('Formatters | List routes | toJSON', () => { handler: { type: 'closure', name: 'closure', + args: undefined, }, middleware: [], }, @@ -136,6 +137,7 @@ test.group('Formatters | List routes | toJSON', () => { handler: { type: 'closure', name: 'closure', + args: undefined, }, middleware: [], }, @@ -270,6 +272,7 @@ test.group('Formatters | List routes | toJSON', () => { handler: { type: 'closure', name: 'closure', + args: undefined, }, middleware: [], }, @@ -280,6 +283,7 @@ test.group('Formatters | List routes | toJSON', () => { handler: { type: 'closure', name: 'closure', + args: undefined, }, middleware: [], }, @@ -616,6 +620,7 @@ test.group('Formatters | List routes | filters', () => { handler: { type: 'closure', name: 'closure', + args: undefined, }, middleware: [], }, @@ -626,6 +631,7 @@ test.group('Formatters | List routes | filters', () => { handler: { type: 'closure', name: 'closure', + args: undefined, }, middleware: [], },