refactor: optional directory check (#1435)

This commit is contained in:
Marvin Hagemeister
2023-07-10 14:07:03 +02:00
committed by GitHub
parent 17811dd593
commit 1dc3a992f2
+10 -17
View File
@@ -1,12 +1,4 @@
import { import { dirname, fromFileUrl, gte, join, toFileUrl, walk } from "./deps.ts";
dirname,
fromFileUrl,
gte,
join,
toFileUrl,
walk,
WalkError,
} from "./deps.ts";
import { error } from "./error.ts"; import { error } from "./error.ts";
const MIN_DENO_VERSION = "1.31.0"; const MIN_DENO_VERSION = "1.31.0";
@@ -29,6 +21,15 @@ export function ensureMinDenoVersion() {
} }
async function collectDir(dir: string): Promise<string[]> { async function collectDir(dir: string): Promise<string[]> {
// Check if provided path is a directory
try {
const stat = await Deno.stat(dir);
if (!stat.isDirectory) return [];
} catch (err) {
if (err instanceof Deno.errors.NotFound) return [];
throw err;
}
const dirUrl = toFileUrl(dir); const dirUrl = toFileUrl(dir);
const paths = []; const paths = [];
const routesFolder = walk(dir, { const routesFolder = walk(dir, {
@@ -37,20 +38,12 @@ async function collectDir(dir: string): Promise<string[]> {
exts: ["tsx", "jsx", "ts", "js"], exts: ["tsx", "jsx", "ts", "js"],
}); });
try {
for await (const entry of routesFolder) { for await (const entry of routesFolder) {
const path = toFileUrl(entry.path).href.substring( const path = toFileUrl(entry.path).href.substring(
dirUrl.href.length, dirUrl.href.length,
); );
paths.push(path); paths.push(path);
} }
} catch (err) {
if (err instanceof WalkError && err.cause instanceof Deno.errors.NotFound) {
// Do nothing.
return [];
}
throw err;
}
paths.sort(); paths.sort();
return paths; return paths;