Browse Source

Use deno fmt.

tags/0.1.1
Bauke 9 months ago
parent
commit
07b31f9d84
Signed by: Bauke <me@bauke.xyz> GPG Key ID: C1C0F29952BCF558
3 changed files with 14 additions and 12 deletions
  1. +1
    -2
      README.md
  2. +9
    -6
      minimal_dotenv.test.ts
  3. +4
    -4
      minimal_dotenv.ts

+ 1
- 2
README.md View File

@@ -2,8 +2,7 @@

A very minimal `.env` parser for Deno.

Requires `--allow-read` and `--allow-env` permissions
(for obvious reasons).
Requires `--allow-read` and `--allow-env` permissions (for obvious reasons).

## License



+ 9
- 6
minimal_dotenv.test.ts View File

@@ -1,13 +1,16 @@
import { assertThrowsAsync, assertEquals } from "https://deno.land/std@0.104.0/testing/asserts.ts";
import {
assertEquals,
assertThrowsAsync,
} from "https://deno.land/std@0.104.0/testing/asserts.ts";

import dotenv from './minimal_dotenv.ts';
import dotenv from "./minimal_dotenv.ts";

Deno.test('minimal dotenv works', async () => {
Deno.test("minimal dotenv works", async () => {
// Test for correctness.
await dotenv();
assertEquals(Deno.env.get('MINIMAL_DOTENV'), 'Working');
assertEquals(Deno.env.get('M1N1M4L_D0T3NV_T35T'), 'Definitely working');
assertEquals(Deno.env.get("MINIMAL_DOTENV"), "Working");
assertEquals(Deno.env.get("M1N1M4L_D0T3NV_T35T"), "Definitely working");

// Test for incorrectness.
assertThrowsAsync(async () => await dotenv('.env.wrong'));
assertThrowsAsync(async () => await dotenv(".env.wrong"));
});

+ 4
- 4
minimal_dotenv.ts View File

@@ -3,16 +3,16 @@
* environment.
* @param filename An optional file name to use instead of `.env`.
*/
export default async function minimal_dotenv(filename = '.env'): Promise<void> {
export default async function minimal_dotenv(filename = ".env"): Promise<void> {
// Read and decode the .env file.
const decoder = new TextDecoder('utf8');
const decoder = new TextDecoder("utf8");
const contents = decoder.decode(await Deno.readFile(filename));

// Split the contents by newline and only include non-zero length strings and
// lines not starting with a #.
const lines = contents
.split(/[\r\n]+/)
.filter((line) => line.length > 0 && !line.startsWith('#'));
.filter((line) => line.length > 0 && !line.startsWith("#"));

for (const line of lines) {
const matches = /^(?<key>[A-Z0-9_]+)="(?<value>.+)"$/.exec(line);
@@ -22,7 +22,7 @@ export default async function minimal_dotenv(filename = '.env'): Promise<void> {

// The `?? {}` is to make the compiler happy, if the regex matches then we
// know the capture groups will too.
const {key, value} = matches.groups ?? {};
const { key, value } = matches.groups ?? {};
Deno.env.set(key, value);
}
}