Skip to content

Commit

Permalink
Merge pull request #33 from gemini-testing/HERMIONE-1471.fix_nested_e…
Browse files Browse the repository at this point in the history
…nv_prefix

fix: fallback env prefix for nested options
  • Loading branch information
KuznetsovRoman authored Apr 3, 2024
2 parents b8275fd + b1c258f commit b2d826f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/locator.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ module.exports = function({options, env, argv, envPrefix = '', cliPrefix = '--'}
return envPrefix + subKey;
}

function getEnvPrefix(prefixes, envSubKey) {
if (typeof prefixes === 'string') {
return prefixes + envSubKey + '_';
}

return prefixes.map(prefix => prefix + envSubKey + '_');
}

function getNested(option, {namePrefix, envPrefix, cliPrefix}) {
return (subKey) => {
const envSubKey = _.snakeCase(subKey);
Expand All @@ -48,7 +56,7 @@ module.exports = function({options, env, argv, envPrefix = '', cliPrefix = '--'}
},
{
namePrefix: newName,
envPrefix: `${envName}_`,
envPrefix: getEnvPrefix(envPrefix, envSubKey),
cliPrefix: `${cliFlag}-`
}
);
Expand Down
13 changes: 13 additions & 0 deletions test/locator.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,17 @@ describe('locator', () => {

assert.propertyVal(childPointer, 'envVar', 'some env value');
});

it('should support multiple prefixes with deep nesting', () => {
const pointer = locatorWithEnv(
{
'bar_some_deep_nested_option': 'some value'
},
{envPrefix: ['foo_', 'bar_']}
);

const childPointer = pointer.nested('some').nested('deep').nested('nested').nested('option');

assert.propertyVal(childPointer, 'envVar', 'some value');
});
});

0 comments on commit b2d826f

Please sign in to comment.