Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(utils): add nested objects to cli args parsing #758

Merged
merged 4 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/nx-plugin/src/executors/internal/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ 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)) {
return value.map(v => `${prefix}${key}="${v}"`);
}

if (typeof value === 'object') {
return Object.entries(value as Record<string, unknown>).map(
([k, v]) => `${prefix}${key}.${k}="${v?.toString()}"`,
return Object.entries(value as Record<string, unknown>).flatMap(
// transform nested objects to the dot notation `key.subkey`
([k, v]) => objectToCliArgs({ [`${key}.${k}`]: v }),
);
}

Expand Down
10 changes: 10 additions & 0 deletions packages/nx-plugin/src/executors/internal/cli.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions packages/utils/src/lib/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ export function objectToCliArgs<
return value.map(v => `${prefix}${key}="${v}"`);
}

if (typeof value === 'object') {
return Object.entries(value as Record<string, ArgumentValue>).flatMap(
// transform nested objects to the dot notation `key.subkey`
([k, v]) => objectToCliArgs({ [`${key}.${k}`]: v }),
);
}

if (typeof value === 'string') {
return [`${prefix}${key}="${value}"`];
}
Expand Down
10 changes: 10 additions & 0 deletions packages/utils/src/lib/transform.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down