From c65b9dd95a391bee33de5368b3016b437e70293c Mon Sep 17 00:00:00 2001 From: mernxl Date: Tue, 1 Jun 2021 14:46:01 +0100 Subject: [PATCH] fix: ensure defaults don't override object-like properties in base file --- __mocks__/properties1/app.ini | 4 ++++ __test__/__snapshots__/index.spec.ts.snap | 9 ++++++++- __test__/index.spec.ts | 14 ++++++++++---- src/index.ts | 10 +++++++--- 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/__mocks__/properties1/app.ini b/__mocks__/properties1/app.ini index 6f59259..9136659 100644 --- a/__mocks__/properties1/app.ini +++ b/__mocks__/properties1/app.ini @@ -1,2 +1,6 @@ # Base config file CHANGED_VAR=${CHANGED_VAR} + +[field] +# should pass with this comment +checker=checker diff --git a/__test__/__snapshots__/index.spec.ts.snap b/__test__/__snapshots__/index.spec.ts.snap index 2c3e4e3..c8da5f0 100644 --- a/__test__/__snapshots__/index.spec.ts.snap +++ b/__test__/__snapshots__/index.spec.ts.snap @@ -15,7 +15,14 @@ Object { } `; -exports[`loadConfig load order should override defaults with env vars 1`] = `"Changed in process.env"`; +exports[`loadConfig load order should override defaults with env vars 1`] = ` +Object { + "CHANGED_VAR": "Changed in process.env", + "field": Object { + "checker": "checker", + }, +} +`; exports[`loadConfig load order should parse correctly 1`] = `"Changed in process.env"`; diff --git a/__test__/index.spec.ts b/__test__/index.spec.ts index e8bf4a2..e4f5c09 100644 --- a/__test__/index.spec.ts +++ b/__test__/index.spec.ts @@ -32,7 +32,7 @@ describe('loadConfig', () => { }); describe('load order', () => { - beforeAll(() => { + beforeEach(() => { process.env.CHANGED_VAR = 'Changed in process.env'; }); @@ -42,9 +42,15 @@ describe('loadConfig', () => { it('should override defaults with env vars', () => { expect( - loadConfig('__mocks__/properties1/app.ini', { - CHANGED_VAR: 'Initial, Default', - }).CHANGED_VAR, + pick( + ['CHANGED_VAR', 'field'], + loadConfig('__mocks__/properties1/app.ini', { + CHANGED_VAR: 'Initial, Default', + field: { + checker: 'initial', + }, + }), + ), ).toMatchSnapshot(); }); }); diff --git a/src/index.ts b/src/index.ts index 67e9000..03170c1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -129,21 +129,25 @@ export function loadConfig< // load env file if (fs.existsSync(envDepPropsPath)) { - vars = mergeDeepRight(vars, parseProperties(envDepPropsPath, vars) as any); + vars = mergeDeepRight( + vars, + parseProperties(envDepPropsPath, mergeDeepRight(vars, process.env)) as any, + ); } } // required file's local - const requiredLocal = loadLocalPoint(filePath, vars); + const requiredLocal = loadLocalPoint(filePath, mergeDeepRight(vars, process.env)); if (requiredLocal) { vars = mergeDeepRight(requiredLocal as any, vars); } // we only get vars as seen in the fileName file vars = mergeDeepRight( - parseProperties(path.resolve(filePath), mergeDeepRight(vars, process.env)) as any, vars, + parseProperties(path.resolve(filePath), mergeDeepRight(vars, process.env)) as any, ); + // process.env always have priority return mergeDeepRight(vars, process.env as any); }