UNPKG

738 BJavaScriptView Raw
1'use strict';
2const SPACES_REGEXP = / +/g;
3
4const joinCommand = (file, args = []) => {
5 if (!Array.isArray(args)) {
6 return file;
7 }
8
9 return [file, ...args].join(' ');
10};
11
12// Allow spaces to be escaped by a backslash if not meant as a delimiter
13const handleEscaping = (tokens, token, index) => {
14 if (index === 0) {
15 return [token];
16 }
17
18 const previousToken = tokens[tokens.length - 1];
19
20 if (previousToken.endsWith('\\')) {
21 return [...tokens.slice(0, -1), `${previousToken.slice(0, -1)} ${token}`];
22 }
23
24 return [...tokens, token];
25};
26
27// Handle `execa.command()`
28const parseCommand = command => {
29 return command
30 .trim()
31 .split(SPACES_REGEXP)
32 .reduce(handleEscaping, []);
33};
34
35module.exports = {
36 joinCommand,
37 parseCommand
38};