Skip to content

Commit a9df281

Browse files
authored
feat: improve dev routes rendering (#160)
Co-authored-by: MarioCadenas <MarioCadenas@users.noreply.github.com>
1 parent 06dffae commit a9df281

5 files changed

Lines changed: 119 additions & 69 deletions

File tree

packages/appkit/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"express": "^4.22.0",
6565
"obug": "^2.1.1",
6666
"pg": "^8.18.0",
67+
"picocolors": "^1.1.1",
6768
"semver": "^7.7.3",
6869
"shared": "workspace:*",
6970
"vite": "npm:rolldown-vite@7.1.14",

packages/appkit/src/plugins/server/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { serverManifest } from "./manifest";
1212
import { RemoteTunnelController } from "./remote-tunnel/remote-tunnel-controller";
1313
import { StaticServer } from "./static-server";
1414
import type { ServerConfig } from "./types";
15-
import { getRoutes, type PluginEndpoints } from "./utils";
15+
import { getRoutes, type PluginEndpoints, printRoutes } from "./utils";
1616
import { ViteDevServer } from "./vite-dev-server";
1717

1818
dotenv.config({ path: path.resolve(process.cwd(), "./.env") });
@@ -124,7 +124,7 @@ export class ServerPlugin extends Plugin {
124124

125125
if (process.env.NODE_ENV === "development") {
126126
const allRoutes = getRoutes(this.serverApplication._router.stack);
127-
console.dir(allRoutes, { depth: null });
127+
printRoutes(allRoutes);
128128
}
129129
return this.serverApplication;
130130
}

packages/appkit/src/plugins/server/tests/server.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ vi.mock("node:fs", async (importOriginal) => {
156156

157157
vi.mock("../utils", () => ({
158158
getRoutes: vi.fn().mockReturnValue([]),
159+
printRoutes: vi.fn(),
159160
}));
160161

161162
import fs from "node:fs";

packages/appkit/src/plugins/server/utils.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import crypto from "node:crypto";
22
import fs from "node:fs";
33
import type http from "node:http";
44
import path from "node:path";
5+
import pc from "picocolors";
56

67
export 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-zA-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+
75120
export function getQueries(configFolder: string) {
76121
const queriesFolder = path.join(configFolder, "queries");
77122

0 commit comments

Comments
 (0)