Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 76 | 323x 323x 323x 323x 323x 323x 323x 323x 323x 323x 8x 8x 8x 8x 8x 31x 31x 19x 19x 19x 8x 8x 8x 7x 7x 8x 8x 19x 9x 9x 9x 19x 19x 31x 8x 8x 323x 323x 323x 323x 323x 323x 323x 471x 471x 471x 468x 468x 3x 3x 3x 3x 7x 6x 6x 7x 3x 471x 323x 323x 323x 323x 323x 323x 323x 323x 797x 797x 5x 5x 5x 797x 797x | import fs from 'fs';
import path from 'path';
/**
* Parses the content of a .env file and returns an object of key-value pairs.
* Matches dotenv behavior including single/double quotes and inline comments.
* @param {string|Buffer} src
* @returns {Object}
*/
export function parseEnv(src) {
const obj = {};
// Match standard env entries: KEY = VAL
const regex = /^\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*|:\s*)\s*(?:("|')((?:\\\2|.)*?)\2|([^#\r\n]+?))?\s*(?:#.*)?$/;
const lines = src.toString().split(/\r?\n/);
for (const line of lines) {
const match = line.match(regex);
if (match) {
const key = match[1];
let val = '';
if (match[2]) {
// Quoted value
val = match[3];
if (match[2] === '"') {
val = val.replace(/\\n/g, '\n').replace(/\\r/g, '\r');
}
// Unescape escaped quote character
val = val.replace(new RegExp(`\\\\${match[2]}`, 'g'), match[2]);
} else if (match[4]) {
// Unquoted value
val = match[4].trim();
}
obj[key] = val;
}
}
return obj;
}
/**
* Loads environment variables from the `.env` file in rootDir into process.env.
* Does not overwrite existing environment variables.
* @param {string} rootDir
*/
export function loadEnv(rootDir) {
if (!rootDir) return;
const envPath = path.join(rootDir, '.env');
if (!fs.existsSync(envPath)) {
return;
}
try {
const content = fs.readFileSync(envPath, 'utf-8');
const parsed = parseEnv(content);
for (const key of Object.keys(parsed)) {
if (process.env[key] === undefined) {
process.env[key] = parsed[key];
}
}
} catch {
// Fail silently if reading fails
}
}
/**
* Replaces process.env.AVX_PUBLIC_... occurrences in the content
* with their stringified values from process.env.
* @param {string} content
* @returns {string}
*/
export function replaceEnvVariables(content) {
if (!content) return content;
return content.replace(/process\.env\.AVX_PUBLIC_([a-zA-Z0-9_]+)/g, (match, key) => {
const fullKey = 'AVX_PUBLIC_' + key;
const val = process.env[fullKey];
return val !== undefined ? JSON.stringify(val) : 'undefined';
});
}
|