From 114c4dbfabca2b73b923cd7aba7be9b9d49b9777 Mon Sep 17 00:00:00 2001 From: Cem Date: Wed, 17 May 2023 02:48:39 +0300 Subject: [PATCH] dotenv option (#740) added dotenv option to read environment variables files --------- Co-authored-by: Asher Gomez --- docs/getting-started/dynamic-routes.md | 16 ++++++++-------- docs/getting-started/running-locally.md | 3 ++- init.ts | 22 +++++++++++++++++++++- src/dev/imports.ts | 5 +++++ tests/cli_test.ts | 1 + 5 files changed, 37 insertions(+), 10 deletions(-) diff --git a/docs/getting-started/dynamic-routes.md b/docs/getting-started/dynamic-routes.md index 68337c78..71686069 100644 --- a/docs/getting-started/dynamic-routes.md +++ b/docs/getting-started/dynamic-routes.md @@ -6,23 +6,23 @@ description: | The `/about` route created on the last page is pretty static. It does not matter what query or path parameters are passed to the route, it will always render the -same page. Let's create a `/:name` that will render a page with a greeting -that contains the name passed in the path. +same page. Let's create a `/:name` that will render a page with a greeting that +contains the name passed in the path. Before diving in, a quick refresher on "dynamic" routes. Dynamic routes don't just match a single static path, but rather a whole bunch of different paths -based on a pattern. For example, the `/:name` route will match the paths -`/Luca` and `/John`, but not `/Luca/John`. +based on a pattern. For example, the `/:name` route will match the paths `/Luca` +and `/John`, but not `/Luca/John`. Fresh supports dynamic routes out of the box through file system routing. To make any path segment dynamic, just put square brackets around that segment in the file name. For example the `/:name` route maps to the file name `routes/[name].tsx`. -Just like the static `/about` route, the dynamic `/:name` route will -render a page. The module must once again expose a component as a default -export. This time the component will receive the matched path segment properties -as arguments in its `props` object though. +Just like the static `/about` route, the dynamic `/:name` route will render a +page. The module must once again expose a component as a default export. This +time the component will receive the matched path segment properties as arguments +in its `props` object though. ```tsx // routes/[name].tsx diff --git a/docs/getting-started/running-locally.md b/docs/getting-started/running-locally.md index 441fee5a..4e4d3219 100644 --- a/docs/getting-started/running-locally.md +++ b/docs/getting-started/running-locally.md @@ -5,7 +5,8 @@ description: | --- The next step after scaffolding out a new project, is to actually start it. To -do this you can just `deno task start`. +do this you can just `deno task start`. Environment variables will be +automatically read from `.env`. ``` $ deno task start diff --git a/init.ts b/init.ts index e28e3100..d20d045c 100644 --- a/init.ts +++ b/init.ts @@ -1,7 +1,11 @@ import { join, parse, resolve } from "./src/dev/deps.ts"; import { error } from "./src/dev/error.ts"; import { collect, ensureMinDenoVersion, generate } from "./src/dev/mod.ts"; -import { freshImports, twindImports } from "./src/dev/imports.ts"; +import { + dotenvImports, + freshImports, + twindImports, +} from "./src/dev/imports.ts"; ensureMinDenoVersion(); @@ -84,9 +88,23 @@ if (useVSCode) { await Deno.mkdir(join(resolvedDirectory, ".vscode"), { recursive: true }); } +const GITIGNORE = `# dotenv environment variable files +.env +.env.development.local +.env.test.local +.env.production.local +.env.local +`; + +await Deno.writeTextFile( + join(resolvedDirectory, ".gitignore"), + GITIGNORE, +); + const importMap = { imports: {} as Record }; freshImports(importMap.imports); if (useTwind) twindImports(importMap.imports); +dotenvImports(importMap.imports); const IMPORT_MAP_JSON = JSON.stringify(importMap, null, 2) + "\n"; await Deno.writeTextFile( join(resolvedDirectory, "import_map.json"), @@ -250,6 +268,8 @@ let MAIN_TS = `/// /// /// +import "$std/dotenv/load.ts"; + import { start } from "$fresh/server.ts"; import manifest from "./fresh.gen.ts"; `; diff --git a/src/dev/imports.ts b/src/dev/imports.ts index c27d5a25..692bb7d5 100644 --- a/src/dev/imports.ts +++ b/src/dev/imports.ts @@ -3,6 +3,7 @@ export const RECOMMENDED_PREACT_RTS_VERSION = "5.2.6"; export const RECOMMENDED_PREACT_SIGNALS_VERSION = "1.1.3"; export const RECOMMENDED_PREACT_SIGNALS_CORE_VERSION = "1.2.3"; export const RECOMMENDED_TWIND_VERSION = "0.16.19"; +export const RECOMMENDED_STD_VERSION = "0.187.0"; export function freshImports(imports: Record) { imports["$fresh/"] = new URL("../../", import.meta.url).href; @@ -20,3 +21,7 @@ export function twindImports(imports: Record) { imports["twind"] = `https://esm.sh/twind@${RECOMMENDED_TWIND_VERSION}`; imports["twind/"] = `https://esm.sh/twind@${RECOMMENDED_TWIND_VERSION}/`; } + +export function dotenvImports(imports: Record) { + imports["$std/"] = `https://deno.land/std@${RECOMMENDED_STD_VERSION}/`; +} diff --git a/tests/cli_test.ts b/tests/cli_test.ts index 232daa2e..a3c06f0d 100644 --- a/tests/cli_test.ts +++ b/tests/cli_test.ts @@ -252,6 +252,7 @@ Deno.test({ { "type": "file", "name": "extensions.json" }, ], }, + { "type": "file", "name": ".gitignore" }, ], }, ];