dotenv option (#740)

added dotenv option to read environment variables files

---------

Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
This commit is contained in:
Cem
2023-05-17 09:48:39 +10:00
committed by GitHub
co-authored by Asher Gomez
parent 8ff2831680
commit 114c4dbfab
5 changed files with 37 additions and 10 deletions
+8 -8
View File
@@ -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
+2 -1
View File
@@ -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
+21 -1
View File
@@ -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<string, string> };
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 = `/// <reference no-default-lib="true" />
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />
import "$std/dotenv/load.ts";
import { start } from "$fresh/server.ts";
import manifest from "./fresh.gen.ts";
`;
+5
View File
@@ -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<string, string>) {
imports["$fresh/"] = new URL("../../", import.meta.url).href;
@@ -20,3 +21,7 @@ export function twindImports(imports: Record<string, string>) {
imports["twind"] = `https://esm.sh/twind@${RECOMMENDED_TWIND_VERSION}`;
imports["twind/"] = `https://esm.sh/twind@${RECOMMENDED_TWIND_VERSION}/`;
}
export function dotenvImports(imports: Record<string, string>) {
imports["$std/"] = `https://deno.land/std@${RECOMMENDED_STD_VERSION}/`;
}
+1
View File
@@ -252,6 +252,7 @@ Deno.test({
{ "type": "file", "name": "extensions.json" },
],
},
{ "type": "file", "name": ".gitignore" },
],
},
];