MCPcopy Create free account
hub / github.com/Effect-TS/effect / parseDotEnvContents

Function parseDotEnvContents

packages/effect/src/ConfigProvider.ts:1060–1093  ·  view source on GitHub ↗
(lines: string)

Source from the content-addressed store, hash-verified

1058 /(?:^|^)\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?(?:$|$)/mg
1059
1060function parseDotEnvContents(lines: string): Record<string, string> {
1061 const obj: Record<string, string> = Object.create(null)
1062
1063 // Convert line breaks to same format
1064 lines = lines.replace(/\r\n?/gm, "\n")
1065
1066 let match: RegExpExecArray | null
1067 while ((match = DOT_ENV_LINE.exec(lines)) != null) {
1068 const key = match[1]
1069
1070 // Default undefined or null to empty string
1071 let value = match[2] || ""
1072
1073 // Remove whitespace
1074 value = value.trim()
1075
1076 // Check if double quoted
1077 const maybeQuote = value[0]
1078
1079 // Remove surrounding quotes
1080 value = value.replace(/^(['"`])([\s\S]*)\1$/gm, "$2")
1081
1082 // Expand newlines if double quoted
1083 if (maybeQuote === "\"") {
1084 value = value.replace(/\\n/g, "\n")
1085 value = value.replace(/\\r/g, "\r")
1086 }
1087
1088 // Add to object
1089 obj[key] = value
1090 }
1091
1092 return obj
1093}
1094
1095function dotEnvExpand(parsed: Record<string, string>): Record<string, string> {
1096 const newParsed: Record<string, string> = Object.create(null)

Callers 1

fromDotEnvContentsFunction · 0.85

Calls 2

execMethod · 0.80
replaceMethod · 0.45

Tested by

no test coverage detected