This reverts commit a1571b76ed.
It broke fresh.deno.dev :S
This commit is contained in:
+79
-102
@@ -1,4 +1,4 @@
|
||||
import { type FsAdapter, fsAdapter } from "../fs.ts";
|
||||
import type { FsAdapter } from "../fs.ts";
|
||||
import type { WalkEntry } from "@std/fs/walk";
|
||||
import type { FsRouteFileNoMod } from "./dev_build_cache.ts";
|
||||
import * as path from "@std/path";
|
||||
@@ -14,90 +14,92 @@ export async function crawlRouteDir<State>(
|
||||
routeDir: string,
|
||||
ignore: RegExp[],
|
||||
onIslandSpecifier: (spec: string) => void,
|
||||
): Promise<FsRouteFileNoMod<State>[]> {
|
||||
const files: FsRouteFileNoMod<State>[] = [];
|
||||
|
||||
await walkDir(fs, routeDir, async (entry) => {
|
||||
// A `(_islands)` path segment is a local island folder.
|
||||
// Any route path segment wrapped in `(_...)` is ignored
|
||||
// during route collection.
|
||||
const match = entry.path.match(GROUP_REG);
|
||||
if (match !== null) {
|
||||
if (match[1] === "_islands") {
|
||||
onIslandSpecifier(entry.path);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
let lazy = false;
|
||||
const relative = path.relative(routeDir, entry.path);
|
||||
const url = new URL(relative, "http://localhost/");
|
||||
const id = url.pathname.slice(0, url.pathname.lastIndexOf("."));
|
||||
|
||||
let overrideConfig: RouteConfig | undefined;
|
||||
let pattern = "*";
|
||||
let routePattern = pattern;
|
||||
let type = CommandType.Route;
|
||||
if (id.endsWith("/_middleware")) {
|
||||
type = CommandType.Middleware;
|
||||
pattern = pathToPattern(
|
||||
id.slice(1, -"/_middleware".length),
|
||||
{ keepGroups: true },
|
||||
);
|
||||
routePattern = pattern;
|
||||
} else if (id.endsWith("/_layout")) {
|
||||
type = CommandType.Layout;
|
||||
pattern = pathToPattern(
|
||||
id.slice(1, -"/_layout".length),
|
||||
{ keepGroups: true },
|
||||
);
|
||||
routePattern = pattern;
|
||||
} else if (id.endsWith("/_app")) {
|
||||
type = CommandType.App;
|
||||
} else if (id.endsWith("/_404")) {
|
||||
type = CommandType.NotFound;
|
||||
} else if (id.endsWith("/_error") || id.endsWith("/_500")) {
|
||||
type = CommandType.Error;
|
||||
pattern = pathToPattern(
|
||||
id.slice(1, -"/_error".length),
|
||||
{ keepGroups: true },
|
||||
);
|
||||
routePattern = pattern;
|
||||
} else {
|
||||
pattern = pathToPattern(id.slice(1), { keepGroups: true });
|
||||
if (id.endsWith("/index")) {
|
||||
if (!pattern.endsWith("/")) {
|
||||
pattern += "/";
|
||||
files: FsRouteFileNoMod<State>[],
|
||||
) {
|
||||
await walkDir(
|
||||
fs,
|
||||
routeDir,
|
||||
async (entry) => {
|
||||
// A `(_islands)` path segment is a local island folder.
|
||||
// Any route path segment wrapped in `(_...)` is ignored
|
||||
// during route collection.
|
||||
const match = entry.path.match(GROUP_REG);
|
||||
if (match !== null) {
|
||||
if (match[1] === "_islands") {
|
||||
onIslandSpecifier(entry.path);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
routePattern = pathToPattern(id.slice(1));
|
||||
let lazy = false;
|
||||
const relative = path.relative(routeDir, entry.path);
|
||||
const url = new URL(relative, "http://localhost/");
|
||||
const id = url.pathname.slice(0, url.pathname.lastIndexOf("."));
|
||||
|
||||
const code = await fs.readTextFile(entry.path);
|
||||
lazy = !code.includes("routeOverride");
|
||||
let overrideConfig: RouteConfig | undefined;
|
||||
let pattern = "*";
|
||||
let routePattern = pattern;
|
||||
let type = CommandType.Route;
|
||||
if (id.endsWith("/_middleware")) {
|
||||
type = CommandType.Middleware;
|
||||
pattern = pathToPattern(
|
||||
id.slice(1, -"/_middleware".length),
|
||||
{ keepGroups: true },
|
||||
);
|
||||
routePattern = pattern;
|
||||
} else if (id.endsWith("/_layout")) {
|
||||
type = CommandType.Layout;
|
||||
pattern = pathToPattern(
|
||||
id.slice(1, -"/_layout".length),
|
||||
{ keepGroups: true },
|
||||
);
|
||||
routePattern = pattern;
|
||||
} else if (id.endsWith("/_app")) {
|
||||
type = CommandType.App;
|
||||
} else if (id.endsWith("/_404")) {
|
||||
type = CommandType.NotFound;
|
||||
} else if (id.endsWith("/_error") || id.endsWith("/_500")) {
|
||||
type = CommandType.Error;
|
||||
pattern = pathToPattern(
|
||||
id.slice(1, -"/_error".length),
|
||||
{ keepGroups: true },
|
||||
);
|
||||
routePattern = pattern;
|
||||
} else {
|
||||
pattern = pathToPattern(id.slice(1), { keepGroups: true });
|
||||
if (id.endsWith("/index")) {
|
||||
if (!pattern.endsWith("/")) {
|
||||
pattern += "/";
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: We could do an AST parse here to detect the
|
||||
// kind of handler that's used to get a more accurate
|
||||
// list of methods this route supports.
|
||||
overrideConfig = {
|
||||
methods: "ALL",
|
||||
};
|
||||
}
|
||||
routePattern = pathToPattern(id.slice(1));
|
||||
|
||||
files.push({
|
||||
id,
|
||||
filePath: entry.path,
|
||||
type,
|
||||
pattern,
|
||||
routePattern,
|
||||
lazy,
|
||||
overrideConfig,
|
||||
});
|
||||
}, ignore);
|
||||
const code = await fs.readTextFile(entry.path);
|
||||
lazy = !code.includes("routeOverride");
|
||||
|
||||
// TODO: We could do an AST parse here to detect the
|
||||
// kind of handler that's used to get a more accurate
|
||||
// list of methods this route supports.
|
||||
overrideConfig = {
|
||||
methods: "ALL",
|
||||
};
|
||||
}
|
||||
|
||||
files.push({
|
||||
id,
|
||||
filePath: entry.path,
|
||||
type,
|
||||
pattern,
|
||||
routePattern,
|
||||
lazy,
|
||||
overrideConfig,
|
||||
});
|
||||
},
|
||||
ignore,
|
||||
);
|
||||
|
||||
files.sort((a, b) => sortRoutePaths(a.id, b.id));
|
||||
|
||||
return files;
|
||||
}
|
||||
|
||||
export async function walkDir(
|
||||
@@ -119,28 +121,3 @@ export async function walkDir(
|
||||
await callback(entry);
|
||||
}
|
||||
}
|
||||
|
||||
export async function crawlFsItem(
|
||||
options: { islandDir: string; routeDir: string; ignore: RegExp[] },
|
||||
): Promise<{ islands: string[]; routes: FsRouteFileNoMod<unknown>[] }> {
|
||||
const islands: string[] = [];
|
||||
|
||||
const [, routes] = await Promise.all([
|
||||
walkDir(
|
||||
fsAdapter,
|
||||
options.islandDir,
|
||||
(entry) => {
|
||||
islands.push(entry.path);
|
||||
},
|
||||
options.ignore,
|
||||
),
|
||||
crawlRouteDir(
|
||||
fsAdapter,
|
||||
options.routeDir,
|
||||
options.ignore,
|
||||
(entry) => islands.push(entry),
|
||||
),
|
||||
]);
|
||||
|
||||
return { islands, routes };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user