(params?: CliArgsObject<T>)
| 61 | * }); |
| 62 | */ |
| 63 | export 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 | |
| 113 | export function toUnixPath(path: string): string { |
| 114 | return path.replace(/\\/g, '/'); |
no outgoing calls
no test coverage detected