Skip to content

Commit 49633df

Browse files
committed
feat: resolve path using path join
1 parent 0a1952d commit 49633df

File tree

5 files changed

+34
-19
lines changed

5 files changed

+34
-19
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.vscode/

README.md

+10-13
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,53 @@
33
manage environment variables with deno.
44

55
## Usage
6+
67
specify the path of the `.env` file and import dino_env.
78

8-
install dino_env with [Trex](https://deno.land/x/trex) using:
9+
install dino_env with [trex](https://deno.land/x/trex) using:
910

10-
``` sh
11+
```sh
1112
$ trex install --map dinoenv
1213
```
1314

14-
``` javascript
15+
```javascript
1516
import * as env from "dinoenv";
1617

1718
env.config();
1819
```
1920

2021
or directly with the url.
2122

22-
``` javascript
23-
23+
```javascript
2424
import * as env from "https://deno.land/x/dinoenv/mod.ts";
2525

2626
env.config();
27-
2827
```
2928

3029
## Config
3130

3231
you can specify the path of the .env file.
3332

34-
35-
``` javascript
33+
```javascript
3634
import * as env from "https://deno.land/x/dinoenv/mod.ts";
3735

3836
env.config({ path: "./environment/.env" });
39-
4037
```
41-
> **note**: by default use root project path
4238

39+
> **note**: by default use root project path
4340
4441
change the decode of the `.env` file
4542

46-
``` javascript
43+
```javascript
4744
import * as env from "https://deno.land/x/dinoenv/mod.ts";
4845

4946
env.config({ path: "./environment/.env", encoding: "utf-8" });
50-
5147
```
48+
5249
> **note**: by default use "utf-8"
5350
5451
## permissions
5552

56-
``` sh
53+
```sh
5754
--allow-read --allow-env
5855
```

imports/deps.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"meta": {
3+
"path": {
4+
"url": "https://deno.land/std/path/mod.ts",
5+
"hash": "7785d20e2d17f5b217f1f5120bc47adcde3854060e7be740bb8bd539c083020f"
6+
}
7+
}
8+
}

imports/path.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "https://deno.land/std/path/mod.ts";

mod.ts

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
/* base code from https://github.com/rubiin/deno-env */
22

3+
import { join } from "./imports/path.ts";
4+
35
export interface Config {
46
path?: string;
57
encoding?: string;
68
}
79

8-
export type objectGen = {
10+
type objectGen = {
911
[name: string]: string;
10-
}
12+
};
1113

12-
const defaultPath = Deno.cwd() + "/.env";
14+
const defaultPath = join(Deno.cwd(), ".env");
1315

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

33-
export function config({ path = defaultPath, encoding = "utf-8" }: Config = {}) {
35+
/**
36+
* load .env file
37+
*/
38+
export function config({
39+
path = defaultPath,
40+
encoding = "utf-8",
41+
}: Config = {}) {
3442
const encoder = new TextDecoder(encoding);
35-
const env = Deno.readFileSync(path);
43+
const env = Deno.readFileSync(join(Deno.cwd(), path));
3644
const entrie = encoder.decode(env);
3745

3846
for (const [key, value] of Object.entries(parse(entrie))) {
3947
Deno.env.set(key, value);
4048
}
41-
}
49+
}

0 commit comments

Comments
 (0)