loony-dotenv 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/LICENSE +15 -0
  2. package/README.md +117 -0
  3. package/index.mjs +81 -0
  4. package/package.json +25 -0
package/LICENSE ADDED
@@ -0,0 +1,15 @@
1
+ ISC License
2
+
3
+ Copyright (c) 2025, Sankar Boro
4
+
5
+ Permission to use, copy, modify, and/or distribute this software for any
6
+ purpose with or without fee is hereby granted, provided that the above
7
+ copyright notice and this permission notice appear in all copies.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,117 @@
1
+ # ๐Ÿ“ฆ loony-dotenv
2
+
3
+ A tiny, fast, zero-dependency utility for loading `.env` files into `process.env` โ€” clean, simple, and reliable.
4
+
5
+ ---
6
+
7
+ ## ๐Ÿš€ Features
8
+
9
+ - โšก Zero dependencies
10
+ - ๐Ÿ“ Loads `.env` files into `process.env`
11
+ - ๐Ÿงผ Handles quotes and whitespace cleanly
12
+ - ๐Ÿ›‘ Does **not** overwrite existing env variables
13
+ - ๐Ÿชถ Lightweight and minimal API
14
+ - ๐Ÿงฉ Works with both CommonJS and ESM projects
15
+
16
+ ---
17
+
18
+ ## ๐Ÿ“ฅ Installation
19
+
20
+ ```bash
21
+ npm install loony-dotenv
22
+ ```
23
+
24
+ or
25
+
26
+ ```bash
27
+ yarn add loony-dotenv
28
+ ```
29
+
30
+ ---
31
+
32
+ ## ๐Ÿ”ง Usage
33
+
34
+ ### Basic example
35
+
36
+ ```js
37
+ import loadEnv from "loony-dotenv";
38
+
39
+ loadEnv(); // loads "./.env"
40
+
41
+ // Now you can use:
42
+ console.log(process.env.MY_VARIABLE);
43
+ ```
44
+
45
+ ---
46
+
47
+ ### Custom .env file path
48
+
49
+ ```js
50
+ loadEnv("./config/custom.env");
51
+ ```
52
+
53
+ ---
54
+
55
+ ## ๐Ÿงช Example `.env` file
56
+
57
+ ```
58
+ PORT=3000
59
+ DB_HOST="localhost"
60
+ API_KEY='secret-key'
61
+ ```
62
+
63
+ ---
64
+
65
+ ## ๐Ÿ“˜ API
66
+
67
+ ### `loadEnv(envPath?: string): void`
68
+
69
+ | Parameter | Type | Default | Description |
70
+ | --------- | -------- | ------- | ----------------------- |
71
+ | `envPath` | `string` | `.env` | Path to the `.env` file |
72
+
73
+ ---
74
+
75
+ ## โš ๏ธ Behavior Notes
76
+
77
+ - Environment variables **already set** will **not** be overwritten.
78
+ - Lines beginning with `#` or empty lines are ignored.
79
+ - Quoted values (`"value"` or `'value'`) are automatically unwrapped.
80
+
81
+ ---
82
+
83
+ ## ๐Ÿ›  How It Works (Simplified)
84
+
85
+ 1. Reads the file synchronously
86
+ 2. Splits the contents line-by-line
87
+ 3. Parses `KEY=VALUE` pairs
88
+ 4. Cleans quotes and whitespace
89
+ 5. Populates `process.env`
90
+
91
+ Clean, obvious, predictable.
92
+
93
+ ---
94
+
95
+ ## ๐Ÿ—‚ Project Structure Suggestion
96
+
97
+ ```
98
+ project/
99
+ โ”œโ”€ index.mjs
100
+ โ”œโ”€ .env
101
+ โ””โ”€ package.json
102
+ ```
103
+
104
+ ---
105
+
106
+ ## ๐Ÿค Contributing
107
+
108
+ Contributions, issues, and feature requests are welcome!
109
+ Feel free to open a pull request or issue on GitHub:
110
+
111
+ ๐Ÿ‘‰ [https://github.com/loony-js/loony-dotenv](https://github.com/loony-js/loony-dotenv)
112
+
113
+ ---
114
+
115
+ ## ๐Ÿ“„ License
116
+
117
+ ISC ยฉ Sankar Boro
package/index.mjs ADDED
@@ -0,0 +1,81 @@
1
+ import fs from "fs";
2
+ import path from "path";
3
+
4
+ /**
5
+ * Safely parse a single line of KEY=VALUE format.
6
+ * Supports quoted values and inline comments.
7
+ * @param {string} line
8
+ * @returns {{ key: string, value: string } | null}
9
+ */
10
+ function parseEnvLine(line) {
11
+ const trimmed = line.trim();
12
+
13
+ // Skip blank lines and comment lines
14
+ if (!trimmed || trimmed.startsWith("#")) return null;
15
+
16
+ // Find first '=' that isn't escaped
17
+ let separatorIndex = -1;
18
+ for (let i = 0; i < trimmed.length; i++) {
19
+ if (trimmed[i] === "=" && trimmed[i - 1] !== "\\") {
20
+ separatorIndex = i;
21
+ break;
22
+ }
23
+ }
24
+
25
+ if (separatorIndex === -1) {
26
+ console.warn(`[loony-dotenv] Skipping malformed line: ${trimmed}`);
27
+ return null;
28
+ }
29
+
30
+ const key = trimmed.slice(0, separatorIndex).trim();
31
+ let value = trimmed.slice(separatorIndex + 1).trim();
32
+
33
+ // Remove inline comments: KEY=value # comment
34
+ const commentIndex = value.indexOf(" #");
35
+ if (commentIndex !== -1) {
36
+ value = value.slice(0, commentIndex).trim();
37
+ }
38
+
39
+ // Remove surrounding quotes
40
+ if (
41
+ (value.startsWith('"') && value.endsWith('"')) ||
42
+ (value.startsWith("'") && value.endsWith("'"))
43
+ ) {
44
+ value = value.slice(1, -1);
45
+ }
46
+
47
+ // Unescape escaped '=' (i.e., KEY=va\=lue)
48
+ value = value.replace(/\\=/g, "=");
49
+
50
+ return { key, value };
51
+ }
52
+
53
+ /**
54
+ * Loads variables from a .env file into process.env.
55
+ * @param {string} envPath - Path to .env file (default: ".env")
56
+ */
57
+ function loadEnv(envPath = ".env") {
58
+ const absolutePath = path.resolve(envPath);
59
+
60
+ if (!fs.existsSync(absolutePath)) {
61
+ console.error(`[loony-dotenv] .env file not found at: ${absolutePath}`);
62
+ return;
63
+ }
64
+
65
+ const fileContents = fs.readFileSync(absolutePath, "utf8");
66
+ const lines = fileContents.split(/\r?\n/);
67
+
68
+ for (const line of lines) {
69
+ const parsed = parseEnvLine(line);
70
+ if (!parsed) continue;
71
+
72
+ const { key, value } = parsed;
73
+
74
+ // Do not override existing environment variables
75
+ if (!(key in process.env)) {
76
+ process.env[key] = value;
77
+ }
78
+ }
79
+ }
80
+
81
+ export default loadEnv;
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "loony-dotenv",
3
+ "version": "0.1.0",
4
+ "description": "Simple environment loader that reads .env files and automatically populates process.env for Node.js applications.",
5
+ "type": "module",
6
+ "main": "index.mjs",
7
+ "exports": {
8
+ ".": "./index.mjs"
9
+ },
10
+ "files": [
11
+ "index.mjs"
12
+ ],
13
+ "keywords": [
14
+ "loony-env",
15
+ "loony-dotenv",
16
+ "dotenv",
17
+ "env"
18
+ ],
19
+ "author": "Sankar Boro",
20
+ "license": "ISC",
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "https://github.com/loony-js/loony-dotenv.git"
24
+ }
25
+ }