feat: remove hardcoded deno.json path (#1208)

`deno.json` files can now be placed in a parent directory of the file
containing `fresh.gen.ts`.

Co-authored-by: Luca Casonato <hello@lcas.dev>
This commit is contained in:
Khang Nguyen Duy
2023-05-31 12:02:28 +02:00
committed by GitHub
co-authored by Luca Casonato
parent 8fc117d000
commit a49e2d04e3
2 changed files with 141 additions and 4 deletions
+35 -2
View File
@@ -1,4 +1,12 @@
import { dirname, fromFileUrl, gte, join, toFileUrl, walk } from "./deps.ts";
import {
dirname,
fromFileUrl,
gte,
join,
relative,
toFileUrl,
walk,
} from "./deps.ts";
import { error } from "./error.ts";
const MIN_DENO_VERSION = "1.25.0";
@@ -52,6 +60,31 @@ export async function collect(directory: string): Promise<Manifest> {
return { routes, islands };
}
async function findDenoConfig(directory: string) {
let dir = directory;
while (true) {
const path = join(dir, "deno.json");
try {
await Deno.stat(path);
break;
} catch (err) {
if (!(err instanceof Deno.errors.NotFound)) {
throw err;
}
const parent = dirname(dir);
if (parent === dir) {
error(
`Could not find a deno.json file in the current directory or any parent directory.`,
);
}
dir = parent;
}
}
// convert into import path, would be cool if there is a stdlib function
return `${relative(directory, dir).replace(/\\/g, "/") || "."}/deno.json`;
}
export async function generate(directory: string, manifest: Manifest) {
const { routes, islands } = manifest;
@@ -59,7 +92,7 @@ export async function generate(directory: string, manifest: Manifest) {
// This file SHOULD be checked into source version control.
// This file is automatically updated during development when running \`dev.ts\`.
import config from "./deno.json" assert { type: "json" };
import config from "${await findDenoConfig(directory)}" assert { type: "json" };
${
routes.map((file, i) => `import * as $${i} from "./routes${file}";`).join(
"\n",