command.js
This file
Path
lib/methods/command.js
29 lines · 1.1 KiB · JavaScript
Last changed by
Add more code comments (#984)ehmickybefore adoptionfdd8a9f
History of this fileTake it
123456789101112131415161718192021222324252627282930// Main logic for `execaCommand()`export const mapCommandAsync = ({file, commandArguments}) => parseCommand(file, commandArguments);
// Main logic for `execaCommandSync()`export const mapCommandSync = ({file, commandArguments}) => ({...parseCommand(file, commandArguments), isSync: true});
// Convert `execaCommand(command)` into `execa(file, ...commandArguments)`const parseCommand = (command, unusedArguments) => { if (unusedArguments.length > 0) { throw new TypeError(`The command and its arguments must be passed as a single string: ${command} ${unusedArguments}.`); }
const tokens = []; for (const token of command.trim().split(SPACES_REGEXP)) { // Allow spaces to be escaped by a backslash if not meant as a delimiter const previousToken = tokens.at(-1); if (previousToken && previousToken.endsWith('\\')) { // Merge previous token with current one tokens[tokens.length - 1] = `${previousToken.slice(0, -1)} ${token}`; } else { tokens.push(token); } }
const [file, ...commandArguments] = tokens; return {file, commandArguments};};
const SPACES_REGEXP = / +/g;