command.js
This file
Path
lib/methods/command.js
43 lines · 1.5 KiB · JavaScript
Last changed by
Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
History of this fileTake it
Add more code comments (#984)ehmickybefore adoptionfdd8a9f
line 1why, one step back1// Main logic for `execaCommand()`Upgrade xo (#980)ehmickybefore adoptionccfa412
line 2why, one step back2export const mapCommandAsync = ({file, commandArguments}) => parseCommand(file, commandArguments);Add more code comments (#984)ehmickybefore adoptionfdd8a9f
lines 3–4why, one step back34// Main logic for `execaCommandSync()`Upgrade xo (#980)ehmickybefore adoptionccfa412
line 5why, one step back5export const mapCommandSync = ({file, commandArguments}) => ({...parseCommand(file, commandArguments), isSync: true});Rename files (#977)ehmickybefore adoption4b2e316
line 6why, one step back6Add more code comments (#984)ehmickybefore adoptionfdd8a9f
line 7why, one step back7// Convert `execaCommand(command)` into `execa(file, ...commandArguments)`Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 8–10why, one step back8const parseCommand = (command, unusedArguments) => {9 if (unusedArguments.length > 0) {10 throw new TypeError(`The command and its arguments must be passed as a single string: ${command} ${unusedArguments}.`);Rename files (#977)ehmickybefore adoption4b2e316
lines 11–12why, one step back11 }12Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
lines 13–27why, one step back13 const [file, ...commandArguments] = parseCommandString(command);14 return {file, commandArguments};15};1617// Convert `command` string into an array of file or arguments to pass to $`${...fileOrCommandArguments}`18export const parseCommandString = command => {19 if (typeof command !== 'string') {20 throw new TypeError(`The command must be a string: ${String(command)}.`);21 }2223 const trimmedCommand = command.trim();24 if (trimmedCommand === '') {25 return [];26 }27Rename files (#977)ehmickybefore adoption4b2e316
line 28why, one step back28 const tokens = [];Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
line 29why, one step back29 for (const token of trimmedCommand.split(SPACES_REGEXP)) {Rename files (#977)ehmickybefore adoption4b2e316
lines 30–39why, one step back30 // Allow spaces to be escaped by a backslash if not meant as a delimiter31 const previousToken = tokens.at(-1);32 if (previousToken && previousToken.endsWith('\\')) {33 // Merge previous token with current one34 tokens[tokens.length - 1] = `${previousToken.slice(0, -1)} ${token}`;35 } else {36 tokens.push(token);37 }38 }39Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
line 40why, one step back40 return tokens;Rename files (#977)ehmickybefore adoption4b2e316
lines 41–43why, one step back41};4243const SPACES_REGEXP = / +/g;