From ae984924e554422e3f34e32d1a246db89770636e Mon Sep 17 00:00:00 2001 From: Reed von Redwitz Date: Sat, 24 Jun 2023 13:28:05 +0200 Subject: [PATCH] fix: update script broken when using src dir (#1353) closes https://github.com/denoland/fresh/issues/1338 This is more complex than first expected. It turns out the head of main no longer has this error, because we started swallowing errors in https://github.com/denoland/fresh/pull/1327. I'm not sure this is the right implementation. It seems like we should explicitly check for the `islands` folder instead of ignoring errors. But the obvious approach (https://deno.land/std@0.192.0/fs/mod.ts?s=exists) is apparently forbidden, due to some sort of nonsense with race conditions. Well, not being able to find `islands` because it doesn't exist at all is definitely different than it being buried in a source directory, so we'll need to sort this out at some point. I suppose that's now some technical debt to deal with, and not related to this issue. The latest "error" when running `update` and using a source directory is the following: ``` deno run -A -r file:///Users/reed/code/fresh/update.ts . The manifest has been generated for 0 routes and 0 islands. ``` i.e. it just doesn't do anything. (To be fair, it does generate a `fresh.gen.ts` in the incorrect location, so I suppose it does _something_.) But of course this isn't what we want. So we'll hunt for a source directory, as defined by the precense of `main.ts`. The fix for this is relatively straightforward. The test implements the user's actions, i.e. init a project, move things, and then try to update. --- tests/cli_test.ts | 26 ++++++++++++++++++++++++++ update.ts | 19 +++++++++++++++++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/tests/cli_test.ts b/tests/cli_test.ts index 8e883473..6b777977 100644 --- a/tests/cli_test.ts +++ b/tests/cli_test.ts @@ -363,6 +363,32 @@ Deno.test("fresh-update", async function fn(t) { } }); + await t.step("execute update command src dir", async () => { + const names = [ + "components", + "islands", + "routes", + "static", + "dev.ts", + "main.ts", + "fresh.gen.ts", + ]; + try { + Deno.mkdirSync(tmpDirName + "/src"); + names.forEach((x) => { + Deno.rename(path.join(tmpDirName, x), path.join(tmpDirName, "src", x)); + }); + await updateAndVerify( + /The manifest has been generated for (?!0 routes and 0 islands)\d+ routes and \d+ islands./, + ); + } finally { + names.forEach((x) => { + Deno.rename(path.join(tmpDirName, "src", x), path.join(tmpDirName, x)); + }); + await retry(() => Deno.remove(tmpDirName + "/src", { recursive: true })); + } + }); + await t.step("execute update command (no islands directory)", async () => { await retry(() => Deno.remove(path.join(tmpDirName, "islands"), { recursive: true }) diff --git a/update.ts b/update.ts index c5b4234b..d55f97f9 100644 --- a/update.ts +++ b/update.ts @@ -1,10 +1,12 @@ import { + dirname, existsSync, join, Node, parse, Project, resolve, + walk, } from "./src/dev/deps.ts"; import { error } from "./src/dev/error.ts"; import { freshImports, twindImports } from "./src/dev/imports.ts"; @@ -32,6 +34,7 @@ if (flags._.length !== 1) { const unresolvedDirectory = Deno.args[0]; const resolvedDirectory = resolve(unresolvedDirectory); +const srcDirectory = await findSrcDirectory("main.ts", resolvedDirectory); // Update dependencies in the import map. The import map can either be embedded // in a deno.json (or .jsonc) file or be in a separate JSON file referenced with the @@ -200,5 +203,17 @@ await start(manifest, { plugins: [twindPlugin(twindConfig)] });\n`; } } -const manifest = await collect(resolvedDirectory); -await generate(resolvedDirectory, manifest); +const manifest = await collect(srcDirectory); +await generate(srcDirectory, manifest); + +async function findSrcDirectory( + fileName: string, + directory: string, +): Promise { + for await (const entry of walk(directory)) { + if (entry.isFile && entry.name === fileName) { + return dirname(entry.path); + } + } + return resolvedDirectory; +}