Skip to content

Commit

Permalink
feat: resolve path using path join
Browse files Browse the repository at this point in the history
  • Loading branch information
buttercubz committed Mar 4, 2021
1 parent 0a1952d commit 49633df
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode/
23 changes: 10 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,53 @@
manage environment variables with deno.

## Usage

specify the path of the `.env` file and import dino_env.

install dino_env with [Trex](https://deno.land/x/trex) using:
install dino_env with [trex](https://deno.land/x/trex) using:

``` sh
```sh
$ trex install --map dinoenv
```

``` javascript
```javascript
import * as env from "dinoenv";

env.config();
```

or directly with the url.

``` javascript

```javascript
import * as env from "https://deno.land/x/dinoenv/mod.ts";

env.config();

```

## Config

you can specify the path of the .env file.


``` javascript
```javascript
import * as env from "https://deno.land/x/dinoenv/mod.ts";

env.config({ path: "./environment/.env" });

```
> **note**: by default use root project path

> **note**: by default use root project path
change the decode of the `.env` file

``` javascript
```javascript
import * as env from "https://deno.land/x/dinoenv/mod.ts";

env.config({ path: "./environment/.env", encoding: "utf-8" });

```

> **note**: by default use "utf-8"
## permissions

``` sh
```sh
--allow-read --allow-env
```
8 changes: 8 additions & 0 deletions imports/deps.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"meta": {
"path": {
"url": "https://deno.land/std/path/mod.ts",
"hash": "7785d20e2d17f5b217f1f5120bc47adcde3854060e7be740bb8bd539c083020f"
}
}
}
1 change: 1 addition & 0 deletions imports/path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "https://deno.land/std/path/mod.ts";
20 changes: 14 additions & 6 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
/* base code from https://github.com/rubiin/deno-env */

import { join } from "./imports/path.ts";

export interface Config {
path?: string;
encoding?: string;
}

export type objectGen = {
type objectGen = {
[name: string]: string;
}
};

const defaultPath = Deno.cwd() + "/.env";
const defaultPath = join(Deno.cwd(), ".env");

const LINE_BREAK = /\r\n|\n|\r/;
const DECLARATION = /^\s*(\w+)\s*\=\s*(.*)?\s*$/;
Expand All @@ -30,12 +32,18 @@ function parse(source: string) {
}, {} as objectGen);
}

export function config({ path = defaultPath, encoding = "utf-8" }: Config = {}) {
/**
* load .env file
*/
export function config({
path = defaultPath,
encoding = "utf-8",
}: Config = {}) {
const encoder = new TextDecoder(encoding);
const env = Deno.readFileSync(path);
const env = Deno.readFileSync(join(Deno.cwd(), path));
const entrie = encoder.decode(env);

for (const [key, value] of Object.entries(parse(entrie))) {
Deno.env.set(key, value);
}
}
}

0 comments on commit 49633df

Please sign in to comment.