command.js
This file
Path
lib/methods/command.js
18 lines · 775 bytes · JavaScript
Last changed by
Fix template strings, `parseCommandString()` and `execaNode()` edge casesSindre Sorhusbefore adoptionfb92da1
History of this fileTake it
12345678910111213141516171819// Convert `command` string into an array of file or arguments to pass to $`${...fileOrCommandArguments}`export const parseCommandString = command => { if (typeof command !== 'string') { throw new TypeError(`The command must be a string: ${String(command)}.`); }
// Allow spaces to be escaped by a backslash if not meant as a delimiter return trimCommand(command).match(TOKEN_REGEXP)?.map(token => token.replaceAll('\\ ', ' ')) ?? [];};
const TOKEN_REGEXP = /(?:\\ |[^ ])+/g;
// Like `string.trim()`, but keeps a trailing escaped spaceconst trimCommand = command => { const trimmedCommand = command.trim(); const trimmedEnd = command.trimEnd(); return trimmedEnd.endsWith('\\') && command[trimmedEnd.length] === ' ' ? `${trimmedCommand} ` : trimmedCommand;};