diff --git a/init.ts b/init.ts index 8f9d9418..fd7969ed 100644 --- a/init.ts +++ b/init.ts @@ -538,6 +538,8 @@ const DEV_TS = `#!/usr/bin/env -S deno run -A --watch=static/,routes/ import dev from "$fresh/dev.ts"; import config from "./fresh.config.ts"; +import "$std/dotenv/load.ts"; + await dev(import.meta.url, "./main.ts", config); `; const DEV_TS_PATH = join(resolvedDirectory, "dev.ts"); diff --git a/tests/cli_test.ts b/tests/cli_test.ts index e34f051b..0d34eb7f 100644 --- a/tests/cli_test.ts +++ b/tests/cli_test.ts @@ -12,7 +12,9 @@ import { retry, } from "./deps.ts"; import { + assertTextMany, clickWhenListenerReady, + fetchHtml, startFreshServer, waitForText, } from "./test_utils.ts"; @@ -367,6 +369,84 @@ Deno.test({ sanitizeResources: false, }); +Deno.test({ + name: "fresh-init loads env variables", + async fn(t) { + // Preparation + const tmpDirName = await Deno.makeTempDir(); + + const cliProcess = new Deno.Command(Deno.execPath(), { + args: [ + "run", + "-A", + "init.ts", + tmpDirName, + "--twind", + "--vscode", + ], + stdin: "null", + stdout: "piped", + stderr: "inherit", + }); + + await cliProcess.output(); + + // Add .env file + await Deno.writeTextFile(path.join(tmpDirName, ".env"), "FOO=true\n"); + await Deno.writeTextFile( + path.join(tmpDirName, "routes", "env.tsx"), + `export default function Page() { return

{Deno.env.get("FOO")}

}`, + ); + + await t.step("start up the server", async () => { + const { serverProcess, lines, address } = await startFreshServer({ + args: ["run", "-A", "--no-check", "dev.ts"], + cwd: tmpDirName, + }); + + const doc = await fetchHtml(`${address}/env`); + assertTextMany(doc, "h1", ["true"]); + + await lines.cancel(); + serverProcess.kill("SIGTERM"); + await serverProcess.status; + }); + + await t.step("build code and start server again", async () => { + await new Deno.Command(Deno.execPath(), { + args: [ + "task", + "build", + ], + cwd: tmpDirName, + stdin: "null", + stdout: "piped", + stderr: "inherit", + }).output(); + + const { serverProcess, lines, address, output } = await startFreshServer({ + args: ["run", "-A", "--no-check", "main.ts"], + cwd: tmpDirName, + }); + + assert( + output.find((line) => /Using snapshot found a/.test(line)), + "Snapshot message not printed", + ); + + const doc = await fetchHtml(`${address}/env`); + assertTextMany(doc, "h1", ["true"]); + + await lines.cancel(); + serverProcess.kill("SIGTERM"); + await serverProcess.status; + }); + + await retry(() => Deno.remove(tmpDirName, { recursive: true })); + }, + sanitizeResources: false, +}); + Deno.test("fresh-update", async function fn(t) { // Preparation const tmpDirName = await Deno.makeTempDir();