Skip to content

Commit

Permalink
fix: ensure defaults don't override object-like properties in base file
Browse files Browse the repository at this point in the history
  • Loading branch information
mernxl committed Jun 1, 2021
1 parent dc2a04f commit c65b9dd
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
4 changes: 4 additions & 0 deletions __mocks__/properties1/app.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
# Base config file
CHANGED_VAR=${CHANGED_VAR}

[field]
# should pass with this comment
checker=checker
9 changes: 8 additions & 1 deletion __test__/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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"`;

Expand Down
14 changes: 10 additions & 4 deletions __test__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('loadConfig', () => {
});

describe('load order', () => {
beforeAll(() => {
beforeEach(() => {
process.env.CHANGED_VAR = 'Changed in process.env';
});

Expand All @@ -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();
});
});
Expand Down
10 changes: 7 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,21 +129,25 @@ export function loadConfig<

// load env file
if (fs.existsSync(envDepPropsPath)) {
vars = mergeDeepRight(vars, parseProperties<InternalType>(envDepPropsPath, vars) as any);
vars = mergeDeepRight(
vars,
parseProperties<InternalType>(envDepPropsPath, mergeDeepRight(vars, process.env)) as any,
);
}
}

// required file's local
const requiredLocal = loadLocalPoint<LocalsType>(filePath, vars);
const requiredLocal = loadLocalPoint<LocalsType>(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<PropType>(path.resolve(filePath), mergeDeepRight(vars, process.env)) as any,
vars,
parseProperties<PropType>(path.resolve(filePath), mergeDeepRight(vars, process.env)) as any,
);

// process.env always have priority
return mergeDeepRight(vars, process.env as any);
}

0 comments on commit c65b9dd

Please sign in to comment.