MCPcopy Create free account
hub / github.com/Snapchat/Valdi / readEnvFile

Function readEnvFile

valdi/vscode_debugger/src/targets/node/nodeLauncherBase.ts:457–479  ·  view source on GitHub ↗
(file: string)

Source from the content-addressed store, hash-verified

455}
456
457function readEnvFile(file: string): { [key: string]: string } {
458 if (!fs.existsSync(file)) {
459 return {};
460 }
461
462 const buffer = stripBOM(fs.readFileSync(file, 'utf8'));
463 const env: { [key: string]: string } = {};
464 for (const line of buffer.split('\n')) {
465 const r = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/);
466 if (!r) {
467 continue;
468 }
469
470 let value = r[2] || '';
471 // .env variables never overwrite existing variables (see #21169)
472 if (value.length > 0 && value.charAt(0) === '"' && value.charAt(value.length - 1) === '"') {
473 value = value.replace(/\\n/gm, '\n');
474 }
475 env[r[1]] = value.replace(/(^['"]|['"]$)/g, '');
476 }
477
478 return env;
479}
480
481function stripBOM(s: string): string {
482 if (s && s[0] === '\uFEFF') {

Callers 1

getConfiguredEnvironmentFunction · 0.70

Calls 5

readFileSyncMethod · 0.80
splitMethod · 0.80
stripBOMFunction · 0.70
replaceMethod · 0.65
matchMethod · 0.45

Tested by

no test coverage detected