From 7465dbd978d247e846dc8b3f605a2e04bf4a0a90 Mon Sep 17 00:00:00 2001 From: Reed von Redwitz Date: Tue, 5 Dec 2023 15:03:37 +0100 Subject: [PATCH] feat(init): task to generate manifest file (#1422) --- init.ts | 2 ++ src/dev/cli.ts | 24 +++++++++++++++++++ tests/init_test.ts | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 src/dev/cli.ts diff --git a/init.ts b/init.ts index 7988f6fd..2adb736a 100644 --- a/init.ts +++ b/init.ts @@ -554,6 +554,8 @@ const config = { tasks: { check: "deno fmt --check && deno lint && deno check **/*.ts && deno check **/*.tsx", + cli: "echo \"import '\\$fresh/src/dev/cli.ts'\" | deno run --unstable -A -", + manifest: "deno task cli manifest $(pwd)", start: "deno run -A --watch=static/,routes/ dev.ts", build: "deno run -A dev.ts build", preview: "deno run -A main.ts", diff --git a/src/dev/cli.ts b/src/dev/cli.ts new file mode 100644 index 00000000..e00352de --- /dev/null +++ b/src/dev/cli.ts @@ -0,0 +1,24 @@ +import { join, toFileUrl } from "./deps.ts"; +import { manifest } from "./mod.ts"; +import { type FreshConfig } from "../server/mod.ts"; + +const args = Deno.args; + +switch (args[0]) { + case "manifest": { + if (args[1]) { + const CONFIG_TS_PATH = join(args[1], "fresh.config.ts"); + const url = toFileUrl(CONFIG_TS_PATH).toString(); + const config: FreshConfig = (await import(url)).default; + await manifest(args[1], config?.router?.ignoreFilePattern); + } else { + console.error("Missing input for manifest command"); + Deno.exit(1); + } + break; + } + default: { + console.error("Invalid command"); + Deno.exit(1); + } +} diff --git a/tests/init_test.ts b/tests/init_test.ts index 5b0ad0b8..4b6f9be0 100644 --- a/tests/init_test.ts +++ b/tests/init_test.ts @@ -495,3 +495,61 @@ Deno.test("init - show help screen", async (t) => { assertStringIncludes(stdout, "Initialize a new Fresh project"); }); }); + +Deno.test({ + name: "regenerate manifest", + async fn(t) { + const MANIFEST_FILENAME = "fresh.gen.ts"; + const tmpDirName = await Deno.makeTempDir(); + const manifestFilePath = path.join(tmpDirName, MANIFEST_FILENAME); + + await t.step("execute init command", async () => { + const cliProcess = new Deno.Command(Deno.execPath(), { + args: [ + "run", + "-A", + "init.ts", + tmpDirName, + ], + stdin: "null", + stdout: "null", + }); + const { code } = await cliProcess.output(); + assertEquals(code, 0); + }); + + let oldManifestContent: string; + await t.step("store the contents of the manifest", async () => { + oldManifestContent = await Deno.readTextFile(manifestFilePath); + }); + + await t.step("delete the manifest", async () => { + await Deno.remove(manifestFilePath); + }); + + await t.step("regenerate the manifest", async () => { + const cliProcess = new Deno.Command(Deno.execPath(), { + args: [ + "task", + "manifest", + ], + cwd: tmpDirName, + stdin: "null", + stdout: "piped", + stderr: "piped", + }); + + const output = await cliProcess.output(); + + assertEquals(output.code, 0); + }); + + await t.step("assert the old and new contents are equal", async () => { + const newManifestContent = await Deno.readTextFile(manifestFilePath); + assertEquals(oldManifestContent, newManifestContent); + }); + + await retry(() => Deno.remove(tmpDirName, { recursive: true })); + }, + sanitizeResources: false, +});