resolves errors in filerouter dynamic import
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import Router from "./router.ts";
|
||||
import type { Handler, RouteConfigurator } from "./types.ts";
|
||||
import { isRelativePath } from "./util/isRelativePath.ts";
|
||||
|
||||
function crawl(dir: string, callback: (path: string) => void) {
|
||||
for (const entry of Deno.readDirSync(dir)) {
|
||||
@@ -27,10 +28,15 @@ export class FileRouter extends Router {
|
||||
constructor(root: string) {
|
||||
super();
|
||||
crawl(root, async (path) => {
|
||||
let relativePath = path.replace(root, "");
|
||||
let relativePath = path.replace(root, "").replace(/index\/?/, "");
|
||||
if (path.endsWith(".ts") || path.endsWith(".js")) {
|
||||
if (isRelativePath(path)) {
|
||||
path = Deno.build.os === "windows"
|
||||
? `${Deno.cwd()}\\${path.replace(/^.?.?\\/, "")}`
|
||||
: `${Deno.cwd()}/${path.replace(/^.?.?\//, "")}`;
|
||||
}
|
||||
relativePath = relativePath.replace(/\.[tj]s/, "");
|
||||
const handlers = await import(path);
|
||||
const handlers = await import("file://" + path);
|
||||
|
||||
if (handlers.default) {
|
||||
handlers.default instanceof Router
|
||||
@@ -55,6 +61,15 @@ export class FileRouter extends Router {
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
if (Deno.env.get("BEARMETAL_ROUTER_DEBUG") === "true") {
|
||||
this.route("/debug/dump").get((_ctx) => {
|
||||
console.log("Dumping routes:");
|
||||
return new Response(
|
||||
this.routes.map((r) => r.pattern.pathname).join("\n"),
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user