-
-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement envFile support (#837)
* Implement envFile support. * Test envfile. * Add extra test for missing env file. * README and CHANGELOG.
- Loading branch information
Showing
9 changed files
with
132 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import * as fs from 'fs' | ||
import { LaunchRequestArguments } from './phpDebug' | ||
import * as dotenv from 'dotenv' | ||
|
||
/** | ||
* Returns the user-configured portion of the environment variables. | ||
*/ | ||
export function getConfiguredEnvironment(args: LaunchRequestArguments): { [key: string]: string } { | ||
if (args.envFile) { | ||
try { | ||
return merge(readEnvFile(args.envFile), args.env || {}) | ||
} catch (e) { | ||
throw new Error('Failed reading envFile') | ||
} | ||
} | ||
return args.env || {} | ||
} | ||
|
||
function readEnvFile(file: string): { [key: string]: string } { | ||
if (!fs.existsSync(file)) { | ||
return {} | ||
} | ||
const buffer = stripBOM(fs.readFileSync(file, 'utf8')) | ||
const env = dotenv.parse(Buffer.from(buffer)) | ||
return env | ||
} | ||
|
||
function stripBOM(s: string): string { | ||
if (s && s[0] === '\uFEFF') { | ||
s = s.substring(1) | ||
} | ||
return s | ||
} | ||
|
||
function merge(...vars: { [key: string]: string }[]): { [key: string]: string } { | ||
if (process.platform === 'win32') { | ||
return caseInsensitiveMerge(...vars) | ||
} | ||
return Object.assign({}, ...vars) as { [key: string]: string } | ||
} | ||
|
||
/** | ||
* Performs a case-insenstive merge of the list of objects. | ||
*/ | ||
function caseInsensitiveMerge<V>(...objs: ReadonlyArray<Readonly<{ [key: string]: V }> | undefined | null>) { | ||
if (objs.length === 0) { | ||
return {} | ||
} | ||
const out: { [key: string]: V } = {} | ||
const caseMapping: { [key: string]: string } = Object.create(null) // prototype-free object | ||
for (const obj of objs) { | ||
if (!obj) { | ||
continue | ||
} | ||
for (const key of Object.keys(obj)) { | ||
const normalized = key.toLowerCase() | ||
if (caseMapping[normalized]) { | ||
out[caseMapping[normalized]] = obj[key] | ||
} else { | ||
caseMapping[normalized] = key | ||
out[key] = obj[key] | ||
} | ||
} | ||
} | ||
return out | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { assert } from 'chai' | ||
import { describe, it } from 'mocha' | ||
import { getConfiguredEnvironment } from '../envfile' | ||
|
||
describe('EnvFile', () => { | ||
it('should work without envfile', () => { | ||
const ret = getConfiguredEnvironment({ env: { TEST: 'TEST' } }) | ||
assert.deepEqual(ret, { TEST: 'TEST' }) | ||
}) | ||
it('should work with missing envfile', () => { | ||
const ret = getConfiguredEnvironment({ env: { TEST: 'TEST' }, envFile: 'NONEXISTINGFILE' }) | ||
assert.deepEqual(ret, { TEST: 'TEST' }) | ||
}) | ||
it('should merge envfile', () => { | ||
const ret = getConfiguredEnvironment({ env: { TEST: 'TEST' }, envFile: 'testproject/envfile' }) | ||
assert.deepEqual(ret, { TEST: 'TEST', TEST1: 'VALUE1', Test2: 'Value2' }) | ||
}) | ||
;(process.platform === 'win32' ? it : it.skip)('should merge envfile on win32', () => { | ||
const ret = getConfiguredEnvironment({ env: { TEST1: 'TEST' }, envFile: 'testproject/envfile' }) | ||
assert.deepEqual(ret, { TEST1: 'TEST', Test2: 'Value2' }) | ||
}) | ||
;(process.platform === 'win32' ? it : it.skip)('should merge envfile on win32 case insensitive', () => { | ||
const ret = getConfiguredEnvironment({ env: { Test1: 'TEST' }, envFile: 'testproject/envfile' }) | ||
assert.deepEqual(ret, { TEST1: 'TEST', Test2: 'Value2' }) | ||
}) | ||
;(process.platform !== 'win32' ? it : it.skip)('should merge envfile on unix', () => { | ||
const ret = getConfiguredEnvironment({ env: { TEST1: 'TEST' }, envFile: 'testproject/envfile' }) | ||
assert.deepEqual(ret, { TEST1: 'TEST', Test2: 'Value2' }) | ||
}) | ||
;(process.platform !== 'win32' ? it : it.skip)('should merge envfile on unix case insensitive', () => { | ||
const ret = getConfiguredEnvironment({ env: { Test1: 'TEST' }, envFile: 'testproject/envfile' }) | ||
assert.deepEqual(ret, { Test1: 'TEST', TEST1: 'VALUE1', Test2: 'Value2' }) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
TEST1=VALUE1 | ||
Test2=Value2 |