From 7c81f8113999e2bb68739cc9a6ee008e9db62bfb Mon Sep 17 00:00:00 2001 From: Michael Seredenko <148336720+MishaSeredenkoPushBased@users.noreply.github.com> Date: Fri, 27 Oct 2023 13:59:57 +0200 Subject: [PATCH] fix(cli): parse multiple config args to last item of array (#164) Closes #146 --- packages/cli/src/lib/yargs-cli.spec.ts | 16 ++++++++++++++++ packages/cli/src/lib/yargs-cli.ts | 6 ++++++ 2 files changed, 22 insertions(+) diff --git a/packages/cli/src/lib/yargs-cli.spec.ts b/packages/cli/src/lib/yargs-cli.spec.ts index bb1d07c63..6589dc5c7 100644 --- a/packages/cli/src/lib/yargs-cli.spec.ts +++ b/packages/cli/src/lib/yargs-cli.spec.ts @@ -20,6 +20,22 @@ describe('yargsCli', () => { expect(parsedArgv.verbose).toBe(false); }); + it('multiple config args should be parsed to last item from array', async () => { + const args: string[] = ['--config=./config.a.ts', '--config=./config.b.ts']; + const parsedArgv = await yargsCli(args, { + options, + }).parseAsync(); + expect(parsedArgv.config).toBe('./config.b.ts'); + }); + + it('single config arg should be parsed as a single string', async () => { + const args: string[] = ['--config=./config.a.ts']; + const parsedArgv = await yargsCli(args, { + options, + }).parseAsync(); + expect(parsedArgv.config).toBe('./config.a.ts'); + }); + it('global options should parse correctly', async () => { const args: string[] = objectToCliArgs({ verbose: true, diff --git a/packages/cli/src/lib/yargs-cli.ts b/packages/cli/src/lib/yargs-cli.ts index 1c29ba123..cc5d908df 100644 --- a/packages/cli/src/lib/yargs-cli.ts +++ b/packages/cli/src/lib/yargs-cli.ts @@ -49,6 +49,12 @@ export function yargsCli( 'strip-dashed': true, } satisfies Partial) .array('persist.format') + .coerce('config', (config: string | string[]) => { + if (Array.isArray(config)) { + return config[config.length - 1]; + } + return config; + }) .options(options); //.demandCommand(...demandCommand);