Skip to content
Myr

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 file

Take it

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

line 1why, one step back
1// Main logic for `execaCommand()`

Upgrade xo (#980)ehmickybefore adoptionccfa412

line 2why, one step back
2export const mapCommandAsync = ({file, commandArguments}) => parseCommand(file, commandArguments);

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

lines 3–4why, one step back
3
4// Main logic for `execaCommandSync()`

Upgrade xo (#980)ehmickybefore adoptionccfa412

line 5why, one step back
5export const mapCommandSync = ({file, commandArguments}) => ({...parseCommand(file, commandArguments), isSync: true});

Rename files (#977)ehmickybefore adoption4b2e316

line 6why, one step back
6

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

line 7why, one step back
7// Convert `execaCommand(command)` into `execa(file, ...commandArguments)`

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 8–10why, one step back
8const 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 back
11 }
12

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

lines 13–27why, one step back
13 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 }27

Rename files (#977)ehmickybefore adoption4b2e316

line 28why, one step back
28 const tokens = [];

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

line 29why, one step back
29 for (const token of trimmedCommand.split(SPACES_REGEXP)) {

Rename files (#977)ehmickybefore adoption4b2e316

lines 30–39why, one step back
30 // Allow spaces to be escaped by a backslash if not meant as a delimiter
31 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 }39

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

line 40why, one step back
40 return tokens;

Rename files (#977)ehmickybefore adoption4b2e316

lines 41–43why, one step back
41};
4243const SPACES_REGEXP = / +/g;