loony-dotenv 0.1.0 → 0.1.2
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.
- package/lib/index.cjs +82 -0
- package/lib/index.cjs.map +1 -0
- package/lib/index.d.cts +7 -0
- package/lib/index.d.ts +7 -0
- package/{index.mjs → lib/index.js} +7 -37
- package/lib/index.js.map +1 -0
- package/package.json +25 -4
package/lib/index.cjs
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
default: () => index_default
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(index_exports);
|
|
36
|
+
var import_fs = __toESM(require("fs"), 1);
|
|
37
|
+
var import_path = __toESM(require("path"), 1);
|
|
38
|
+
function parseEnvLine(line) {
|
|
39
|
+
const trimmed = line.trim();
|
|
40
|
+
if (!trimmed || trimmed.startsWith("#")) return null;
|
|
41
|
+
let separatorIndex = -1;
|
|
42
|
+
for (let i = 0; i < trimmed.length; i++) {
|
|
43
|
+
if (trimmed[i] === "=" && trimmed[i - 1] !== "\\") {
|
|
44
|
+
separatorIndex = i;
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (separatorIndex === -1) {
|
|
49
|
+
console.warn(`[loony-dotenv] Skipping malformed line: ${trimmed}`);
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
const key = trimmed.slice(0, separatorIndex).trim();
|
|
53
|
+
let value = trimmed.slice(separatorIndex + 1).trim();
|
|
54
|
+
const commentIndex = value.indexOf(" #");
|
|
55
|
+
if (commentIndex !== -1) {
|
|
56
|
+
value = value.slice(0, commentIndex).trim();
|
|
57
|
+
}
|
|
58
|
+
if (value.startsWith('"') && value.endsWith('"') || value.startsWith("'") && value.endsWith("'")) {
|
|
59
|
+
value = value.slice(1, -1);
|
|
60
|
+
}
|
|
61
|
+
value = value.replace(/\\=/g, "=");
|
|
62
|
+
return { key, value };
|
|
63
|
+
}
|
|
64
|
+
function loadEnv(envPath = ".env") {
|
|
65
|
+
const absolutePath = import_path.default.resolve(envPath);
|
|
66
|
+
if (!import_fs.default.existsSync(absolutePath)) {
|
|
67
|
+
console.error(`[loony-dotenv] .env file not found at: ${absolutePath}`);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
const fileContents = import_fs.default.readFileSync(absolutePath, "utf8");
|
|
71
|
+
const lines = fileContents.split(/\r?\n/);
|
|
72
|
+
for (const line of lines) {
|
|
73
|
+
const parsed = parseEnvLine(line);
|
|
74
|
+
if (!parsed) continue;
|
|
75
|
+
const { key, value } = parsed;
|
|
76
|
+
if (!(key in process.env)) {
|
|
77
|
+
process.env[key] = value;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
var index_default = loadEnv;
|
|
82
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../index.ts"],"sourcesContent":["import fs from \"fs\";\nimport path from \"path\";\n\n/**\n * Safely parse a single line of KEY=VALUE format.\n * Supports quoted values and inline comments.\n * @param {string} line\n * @returns {{ key: string, value: string } | null}\n */\nfunction parseEnvLine(line: any) {\n const trimmed = line.trim();\n\n // Skip blank lines and comment lines\n if (!trimmed || trimmed.startsWith(\"#\")) return null;\n\n // Find first '=' that isn't escaped\n let separatorIndex = -1;\n for (let i = 0; i < trimmed.length; i++) {\n if (trimmed[i] === \"=\" && trimmed[i - 1] !== \"\\\\\") {\n separatorIndex = i;\n break;\n }\n }\n\n if (separatorIndex === -1) {\n console.warn(`[loony-dotenv] Skipping malformed line: ${trimmed}`);\n return null;\n }\n\n const key = trimmed.slice(0, separatorIndex).trim();\n let value = trimmed.slice(separatorIndex + 1).trim();\n\n // Remove inline comments: KEY=value # comment\n const commentIndex = value.indexOf(\" #\");\n if (commentIndex !== -1) {\n value = value.slice(0, commentIndex).trim();\n }\n\n // Remove surrounding quotes\n if (\n (value.startsWith('\"') && value.endsWith('\"')) ||\n (value.startsWith(\"'\") && value.endsWith(\"'\"))\n ) {\n value = value.slice(1, -1);\n }\n\n // Unescape escaped '=' (i.e., KEY=va\\=lue)\n value = value.replace(/\\\\=/g, \"=\");\n\n return { key, value };\n}\n\n/**\n * Loads variables from a .env file into process.env.\n * @param {string} envPath - Path to .env file (default: \".env\")\n */\nfunction loadEnv(envPath = \".env\") {\n const absolutePath = path.resolve(envPath);\n\n if (!fs.existsSync(absolutePath)) {\n console.error(`[loony-dotenv] .env file not found at: ${absolutePath}`);\n return;\n }\n\n const fileContents = fs.readFileSync(absolutePath, \"utf8\");\n const lines = fileContents.split(/\\r?\\n/);\n\n for (const line of lines) {\n const parsed = parseEnvLine(line);\n if (!parsed) continue;\n\n const { key, value } = parsed;\n\n // Do not override existing environment variables\n if (!(key in process.env)) {\n process.env[key] = value;\n }\n }\n}\n\nexport default loadEnv;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,gBAAe;AACf,kBAAiB;AAQjB,SAAS,aAAa,MAAW;AAC/B,QAAM,UAAU,KAAK,KAAK;AAG1B,MAAI,CAAC,WAAW,QAAQ,WAAW,GAAG,EAAG,QAAO;AAGhD,MAAI,iBAAiB;AACrB,WAAS,IAAI,GAAG,IAAI,QAAQ,QAAQ,KAAK;AACvC,QAAI,QAAQ,CAAC,MAAM,OAAO,QAAQ,IAAI,CAAC,MAAM,MAAM;AACjD,uBAAiB;AACjB;AAAA,IACF;AAAA,EACF;AAEA,MAAI,mBAAmB,IAAI;AACzB,YAAQ,KAAK,2CAA2C,OAAO,EAAE;AACjE,WAAO;AAAA,EACT;AAEA,QAAM,MAAM,QAAQ,MAAM,GAAG,cAAc,EAAE,KAAK;AAClD,MAAI,QAAQ,QAAQ,MAAM,iBAAiB,CAAC,EAAE,KAAK;AAGnD,QAAM,eAAe,MAAM,QAAQ,IAAI;AACvC,MAAI,iBAAiB,IAAI;AACvB,YAAQ,MAAM,MAAM,GAAG,YAAY,EAAE,KAAK;AAAA,EAC5C;AAGA,MACG,MAAM,WAAW,GAAG,KAAK,MAAM,SAAS,GAAG,KAC3C,MAAM,WAAW,GAAG,KAAK,MAAM,SAAS,GAAG,GAC5C;AACA,YAAQ,MAAM,MAAM,GAAG,EAAE;AAAA,EAC3B;AAGA,UAAQ,MAAM,QAAQ,QAAQ,GAAG;AAEjC,SAAO,EAAE,KAAK,MAAM;AACtB;AAMA,SAAS,QAAQ,UAAU,QAAQ;AACjC,QAAM,eAAe,YAAAA,QAAK,QAAQ,OAAO;AAEzC,MAAI,CAAC,UAAAC,QAAG,WAAW,YAAY,GAAG;AAChC,YAAQ,MAAM,0CAA0C,YAAY,EAAE;AACtE;AAAA,EACF;AAEA,QAAM,eAAe,UAAAA,QAAG,aAAa,cAAc,MAAM;AACzD,QAAM,QAAQ,aAAa,MAAM,OAAO;AAExC,aAAW,QAAQ,OAAO;AACxB,UAAM,SAAS,aAAa,IAAI;AAChC,QAAI,CAAC,OAAQ;AAEb,UAAM,EAAE,KAAK,MAAM,IAAI;AAGvB,QAAI,EAAE,OAAO,QAAQ,MAAM;AACzB,cAAQ,IAAI,GAAG,IAAI;AAAA,IACrB;AAAA,EACF;AACF;AAEA,IAAO,gBAAQ;","names":["path","fs"]}
|
package/lib/index.d.cts
ADDED
package/lib/index.d.ts
ADDED
|
@@ -1,19 +1,9 @@
|
|
|
1
|
+
// index.ts
|
|
1
2
|
import fs from "fs";
|
|
2
3
|
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
4
|
function parseEnvLine(line) {
|
|
11
5
|
const trimmed = line.trim();
|
|
12
|
-
|
|
13
|
-
// Skip blank lines and comment lines
|
|
14
6
|
if (!trimmed || trimmed.startsWith("#")) return null;
|
|
15
|
-
|
|
16
|
-
// Find first '=' that isn't escaped
|
|
17
7
|
let separatorIndex = -1;
|
|
18
8
|
for (let i = 0; i < trimmed.length; i++) {
|
|
19
9
|
if (trimmed[i] === "=" && trimmed[i - 1] !== "\\") {
|
|
@@ -21,61 +11,41 @@ function parseEnvLine(line) {
|
|
|
21
11
|
break;
|
|
22
12
|
}
|
|
23
13
|
}
|
|
24
|
-
|
|
25
14
|
if (separatorIndex === -1) {
|
|
26
15
|
console.warn(`[loony-dotenv] Skipping malformed line: ${trimmed}`);
|
|
27
16
|
return null;
|
|
28
17
|
}
|
|
29
|
-
|
|
30
18
|
const key = trimmed.slice(0, separatorIndex).trim();
|
|
31
19
|
let value = trimmed.slice(separatorIndex + 1).trim();
|
|
32
|
-
|
|
33
|
-
// Remove inline comments: KEY=value # comment
|
|
34
20
|
const commentIndex = value.indexOf(" #");
|
|
35
21
|
if (commentIndex !== -1) {
|
|
36
22
|
value = value.slice(0, commentIndex).trim();
|
|
37
23
|
}
|
|
38
|
-
|
|
39
|
-
// Remove surrounding quotes
|
|
40
|
-
if (
|
|
41
|
-
(value.startsWith('"') && value.endsWith('"')) ||
|
|
42
|
-
(value.startsWith("'") && value.endsWith("'"))
|
|
43
|
-
) {
|
|
24
|
+
if (value.startsWith('"') && value.endsWith('"') || value.startsWith("'") && value.endsWith("'")) {
|
|
44
25
|
value = value.slice(1, -1);
|
|
45
26
|
}
|
|
46
|
-
|
|
47
|
-
// Unescape escaped '=' (i.e., KEY=va\=lue)
|
|
48
27
|
value = value.replace(/\\=/g, "=");
|
|
49
|
-
|
|
50
28
|
return { key, value };
|
|
51
29
|
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Loads variables from a .env file into process.env.
|
|
55
|
-
* @param {string} envPath - Path to .env file (default: ".env")
|
|
56
|
-
*/
|
|
57
30
|
function loadEnv(envPath = ".env") {
|
|
58
31
|
const absolutePath = path.resolve(envPath);
|
|
59
|
-
|
|
60
32
|
if (!fs.existsSync(absolutePath)) {
|
|
61
33
|
console.error(`[loony-dotenv] .env file not found at: ${absolutePath}`);
|
|
62
34
|
return;
|
|
63
35
|
}
|
|
64
|
-
|
|
65
36
|
const fileContents = fs.readFileSync(absolutePath, "utf8");
|
|
66
37
|
const lines = fileContents.split(/\r?\n/);
|
|
67
|
-
|
|
68
38
|
for (const line of lines) {
|
|
69
39
|
const parsed = parseEnvLine(line);
|
|
70
40
|
if (!parsed) continue;
|
|
71
|
-
|
|
72
41
|
const { key, value } = parsed;
|
|
73
|
-
|
|
74
|
-
// Do not override existing environment variables
|
|
75
42
|
if (!(key in process.env)) {
|
|
76
43
|
process.env[key] = value;
|
|
77
44
|
}
|
|
78
45
|
}
|
|
79
46
|
}
|
|
80
|
-
|
|
81
|
-
export
|
|
47
|
+
var index_default = loadEnv;
|
|
48
|
+
export {
|
|
49
|
+
index_default as default
|
|
50
|
+
};
|
|
51
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../index.ts"],"sourcesContent":["import fs from \"fs\";\nimport path from \"path\";\n\n/**\n * Safely parse a single line of KEY=VALUE format.\n * Supports quoted values and inline comments.\n * @param {string} line\n * @returns {{ key: string, value: string } | null}\n */\nfunction parseEnvLine(line: any) {\n const trimmed = line.trim();\n\n // Skip blank lines and comment lines\n if (!trimmed || trimmed.startsWith(\"#\")) return null;\n\n // Find first '=' that isn't escaped\n let separatorIndex = -1;\n for (let i = 0; i < trimmed.length; i++) {\n if (trimmed[i] === \"=\" && trimmed[i - 1] !== \"\\\\\") {\n separatorIndex = i;\n break;\n }\n }\n\n if (separatorIndex === -1) {\n console.warn(`[loony-dotenv] Skipping malformed line: ${trimmed}`);\n return null;\n }\n\n const key = trimmed.slice(0, separatorIndex).trim();\n let value = trimmed.slice(separatorIndex + 1).trim();\n\n // Remove inline comments: KEY=value # comment\n const commentIndex = value.indexOf(\" #\");\n if (commentIndex !== -1) {\n value = value.slice(0, commentIndex).trim();\n }\n\n // Remove surrounding quotes\n if (\n (value.startsWith('\"') && value.endsWith('\"')) ||\n (value.startsWith(\"'\") && value.endsWith(\"'\"))\n ) {\n value = value.slice(1, -1);\n }\n\n // Unescape escaped '=' (i.e., KEY=va\\=lue)\n value = value.replace(/\\\\=/g, \"=\");\n\n return { key, value };\n}\n\n/**\n * Loads variables from a .env file into process.env.\n * @param {string} envPath - Path to .env file (default: \".env\")\n */\nfunction loadEnv(envPath = \".env\") {\n const absolutePath = path.resolve(envPath);\n\n if (!fs.existsSync(absolutePath)) {\n console.error(`[loony-dotenv] .env file not found at: ${absolutePath}`);\n return;\n }\n\n const fileContents = fs.readFileSync(absolutePath, \"utf8\");\n const lines = fileContents.split(/\\r?\\n/);\n\n for (const line of lines) {\n const parsed = parseEnvLine(line);\n if (!parsed) continue;\n\n const { key, value } = parsed;\n\n // Do not override existing environment variables\n if (!(key in process.env)) {\n process.env[key] = value;\n }\n }\n}\n\nexport default loadEnv;\n"],"mappings":";AAAA,OAAO,QAAQ;AACf,OAAO,UAAU;AAQjB,SAAS,aAAa,MAAW;AAC/B,QAAM,UAAU,KAAK,KAAK;AAG1B,MAAI,CAAC,WAAW,QAAQ,WAAW,GAAG,EAAG,QAAO;AAGhD,MAAI,iBAAiB;AACrB,WAAS,IAAI,GAAG,IAAI,QAAQ,QAAQ,KAAK;AACvC,QAAI,QAAQ,CAAC,MAAM,OAAO,QAAQ,IAAI,CAAC,MAAM,MAAM;AACjD,uBAAiB;AACjB;AAAA,IACF;AAAA,EACF;AAEA,MAAI,mBAAmB,IAAI;AACzB,YAAQ,KAAK,2CAA2C,OAAO,EAAE;AACjE,WAAO;AAAA,EACT;AAEA,QAAM,MAAM,QAAQ,MAAM,GAAG,cAAc,EAAE,KAAK;AAClD,MAAI,QAAQ,QAAQ,MAAM,iBAAiB,CAAC,EAAE,KAAK;AAGnD,QAAM,eAAe,MAAM,QAAQ,IAAI;AACvC,MAAI,iBAAiB,IAAI;AACvB,YAAQ,MAAM,MAAM,GAAG,YAAY,EAAE,KAAK;AAAA,EAC5C;AAGA,MACG,MAAM,WAAW,GAAG,KAAK,MAAM,SAAS,GAAG,KAC3C,MAAM,WAAW,GAAG,KAAK,MAAM,SAAS,GAAG,GAC5C;AACA,YAAQ,MAAM,MAAM,GAAG,EAAE;AAAA,EAC3B;AAGA,UAAQ,MAAM,QAAQ,QAAQ,GAAG;AAEjC,SAAO,EAAE,KAAK,MAAM;AACtB;AAMA,SAAS,QAAQ,UAAU,QAAQ;AACjC,QAAM,eAAe,KAAK,QAAQ,OAAO;AAEzC,MAAI,CAAC,GAAG,WAAW,YAAY,GAAG;AAChC,YAAQ,MAAM,0CAA0C,YAAY,EAAE;AACtE;AAAA,EACF;AAEA,QAAM,eAAe,GAAG,aAAa,cAAc,MAAM;AACzD,QAAM,QAAQ,aAAa,MAAM,OAAO;AAExC,aAAW,QAAQ,OAAO;AACxB,UAAM,SAAS,aAAa,IAAI;AAChC,QAAI,CAAC,OAAQ;AAEb,UAAM,EAAE,KAAK,MAAM,IAAI;AAGvB,QAAI,EAAE,OAAO,QAAQ,MAAM;AACzB,cAAQ,IAAI,GAAG,IAAI;AAAA,IACrB;AAAA,EACF;AACF;AAEA,IAAO,gBAAQ;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,14 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "loony-dotenv",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Simple environment loader that reads .env files and automatically populates process.env for Node.js applications.",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"
|
|
6
|
+
"private": false,
|
|
7
|
+
"main": "./lib/index.cjs",
|
|
8
|
+
"module": "./lib/index.js",
|
|
9
|
+
"types": "./lib/index.d.ts",
|
|
7
10
|
"exports": {
|
|
8
|
-
".":
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./lib/index.d.ts",
|
|
13
|
+
"import": "./lib/index.js",
|
|
14
|
+
"require": "./lib/index.cjs"
|
|
15
|
+
}
|
|
9
16
|
},
|
|
10
17
|
"files": [
|
|
11
|
-
"
|
|
18
|
+
"lib"
|
|
12
19
|
],
|
|
13
20
|
"keywords": [
|
|
14
21
|
"loony-env",
|
|
@@ -21,5 +28,19 @@
|
|
|
21
28
|
"repository": {
|
|
22
29
|
"type": "git",
|
|
23
30
|
"url": "https://github.com/loony-js/loony-dotenv.git"
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"clean": "rm -rf lib dist",
|
|
34
|
+
"build": "npm run clean && tsup ./index.ts --format cjs,esm --dts --out-dir lib",
|
|
35
|
+
"prepublishOnly": "npm run build"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@types/node": "^25.0.3",
|
|
39
|
+
"ts-node": "^10.9.2",
|
|
40
|
+
"tsup": "^8.5.1",
|
|
41
|
+
"typescript": "^5.9.3"
|
|
42
|
+
},
|
|
43
|
+
"engines": {
|
|
44
|
+
"node": ">=18"
|
|
24
45
|
}
|
|
25
46
|
}
|