feat: co-locate islands + non-route files (#1600)

This commit is contained in:
Marvin Hagemeister
2023-08-07 20:33:08 +02:00
committed by GitHub
parent d8211dad28
commit 97c6876bf7
9 changed files with 171 additions and 67 deletions
+5 -1
View File
@@ -13,7 +13,11 @@ export {
} from "https://deno.land/std@0.193.0/path/mod.ts";
export { DAY, WEEK } from "https://deno.land/std@0.193.0/datetime/constants.ts";
export * as colors from "https://deno.land/std@0.193.0/fmt/colors.ts";
export { walk, WalkError } from "https://deno.land/std@0.193.0/fs/walk.ts";
export {
walk,
type WalkEntry,
WalkError,
} from "https://deno.land/std@0.193.0/fs/walk.ts";
export { parse } from "https://deno.land/std@0.193.0/flags/mod.ts";
export { gte } from "https://deno.land/std@0.193.0/semver/mod.ts";
export { existsSync } from "https://deno.land/std@0.193.0/fs/mod.ts";
+49 -28
View File
@@ -8,6 +8,7 @@ import {
posix,
relative,
walk,
WalkEntry,
} from "./deps.ts";
import { error } from "./error.ts";
@@ -30,18 +31,19 @@ export function ensureMinDenoVersion() {
}
}
async function collectDir(dir: string): Promise<string[]> {
async function collectDir(
dir: string,
callback: (entry: WalkEntry, dir: string) => void,
): Promise<void> {
// Check if provided path is a directory
try {
const stat = await Deno.stat(dir);
if (!stat.isDirectory) return [];
if (!stat.isDirectory) return;
} catch (err) {
if (err instanceof Deno.errors.NotFound) return [];
if (err instanceof Deno.errors.NotFound) return;
throw err;
}
const paths = [];
const fileNames = new Set<string>();
const routesFolder = walk(dir, {
includeDirs: false,
includeFiles: true,
@@ -49,21 +51,8 @@ async function collectDir(dir: string): Promise<string[]> {
});
for await (const entry of routesFolder) {
const fileNameWithoutExt = relative(dir, entry.path).split(".").slice(0, -1)
.join(".");
if (fileNames.has(fileNameWithoutExt)) {
throw new Error(
`Route conflict detected. Multiple files have the same name: ${dir}${fileNameWithoutExt}`,
);
}
fileNames.add(fileNameWithoutExt);
paths.push(relative(dir, entry.path));
callback(entry, dir);
}
paths.sort();
return paths;
}
interface Manifest {
@@ -71,12 +60,46 @@ interface Manifest {
islands: string[];
}
const GROUP_REG = /[/\\\\]\((_[^/\\\\]+)\)[/\\\\]/;
export async function collect(directory: string): Promise<Manifest> {
const [routes, islands] = await Promise.all([
collectDir(join(directory, "./routes")),
collectDir(join(directory, "./islands")),
const filePaths = new Set<string>();
const routes: string[] = [];
const islands: string[] = [];
await Promise.all([
collectDir(join(directory, "./routes"), (entry, dir) => {
const rel = join("routes", relative(dir, entry.path));
const normalized = rel.slice(0, rel.lastIndexOf("."));
// A `(_islands)` path segment is a local island folder.
// Any route path segment wrapped in `(_...)` is ignored
// during route collection.
const match = normalized.match(GROUP_REG);
if (match && match[1].startsWith("_")) {
if (match[1] === "_islands") {
console.log(rel);
islands.push(rel);
}
return;
}
if (filePaths.has(normalized)) {
throw new Error(
`Route conflict detected. Multiple files have the same name: ${dir}${normalized}`,
);
}
filePaths.add(normalized);
routes.push(rel);
}),
collectDir(join(directory, "./islands"), (entry, dir) => {
const rel = join("islands", relative(dir, entry.path));
islands.push(rel);
}),
]);
routes.sort();
islands.sort();
return { routes, islands };
}
@@ -100,14 +123,14 @@ export async function generate(directory: string, manifest: Manifest) {
${
routes.map((file, i) =>
`import * as $${i} from "${toImportSpecifier(join("routes", file))}";`
`import * as $${i} from "${toImportSpecifier(file)}";`
).join(
"\n",
)
}
${
islands.map((file, i) =>
`import * as $$${i} from "${toImportSpecifier(join("islands", file))}";`
`import * as $$${i} from "${toImportSpecifier(file)}";`
)
.join("\n")
}
@@ -116,7 +139,7 @@ const manifest = {
routes: {
${
routes.map((file, i) =>
`${JSON.stringify(`${toImportSpecifier(join("routes", file))}`)}: $${i},`
`${JSON.stringify(`${toImportSpecifier(file)}`)}: $${i},`
)
.join("\n ")
}
@@ -124,9 +147,7 @@ const manifest = {
islands: {
${
islands.map((file, i) =>
`${
JSON.stringify(`${toImportSpecifier(join("islands", file))}`)
}: $$${i},`
`${JSON.stringify(`${toImportSpecifier(file)}`)}: $$${i},`
)
.join("\n ")
}
+6 -3
View File
@@ -317,8 +317,11 @@ export class ServerContext {
if (!url.startsWith(baseUrl)) {
throw new TypeError("Island is not a child of the basepath.");
}
const path = url.substring(baseUrl.length).substring("islands".length);
const baseRoute = path.substring(1, path.length - extname(path).length);
let path = url.substring(baseUrl.length);
if (path.startsWith("islands")) {
path = path.slice("islands".length + 1);
}
const baseRoute = path.substring(0, path.length - extname(path).length);
for (const [exportName, exportedFunction] of Object.entries(module)) {
if (typeof exportedFunction !== "function") {
@@ -1154,7 +1157,7 @@ function toPascalCase(text: string): string {
}
function sanitizeIslandName(name: string): string {
const fileName = name.replaceAll("/", "_");
const fileName = name.replaceAll(/[/\\\\\(\)]/g, "_");
return toPascalCase(fileName);
}