From 66142bc4d0292387fb4b8c7c010de8b8fece57fb Mon Sep 17 00:00:00 2001 From: Mate Pek Date: Fri, 28 Apr 2023 19:33:45 +0700 Subject: [PATCH] improved .env file parser --- CHANGELOG.md | 4 ++++ src/ConfigOfExecGroup.ts | 28 ++++++++++++++++------------ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 92f0d1b6..a4d4dbeb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## [Unreleased] +## [4.4.2] + +Improved .env file parser + ## [4.4.1] - 2023-04-05 ### Added diff --git a/src/ConfigOfExecGroup.ts b/src/ConfigOfExecGroup.ts index b8c46370..c7a9e056 100644 --- a/src/ConfigOfExecGroup.ts +++ b/src/ConfigOfExecGroup.ts @@ -282,20 +282,24 @@ export class ConfigOfExecGroup implements vscode.Disposable { let envFromFile: Record | undefined = undefined; if (resolvedEnvFile.absPath.endsWith('.json')) { envFromFile = readJSONSync(resolvedEnvFile.absPath); - } else if (resolvedEnvFile.absPath.endsWith('.env')) { + } else if (resolvedEnvFile.absPath.indexOf('.env') !== -1) { const content = readFileSync(resolvedEnvFile.absPath).toString(); envFromFile = {}; - content - .split(/\r?\n/) - .filter(x => x.length > 0) - .map(line => line.split('=')) - .forEach(entry => { - if (entry.length === 2) { - envFromFile![entry[0]] = entry[1]; - } else { - throw Error('Unknown line in: "' + resolvedEnvFile.absPath + '"' + entry); - } - }); + const lines = content.split(/\r?\n/).filter(x => { + const t = x.trim(); + return t.length > 0 && !t.startsWith('#') && !t.startsWith('//'); + }); + lines.forEach((line: string) => { + const eqChar = line.indexOf('='); + if (eqChar !== -1) { + let value = line.substring(eqChar + 1); + if (value.startsWith('"') || value.startsWith("'")) value = value.substring(1); + if (value.endsWith('"') || value.endsWith("'")) value = value.substring(0, value.length - 1); + envFromFile![line.substring(0, eqChar)] = line.substring(eqChar + 1); + } else { + throw Error('line missing "=" in: "' + resolvedEnvFile.absPath + '"' + line); + } + }); } else { throw Error('Unsupported file format: "' + resolvedEnvFile.absPath + '". Use only .json or .env'); }