From a49e2d04e31d6a476a21acc50acccabcffb81e69 Mon Sep 17 00:00:00 2001 From: Khang Nguyen Duy <40488299+iceghost@users.noreply.github.com> Date: Wed, 31 May 2023 17:02:28 +0700 Subject: [PATCH] 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 --- src/dev/mod.ts | 37 +++++++++++++++- tests/cli_test.ts | 108 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 141 insertions(+), 4 deletions(-) diff --git a/src/dev/mod.ts b/src/dev/mod.ts index 871fa3e0..b3343f2d 100644 --- a/src/dev/mod.ts +++ b/src/dev/mod.ts @@ -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 { 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", diff --git a/tests/cli_test.ts b/tests/cli_test.ts index 7f9538c5..a0b17137 100644 --- a/tests/cli_test.ts +++ b/tests/cli_test.ts @@ -1,3 +1,5 @@ +import * as path from "$std/path/mod.ts"; +import { assertNotMatch } from "https://deno.land/std@0.189.0/testing/asserts.ts"; import { Status } from "../src/server/deps.ts"; import { assert, @@ -8,8 +10,6 @@ import { retry, TextLineStream, } from "./deps.ts"; -import * as path from "$std/path/mod.ts"; -import { assertNotMatch } from "https://deno.land/std@0.189.0/testing/asserts.ts"; type FileTree = { type: "file"; @@ -398,3 +398,107 @@ Deno.test("fresh-init .", async function (t) { assertEquals(code, 0); }); }); + +Deno.test({ + name: "fresh-init subdirectory", + async fn(t) { + // Preparation + const tmpDirName = await Deno.makeTempDir(); + + await Deno.mkdir(path.join(tmpDirName, "subdirectory")); + + const cliProcess = new Deno.Command(Deno.execPath(), { + args: [ + "run", + "-A", + path.join(Deno.cwd(), "init.ts"), + "subdirectory/subsubdirectory", + ], + cwd: tmpDirName, + stdin: "null", + stdout: "piped", + stderr: "inherit", + }); + + await cliProcess.output(); + + // "move deno.json and import_map.json one level up" + await Deno.rename( + path.join(tmpDirName, "subdirectory", "subsubdirectory", "deno.json"), + path.join(tmpDirName, "deno.json"), + ); + await Deno.rename( + path.join( + tmpDirName, + "subdirectory", + "subsubdirectory", + "import_map.json", + ), + path.join(tmpDirName, "import_map.json"), + ); + + const targetFileTree: FileTree[] = [ + { + "type": "directory", + "name": tmpDirName, + "contents": [ + { "type": "file", "name": "deno.json" }, + { "type": "file", "name": "import_map.json" }, + { + "type": "directory", + "name": "subdirectory", + contents: [ + { + "type": "directory", + "name": "subsubdirectory", + "contents": [ + { "type": "file", "name": "main.ts" }, + { "type": "file", "name": "dev.ts" }, + { "type": "file", "name": "fresh.gen.ts" }, + ], + }, + ], + }, + ], + }, + ]; + + await t.step("check generated files", async () => { + await assertFileExistence(targetFileTree); + }); + + await t.step("start up the server", async () => { + const serverProcess = new Deno.Command(Deno.execPath(), { + args: ["run", "-A", "--check", "subdirectory/subsubdirectory/dev.ts"], + stdin: "null", + stdout: "piped", + stderr: "inherit", + cwd: tmpDirName, + }).spawn(); + + const lines = serverProcess.stdout + .pipeThrough(new TextDecoderStream()) + .pipeThrough(new TextLineStream()); + + let started = false; + for await (const line of lines) { + console.log(line); + if (line.includes("Listening on http://")) { + started = true; + break; + } + } + + assert(started, "Server didn't start up"); + + await delay(100); + + await lines.cancel(); + serverProcess.kill("SIGTERM"); + await delay(100); + }); + + await retry(() => Deno.remove(tmpDirName, { recursive: true })); + }, + sanitizeResources: false, +});