From 7a463777dbfeb0488a52dc8616d7240d8e65872c Mon Sep 17 00:00:00 2001 From: Nathaniel Wesley Filardo Date: Wed, 14 Sep 2022 09:58:42 +0000 Subject: [PATCH] Allow repeated config options FIXES https://github.com/yargs/yargs-parser/issues/430 --- lib/yargs-parser.ts | 11 ++++++++--- test/yargs-parser.cjs | 13 +++++++++++++ test/yargs-parser.mjs | 13 +++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/lib/yargs-parser.ts b/lib/yargs-parser.ts index 764ca64e..89364851 100644 --- a/lib/yargs-parser.ts +++ b/lib/yargs-parser.ts @@ -659,8 +659,13 @@ export class YargsParser { applyDefaultsAndAliases(configLookup, flags.aliases, defaults) Object.keys(flags.configs).forEach(function (configKey) { - const configPath = argv[configKey] || configLookup[configKey] - if (configPath) { + var configPaths = argv[configKey] || configLookup[configKey] + + if (typeof configPaths === "string") { + configPaths = [configPaths] + } + + configPaths.forEach(function (configPath : string) { try { let config = null const resolvedConfigPath = mixin.resolve(mixin.cwd(), configPath) @@ -687,7 +692,7 @@ export class YargsParser { if (ex.name === 'PermissionDenied') error = ex else if (argv[configKey]) error = Error(__('Invalid JSON config file: %s', configPath)) } - } + }) }) } diff --git a/test/yargs-parser.cjs b/test/yargs-parser.cjs index 492e3b90..e0465632 100644 --- a/test/yargs-parser.cjs +++ b/test/yargs-parser.cjs @@ -597,6 +597,19 @@ describe('yargs-parser', function () { argv.should.have.property('foo').and.deep.equal('bar') }) + // see https://github.com/yargs/yargs-parser/issues/430 + it("should allow repeated config options", function () { + const argv = parser(['--settings', jsonPath, '--settings', jsonPath], { + alias: { + z: 'zoom' + }, + config: ['settings'] + }) + + argv.should.have.property('herp', 'derp') + argv.should.have.property('zoom', 55) + }) + it('should load options and values from a JS file when config has .js extention', function () { const jsPath = path.resolve(__dirname, './fixtures/settings.cjs') const argv = parser(['--settings', jsPath, '--foo', 'bar'], { diff --git a/test/yargs-parser.mjs b/test/yargs-parser.mjs index 5ef41314..9e9384ba 100644 --- a/test/yargs-parser.mjs +++ b/test/yargs-parser.mjs @@ -112,6 +112,19 @@ describe('yargs-parser (esm)', function () { argv.should.have.property('foo').and.deep.equal('bar') }) + // see https://github.com/yargs/yargs-parser/issues/430 + it("should allow repeated config options", function () { + const argv = parser(['--settings', jsonPath, '--settings', jsonPath], { + alias: { + z: 'zoom' + }, + config: ['settings'] + }) + + argv.should.have.property('herp', 'derp') + argv.should.have.property('zoom', 55) + }) + // for esm, only support importing json files it('should fail to load options and values from a JS file when config has .js extention', function () { const jsPath = path.resolve(__dirname, './fixtures/settings.cjs')