diff --git a/packages/nx-plugin/src/executors/internal/cli.ts b/packages/nx-plugin/src/executors/internal/cli.ts index d99ebf769..abb5edf4b 100644 --- a/packages/nx-plugin/src/executors/internal/cli.ts +++ b/packages/nx-plugin/src/executors/internal/cli.ts @@ -27,6 +27,7 @@ export function objectToCliArgs< // eslint-disable-next-line @typescript-eslint/no-unsafe-return return Array.isArray(value) ? value : [`${value}`]; } + const prefix = key.length === 1 ? '-' : '--'; // "-*" arguments (shorthands) if (Array.isArray(value)) { @@ -34,8 +35,9 @@ export function objectToCliArgs< } if (typeof value === 'object') { - return Object.entries(value as Record).map( - ([k, v]) => `${prefix}${key}.${k}="${v?.toString()}"`, + return Object.entries(value as Record).flatMap( + // transform nested objects to the dot notation `key.subkey` + ([k, v]) => objectToCliArgs({ [`${key}.${k}`]: v }), ); } diff --git a/packages/nx-plugin/src/executors/internal/cli.unit.test.ts b/packages/nx-plugin/src/executors/internal/cli.unit.test.ts index f20620db9..41892097b 100644 --- a/packages/nx-plugin/src/executors/internal/cli.unit.test.ts +++ b/packages/nx-plugin/src/executors/internal/cli.unit.test.ts @@ -63,6 +63,16 @@ describe('objectToCliArgs', () => { expect(result).toStrictEqual(['--format.json="simple"']); }); + it('should handle nested objects', () => { + const params = { persist: { format: ['json', 'md'], verbose: false } }; + const result = objectToCliArgs(params); + expect(result).toEqual([ + '--persist.format="json"', + '--persist.format="md"', + '--no-persist.verbose', + ]); + }); + it('should handle objects with undefined', () => { const params = { format: undefined }; const result = objectToCliArgs(params); diff --git a/packages/utils/src/lib/transform.ts b/packages/utils/src/lib/transform.ts index d7afa5efe..c21eb7088 100644 --- a/packages/utils/src/lib/transform.ts +++ b/packages/utils/src/lib/transform.ts @@ -91,6 +91,13 @@ export function objectToCliArgs< return value.map(v => `${prefix}${key}="${v}"`); } + if (typeof value === 'object') { + return Object.entries(value as Record).flatMap( + // transform nested objects to the dot notation `key.subkey` + ([k, v]) => objectToCliArgs({ [`${key}.${k}`]: v }), + ); + } + if (typeof value === 'string') { return [`${prefix}${key}="${value}"`]; } diff --git a/packages/utils/src/lib/transform.unit.test.ts b/packages/utils/src/lib/transform.unit.test.ts index 15971a53f..26c2c9e2b 100644 --- a/packages/utils/src/lib/transform.unit.test.ts +++ b/packages/utils/src/lib/transform.unit.test.ts @@ -191,6 +191,16 @@ describe('objectToCliArgs', () => { expect(result).toEqual(['--format="json"', '--format="md"']); }); + it('should handle nested objects', () => { + const params = { persist: { format: ['json', 'md'], verbose: false } }; + const result = objectToCliArgs(params); + expect(result).toEqual([ + '--persist.format="json"', + '--persist.format="md"', + '--no-persist.verbose', + ]); + }); + it('should throw error for unsupported type', () => { const params = { unsupported: undefined as any }; expect(() => objectToCliArgs(params)).toThrow('Unsupported type');