(command: string)
| 110 | * @returns An array of individual command strings |
| 111 | */ |
| 112 | export function splitCommands(command: string): string[] { |
| 113 | const commands: string[] = []; |
| 114 | let currentCommand = ''; |
| 115 | let inSingleQuotes = false; |
| 116 | let inDoubleQuotes = false; |
| 117 | let i = 0; |
| 118 | |
| 119 | while (i < command.length) { |
| 120 | const char = command[i]; |
| 121 | const nextChar = command[i + 1]; |
| 122 | |
| 123 | if (char === '\\' && i < command.length - 1) { |
| 124 | currentCommand += char + command[i + 1]; |
| 125 | i += 2; |
| 126 | continue; |
| 127 | } |
| 128 | |
| 129 | if (char === "'" && !inDoubleQuotes) { |
| 130 | inSingleQuotes = !inSingleQuotes; |
| 131 | } else if (char === '"' && !inSingleQuotes) { |
| 132 | inDoubleQuotes = !inDoubleQuotes; |
| 133 | } |
| 134 | |
| 135 | if (!inSingleQuotes && !inDoubleQuotes) { |
| 136 | if ( |
| 137 | (char === '&' && nextChar === '&') || |
| 138 | (char === '|' && nextChar === '|') |
| 139 | ) { |
| 140 | commands.push(currentCommand.trim()); |
| 141 | currentCommand = ''; |
| 142 | i++; // Skip the next character |
| 143 | } else if (char === ';' || char === '&' || char === '|') { |
| 144 | commands.push(currentCommand.trim()); |
| 145 | currentCommand = ''; |
| 146 | } else { |
| 147 | currentCommand += char; |
| 148 | } |
| 149 | } else { |
| 150 | currentCommand += char; |
| 151 | } |
| 152 | i++; |
| 153 | } |
| 154 | |
| 155 | if (currentCommand.trim()) { |
| 156 | commands.push(currentCommand.trim()); |
| 157 | } |
| 158 | |
| 159 | return commands.filter(Boolean); // Filter out any empty strings |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Extracts the root command from a given shell command string. |
no outgoing calls
no test coverage detected