-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.mjs
75 lines (70 loc) · 1.91 KB
/
utils.mjs
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
73
74
75
import * as path from "path";
import * as fs from "fs";
import { fileURLToPath } from "url";
const validEnvFiles = JSON.parse(
fs.readFileSync(
path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
"validEnvFiles.json",
),
"utf-8",
),
);
/**
* @param {string} projectRoot
* @returns {string | undefined}
*/
export function getEnvFile(projectRoot) {
for (const projectRelativeEnvFile of validEnvFiles) {
const filepath = path.resolve(projectRoot, projectRelativeEnvFile);
if (fs.existsSync(filepath)) {
return filepath;
}
}
}
/**
* @param {string} projectRoot
* @param {((key: string, value: unknown) => [string, unknown]) | undefined} transform
* @returns {Record<string, unknown>}
*/
export function loadEnv(projectRoot, transform = (key, value) => [key, value]) {
const { env } = process;
/**
* @type {Record<string, unknown>}
*/
const loaded = {};
for (const name of getReferencesInEnvFile(getEnvFile(projectRoot))) {
const [key, value] = transform(name, env[name]);
loaded[key] = value;
}
return loaded;
}
/**
* @param {string|undefined} envFile
* @returns {string[]}
*/
export function getReferencesInEnvFile(envFile) {
if (!envFile) {
return [];
}
const fileContent = fs.readFileSync(envFile, "utf-8");
const references = fileContent.matchAll(/process\.env\.([A-Z_]+)/g);
return Array.from(references).map((match) => match[1]);
}
/**
* @param {string|undefined} envFile
* @returns {{valid: false, error: unknown}|{valid: true, module: unknown}|undefined}
*/
export function validateEnv(envFile) {
if (envFile) {
try {
// Import env file to validate the env var values.
// Our convention is to parse and throw errors when invalid.
// path.relative(__dirname, envFile)
const module = require(envFile);
return { valid: true, module };
} catch (error) {
return { valid: false, error };
}
}
}