| 1 | // Convert `command` string into an array of file or arguments to pass to $`${...fileOrCommandArguments}` |
| 2 | export const parseCommandString = command => { |
| 3 | if (typeof command !== 'string') { |
| 4 | throw new TypeError(`The command must be a string: ${String(command)}.`); |
| 5 | } |
| 6 | |
| 7 | const trimmedCommand = command.trim(); |
| 8 | if (trimmedCommand === '') { |
| 9 | return []; |
| 10 | } |
| 11 | |
| 12 | const tokens = []; |
| 13 | for (const token of trimmedCommand.split(SPACES_REGEXP)) { |
| 14 | // Allow spaces to be escaped by a backslash if not meant as a delimiter |
| 15 | const previousToken = tokens.at(-1); |
| 16 | if (previousToken && previousToken.endsWith('\\')) { |
| 17 | // Merge previous token with current one |
| 18 | tokens[tokens.length - 1] = `${previousToken.slice(0, -1)} ${token}`; |
| 19 | } else { |
| 20 | tokens.push(token); |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | return tokens; |
| 25 | }; |
| 26 | |
| 27 | const SPACES_REGEXP = / +/g; |
no outgoing calls
no test coverage detected