| 45 | } |
| 46 | |
| 47 | export function tryQuoteShellArgs(args: unknown[]): ShellQuoteResult { |
| 48 | try { |
| 49 | const validated: string[] = args.map((arg, index) => { |
| 50 | if (arg === null || arg === undefined) { |
| 51 | return String(arg) |
| 52 | } |
| 53 | |
| 54 | const type = typeof arg |
| 55 | |
| 56 | if (type === 'string') { |
| 57 | return arg as string |
| 58 | } |
| 59 | if (type === 'number' || type === 'boolean') { |
| 60 | return String(arg) |
| 61 | } |
| 62 | |
| 63 | if (type === 'object') { |
| 64 | throw new Error( |
| 65 | `Cannot quote argument at index ${index}: object values are not supported`, |
| 66 | ) |
| 67 | } |
| 68 | if (type === 'symbol') { |
| 69 | throw new Error( |
| 70 | `Cannot quote argument at index ${index}: symbol values are not supported`, |
| 71 | ) |
| 72 | } |
| 73 | if (type === 'function') { |
| 74 | throw new Error( |
| 75 | `Cannot quote argument at index ${index}: function values are not supported`, |
| 76 | ) |
| 77 | } |
| 78 | |
| 79 | throw new Error( |
| 80 | `Cannot quote argument at index ${index}: unsupported type ${type}`, |
| 81 | ) |
| 82 | }) |
| 83 | |
| 84 | const quoted = shellQuoteQuote(validated) |
| 85 | return { success: true, quoted } |
| 86 | } catch (error) { |
| 87 | if (error instanceof Error) { |
| 88 | logError(error) |
| 89 | } |
| 90 | return { |
| 91 | success: false, |
| 92 | error: error instanceof Error ? error.message : 'Unknown quote error', |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Checks if parsed tokens contain malformed entries that suggest shell-quote |