This repository was archived by the owner on Dec 6, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
43 lines (35 loc) · 1.15 KB
/
index.js
File metadata and controls
43 lines (35 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { parseEnv } from 'node:util';
import pkg from './package.json' with { type: 'json' };
const coerceValue = v => {
if (!v.trim()) return v;
if (v === 'true') return true;
if (v === 'false') return false;
const n = Number(v);
return !Number.isNaN(n) && String(n) === v ? n : v;
};
export const parse = (content, { coerce = true, freeze = true } = {}) => {
const raw = parseEnv(content);
const parsed = {};
for (const [k, v] of Object.entries(raw)) {
parsed[k] = coerce ? coerceValue(v) : v;
}
return freeze ? Object.freeze(parsed) : parsed;
};
export const config = ({ path = '.env', encoding = 'utf8', override = false } = {}) => {
const cwd = process.cwd();
for (const p of Array.isArray(path) ? path : [path]) {
let content;
try {
content = readFileSync(resolve(cwd, p), encoding);
} catch (err) {
throw new Error(err.message);
}
const parsed = parse(content, { coerce: false, freeze: false });
for (const [k, v] of Object.entries(parsed)) {
if (override || !(k in process.env)) process.env[k] = String(v);
}
}
};
export const version = pkg.version;