MCPcopy Index your code
hub / github.com/code-pushup/cli / objectToCliArgs

Function objectToCliArgs

packages/utils/src/lib/transform.ts:63–111  ·  view source on GitHub ↗
(params?: CliArgsObject<T>)

Source from the content-addressed store, hash-verified

61 * });
62 */
63export function objectToCliArgs<
64 T extends object = Record<string, ArgumentValue>,
65>(params?: CliArgsObject<T>): string[] {
66 if (!params) {
67 return [];
68 }
69
70 return Object.entries(params).flatMap(([key, value]) => {
71 // process/file/script
72 if (key === '_') {
73 return Array.isArray(value) ? value : [`${value}`];
74 }
75 const prefix = key.length === 1 ? '-' : '--';
76 // "-*" arguments (shorthands)
77 if (Array.isArray(value)) {
78 return value.map(v => `${prefix}${key}="${v}"`);
79 }
80 // "--*" arguments ==========
81
82 if (Array.isArray(value)) {
83 return value.map(v => `${prefix}${key}="${v}"`);
84 }
85
86 if (typeof value === 'object') {
87 return Object.entries(value as Record<string, ArgumentValue>).flatMap(
88 // transform nested objects to the dot notation `key.subkey`
89 ([k, v]) => objectToCliArgs({ [`${key}.${k}`]: v }),
90 );
91 }
92
93 if (typeof value === 'string') {
94 return [`${prefix}${key}="${value}"`];
95 }
96
97 if (typeof value === 'number') {
98 return [`${prefix}${key}=${value}`];
99 }
100
101 if (typeof value === 'boolean') {
102 return [`${prefix}${value ? '' : 'no-'}${key}`];
103 }
104
105 if (value == null) {
106 return [];
107 }
108
109 throw new Error(`Unsupported type ${typeof value} for key ${key}`);
110 });
111}
112
113export function toUnixPath(path: string): string {
114 return path.replace(/\\/g, '/');

Callers 3

runCliExecutorFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected