-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (64 loc) · 2.24 KB
/
index.js
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { existsSync, readFileSync, readdirSync } from "node:fs";
import { resolve } from "node:path";
/**
* Loads environment variables from a JSON file.
*
* @async
* @param {string} filePath - The path to the JSON file.
* @throws {Error} If the file is not found or an error occurs when reading and parsing the JSON.
*/
export async function loadEnvFromFile(filePath) {
const fileExists = existsSync(filePath);
if (!filePath) throw new Error(`File ${filePath} was not found!`);
try {
const fileData = await readFileSync(resolve(process.cwd(), filePath), "utf8");
const parsedJson = JSON.parse(fileData);
for (const key in parsedJson) {
const value = parsedJson[key];
process.env[key] = typeof value === "object" ? JSON.stringify(value) : String(value);
}
} catch (error) {
throw new Error(`Error loading JSON file: ${error.stack}`);
}
}
/**
* Loads environment variables from JSON files in a folder.
*
* @param {string} folderPath - The path to the folder containing the JSON files.
* @throws {Error} If an error occurs while loading the JSON files from the folder.
*/
export function loadEnvFromFolder(folderPath) {
try {
const files = readdirSync(resolve(process.cwd(), folderPath));
for (const file of files) {
if (file.endsWith(".json")) {
const filePath = resolve(process.cwd(), folderPath, file);
loadEnvFromFile(filePath);
}
}
} catch (error) {
throw new Error(`Error loading JSON files from folder: ${error.stack}`);
}
}
/**
* Parses an object or array within process.env and converts it to its respective data type.
*
* @param {string} key - The key for the environment variable to parse.
* @returns {any} The parsed value.
*/
export function parse(key) {
const value = process.env[key];
if (value === undefined) {
throw new Error(`Environment variable ${key} not found.`);
}
// Check if the value is a JSON string representation of an object or array
if (value.startsWith("{") || value.startsWith("[")) {
try {
return JSON.parse(value);
} catch (error) {
throw new Error(`Error parsing JSON for environment variable ${key}: ${error.message}`);
}
}
// If it's not an object or array, return the original value
return value;
}