feat: island sub-directories (#1185)

This commit is contained in:
Asher Gomez
2023-05-31 05:29:56 +10:00
committed by GitHub
parent 8347eadbef
commit b5532aeb69
6 changed files with 56 additions and 66 deletions
+2
View File
@@ -4,7 +4,9 @@ export {
extname,
fromFileUrl,
join,
relative,
resolve,
SEP,
toFileUrl,
} from "https://deno.land/std@0.189.0/path/mod.ts";
export { walk } from "https://deno.land/std@0.189.0/fs/walk.ts";
+23 -62
View File
@@ -1,12 +1,4 @@
import {
dirname,
extname,
fromFileUrl,
gte,
join,
toFileUrl,
walk,
} from "./deps.ts";
import { dirname, fromFileUrl, gte, join, toFileUrl, walk } from "./deps.ts";
import { error } from "./error.ts";
const MIN_DENO_VERSION = "1.25.0";
@@ -28,65 +20,34 @@ export function ensureMinDenoVersion() {
}
}
async function collectDir(dir: string): Promise<string[]> {
const dirUrl = toFileUrl(dir);
const paths = [];
const routesFolder = walk(dir, {
includeDirs: false,
includeFiles: true,
exts: ["tsx", "jsx", "ts", "js"],
});
for await (const entry of routesFolder) {
const path = toFileUrl(entry.path).href.substring(
dirUrl.href.length,
);
paths.push(path);
}
paths.sort();
return paths;
}
interface Manifest {
routes: string[];
islands: string[];
}
export async function collect(directory: string): Promise<Manifest> {
const routesDir = join(directory, "./routes");
const islandsDir = join(directory, "./islands");
const routes = [];
try {
const routesUrl = toFileUrl(routesDir);
const routesFolder = walk(routesDir, {
includeDirs: false,
includeFiles: true,
exts: ["tsx", "jsx", "ts", "js"],
});
for await (const entry of routesFolder) {
if (entry.isFile) {
const file = toFileUrl(entry.path).href.substring(
routesUrl.href.length,
);
routes.push(file);
}
}
} catch (err) {
if (err.cause instanceof Deno.errors.NotFound) {
// Do nothing.
} else {
throw err;
}
}
routes.sort();
const islands = [];
try {
const islandsUrl = toFileUrl(islandsDir);
for await (const entry of Deno.readDir(islandsDir)) {
if (entry.isDirectory) {
error(
`Found subdirectory '${entry.name}' in islands/. The islands/ folder must not contain any subdirectories.`,
);
}
if (entry.isFile) {
const ext = extname(entry.name);
if (![".tsx", ".jsx", ".ts", ".js"].includes(ext)) continue;
const path = join(islandsDir, entry.name);
const file = toFileUrl(path).href.substring(islandsUrl.href.length);
islands.push(file);
}
}
} catch (err) {
if (err instanceof Deno.errors.NotFound) {
// Do nothing.
} else {
throw err;
}
}
islands.sort();
const [routes, islands] = await Promise.all([
collectDir(join(directory, "./routes")),
collectDir(join(directory, "./islands")),
]);
return { routes, islands };
}