Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than 63de206 point at it.

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

lines 1–6why, one step back
1// Convert `command` string into an array of file or arguments to pass to $`${...fileOrCommandArguments}`
2export const parseCommandString = command => {3 if (typeof command !== 'string') {4 throw new TypeError(`The command must be a string: ${String(command)}.`);5 }6

Fix template strings, `parseCommandString()` and `execaNode()` edge casesSindre Sorhusbefore adoptionfb92da1

lines 7–9why, one step back
7 // Allow spaces to be escaped by a backslash if not meant as a delimiter
8 return trimCommand(command).match(TOKEN_REGEXP)?.map(token => token.replaceAll('\\ ', ' ')) ?? [];9};

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

line 10why, one step back
10

Fix template strings, `parseCommandString()` and `execaNode()` edge casesSindre Sorhusbefore adoptionfb92da1

line 11why, one step back
11const TOKEN_REGEXP = /(?:\\ |[^ ])+/g;

Improve examples used in types (#1000)ehmickybefore adoption63de206

line 12why, one step back
12

Fix template strings, `parseCommandString()` and `execaNode()` edge casesSindre Sorhusbefore adoptionfb92da1

lines 13–17why, one step back
13// Like `string.trim()`, but keeps a trailing escaped space
14const trimCommand = command => {15 const trimmedCommand = command.trim();16 const trimmedEnd = command.trimEnd();17 return trimmedEnd.endsWith('\\') && command[trimmedEnd.length] === ' ' ? `${trimmedCommand} ` : trimmedCommand;

Improve examples used in types (#1000)ehmickybefore adoption63de206

line 18why, one step back
18};