( command: string, addStdinRedirect: boolean = true, )
| 46 | * @returns The properly quoted command |
| 47 | */ |
| 48 | export function quoteShellCommand( |
| 49 | command: string, |
| 50 | addStdinRedirect: boolean = true, |
| 51 | ): string { |
| 52 | // If command contains heredoc or multiline strings, handle specially |
| 53 | // The shell-quote library incorrectly escapes ! to \! in these cases |
| 54 | if (containsHeredoc(command) || containsMultilineString(command)) { |
| 55 | // For heredocs and multiline strings, we need to quote for eval |
| 56 | // but avoid shell-quote's aggressive escaping |
| 57 | // We'll use single quotes and escape only single quotes in the command |
| 58 | const escaped = command.replace(/'/g, "'\"'\"'") |
| 59 | const quoted = `'${escaped}'` |
| 60 | |
| 61 | // Don't add stdin redirect for heredocs as they provide their own input |
| 62 | if (containsHeredoc(command)) { |
| 63 | return quoted |
| 64 | } |
| 65 | |
| 66 | // For multiline strings without heredocs, add stdin redirect if needed |
| 67 | return addStdinRedirect ? `${quoted} < /dev/null` : quoted |
| 68 | } |
| 69 | |
| 70 | // For regular commands, use shell-quote |
| 71 | if (addStdinRedirect) { |
| 72 | return quote([command, '<', '/dev/null']) |
| 73 | } |
| 74 | |
| 75 | return quote([command]) |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Detects if a command already has a stdin redirect |
no test coverage detected