MCPcopy Create free account
hub / github.com/sindresorhus/execa / parseCommandString

Function parseCommandString

lib/methods/command.js:2–25  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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
7 const trimmedCommand = command.trim();
8 if (trimmedCommand === '') {
9 return [];
10 }
11
12 const tokens = [];
13 for (const token of trimmedCommand.split(SPACES_REGEXP)) {
14 // Allow spaces to be escaped by a backslash if not meant as a delimiter
15 const previousToken = tokens.at(-1);
16 if (previousToken && previousToken.endsWith('\\')) {
17 // Merge previous token with current one
18 tokens[tokens.length - 1] = `${previousToken.slice(0, -1)} ${token}`;
19 } else {
20 tokens.push(token);
21 }
22 }
23
24 return tokens;
25};
26
27const SPACES_REGEXP = / +/g;

Callers 4

command.test-d.tsFile · 0.90
parseAndRunCommandFunction · 0.90
command.jsFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected