1 | 'use strict';
|
2 | const SPACES_REGEXP = / +/g;
|
3 |
|
4 | const joinCommand = (file, args = []) => {
|
5 | if (!Array.isArray(args)) {
|
6 | return file;
|
7 | }
|
8 |
|
9 | return [file, ...args].join(' ');
|
10 | };
|
11 |
|
12 |
|
13 | const 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 |
|
28 | const parseCommand = command => {
|
29 | return command
|
30 | .trim()
|
31 | .split(SPACES_REGEXP)
|
32 | .reduce(handleEscaping, []);
|
33 | };
|
34 |
|
35 | module.exports = {
|
36 | joinCommand,
|
37 | parseCommand
|
38 | };
|