├── .env ├── .gitignore ├── LICENSE ├── README.md ├── imports ├── deps.json └── path.ts └── mod.ts /.env: -------------------------------------------------------------------------------- 1 | TEST=qwerty 2 | TEST2=wasd 3 | TEST3=1234 -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Crew Dev 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # dinoenv 2 | 3 | manage environment variables with deno. 4 | 5 | ## Usage 6 | 7 | specify the path of the `.env` file and import dino_env. 8 | 9 | install dino_env with [trex](https://deno.land/x/trex) using: 10 | 11 | ```sh 12 | $ trex install --map dinoenv 13 | ``` 14 | 15 | ```javascript 16 | import * as env from "dinoenv"; 17 | 18 | env.config(); 19 | ``` 20 | 21 | or directly with the url. 22 | 23 | ```javascript 24 | import * as env from "https://deno.land/x/dinoenv/mod.ts"; 25 | 26 | env.config(); 27 | ``` 28 | 29 | ## Config 30 | 31 | you can specify the path of the .env file. 32 | 33 | ```javascript 34 | import * as env from "https://deno.land/x/dinoenv/mod.ts"; 35 | 36 | env.config({ path: "./environment/.env" }); 37 | ``` 38 | 39 | > **note**: by default use root project path 40 | 41 | change the decode of the `.env` file 42 | 43 | ```javascript 44 | import * as env from "https://deno.land/x/dinoenv/mod.ts"; 45 | 46 | env.config({ path: "./environment/.env", encoding: "utf-8" }); 47 | ``` 48 | 49 | > **note**: by default use "utf-8" 50 | 51 | ## permissions 52 | 53 | ```sh 54 | --allow-read --allow-env 55 | ``` 56 | -------------------------------------------------------------------------------- /imports/deps.json: -------------------------------------------------------------------------------- 1 | { 2 | "meta": { 3 | "path": { 4 | "url": "https://deno.land/std/path/mod.ts", 5 | "hash": "7785d20e2d17f5b217f1f5120bc47adcde3854060e7be740bb8bd539c083020f" 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /imports/path.ts: -------------------------------------------------------------------------------- 1 | export * from "https://deno.land/std/path/mod.ts"; -------------------------------------------------------------------------------- /mod.ts: -------------------------------------------------------------------------------- 1 | /* base code from https://github.com/rubiin/deno-env */ 2 | 3 | import { join } from "./imports/path.ts"; 4 | 5 | export interface Config { 6 | path?: string; 7 | encoding?: string; 8 | } 9 | 10 | type objectGen = { 11 | [name: string]: string; 12 | }; 13 | 14 | const defaultPath = join(Deno.cwd(), ".env"); 15 | 16 | const LINE_BREAK = /\r\n|\n|\r/; 17 | const DECLARATION = /^\s*(\w+)\s*\=\s*(.*)?\s*$/; 18 | 19 | function parse(source: string) { 20 | const lines = source.split(LINE_BREAK); 21 | return lines.reduce((vars: objectGen, line: string) => { 22 | if (!DECLARATION.test(line)) return vars; 23 | 24 | const [, name, value] = DECLARATION.exec(line)!; 25 | 26 | if (!value) vars[name] = ""; 27 | else if (/^".*"$/.test(value)) 28 | vars[name] = value.replace(/^\"(.*)\"$/, "$1").replace(/\\n/g, "\n"); 29 | else vars[name] = value; 30 | 31 | return vars; 32 | }, {} as objectGen); 33 | } 34 | 35 | /** 36 | * load .env file 37 | */ 38 | export function config({ 39 | path = defaultPath, 40 | encoding = "utf-8", 41 | }: Config = {}) { 42 | const encoder = new TextDecoder(encoding); 43 | const env = Deno.readFileSync(join(Deno.cwd(), path)); 44 | const entrie = encoder.decode(env); 45 | 46 | for (const [key, value] of Object.entries(parse(entrie))) { 47 | Deno.env.set(key, value); 48 | } 49 | } 50 | --------------------------------------------------------------------------------