Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/appkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"express": "^4.22.0",
"obug": "^2.1.1",
"pg": "^8.18.0",
"picocolors": "^1.1.1",
"semver": "^7.7.3",
"shared": "workspace:*",
"vite": "npm:rolldown-vite@7.1.14",
Expand Down
4 changes: 2 additions & 2 deletions packages/appkit/src/plugins/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { serverManifest } from "./manifest";
import { RemoteTunnelController } from "./remote-tunnel/remote-tunnel-controller";
import { StaticServer } from "./static-server";
import type { ServerConfig } from "./types";
import { getRoutes, type PluginEndpoints } from "./utils";
import { getRoutes, type PluginEndpoints, printRoutes } from "./utils";
import { ViteDevServer } from "./vite-dev-server";

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

if (process.env.NODE_ENV === "development") {
const allRoutes = getRoutes(this.serverApplication._router.stack);
console.dir(allRoutes, { depth: null });
printRoutes(allRoutes);
}
return this.serverApplication;
}
Expand Down
1 change: 1 addition & 0 deletions packages/appkit/src/plugins/server/tests/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ vi.mock("node:fs", async (importOriginal) => {

vi.mock("../utils", () => ({
getRoutes: vi.fn().mockReturnValue([]),
printRoutes: vi.fn(),
}));

import fs from "node:fs";
Expand Down
45 changes: 45 additions & 0 deletions packages/appkit/src/plugins/server/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import crypto from "node:crypto";
import fs from "node:fs";
import type http from "node:http";
import path from "node:path";
import pc from "picocolors";

export function parseCookies(
req: http.IncomingMessage,
Expand Down Expand Up @@ -72,6 +73,50 @@ export function getRoutes(stack: unknown[], basePath = "") {
return routes;
}

const METHOD_COLORS: Record<string, (s: string) => string> = {
GET: pc.green,
POST: pc.blue,
PUT: pc.yellow,
PATCH: pc.yellow,
DELETE: pc.red,
HEAD: pc.magenta,
OPTIONS: pc.magenta,
};

export function printRoutes(
routes: Array<{ path: string; methods: string[] }>,
) {
if (routes.length === 0) return;

const rows = routes
.flatMap((r) => r.methods.map((m) => ({ method: m, path: r.path })))
.sort(
(a, b) =>
a.method.localeCompare(b.method) || a.path.localeCompare(b.path),
);

const maxMethodLen = Math.max(...rows.map((r) => r.method.length));
const separator = pc.dim("─".repeat(50));

const colorizeParams = (p: string) =>
p.replace(/(:[a-zA-Z_]\w*)/g, (match) => pc.cyan(match));

console.log("");
console.log(
` ${pc.bold("Registered Routes")} ${pc.dim(`(${rows.length})`)}`,
);
console.log(` ${separator}`);

for (const { method, path } of rows) {
const colorize = METHOD_COLORS[method] || pc.white;
const methodStr = colorize(pc.bold(method.padEnd(maxMethodLen)));
console.log(` ${methodStr} ${colorizeParams(path)}`);
}

console.log(` ${separator}`);
console.log("");
}

export function getQueries(configFolder: string) {
const queriesFolder = path.join(configFolder, "queries");

Expand Down
Loading