MCPcopy
hub / github.com/toddbluhm/env-cmd / parseEnvVars

Function parseEnvVars

src/parse-env-file.ts:71–95  ·  view source on GitHub ↗
(envString: string)

Source from the content-addressed store, hash-verified

69 * Parse out all env vars from an env file string
70 */
71export function parseEnvVars(envString: string): Environment {
72 const envParseRegex = /^((.+?)[=](.*))$/gim
73 const matches: Environment = {}
74 let match
75 while ((match = envParseRegex.exec(envString)) !== null) {
76 // Note: match[1] is the full env=var line
77 const key = match[2].trim()
78 let value = match[3].trim()
79
80 // if the string is quoted, remove everything after the final
81 // quote. This implicitly removes inline comments.
82 if (value.startsWith("'") || value.startsWith('"')) {
83 value = value.slice(1, value.lastIndexOf(value[0]));
84 } else {
85 // if the string is not quoted, we need to explicitly remove
86 // inline comments.
87 value = value.split('#')[0].trim();
88 }
89
90 value = value.replace(/\\n/g, '\n');
91
92 matches[key] = value
93 }
94 return JSON.parse(JSON.stringify(matches)) as Environment
95}
96
97/**
98 * Strips out comments from env file string

Callers 2

parseEnvStringFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected