source: frontend/node_modules/execa/lib/command.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 1.2 KB
Line 
1'use strict';
2const normalizeArgs = (file, args = []) => {
3 if (!Array.isArray(args)) {
4 return [file];
5 }
6
7 return [file, ...args];
8};
9
10const NO_ESCAPE_REGEXP = /^[\w.-]+$/;
11const DOUBLE_QUOTES_REGEXP = /"/g;
12
13const escapeArg = arg => {
14 if (typeof arg !== 'string' || NO_ESCAPE_REGEXP.test(arg)) {
15 return arg;
16 }
17
18 return `"${arg.replace(DOUBLE_QUOTES_REGEXP, '\\"')}"`;
19};
20
21const joinCommand = (file, args) => {
22 return normalizeArgs(file, args).join(' ');
23};
24
25const getEscapedCommand = (file, args) => {
26 return normalizeArgs(file, args).map(arg => escapeArg(arg)).join(' ');
27};
28
29const SPACES_REGEXP = / +/g;
30
31// Handle `execa.command()`
32const parseCommand = command => {
33 const tokens = [];
34 for (const token of command.trim().split(SPACES_REGEXP)) {
35 // Allow spaces to be escaped by a backslash if not meant as a delimiter
36 const previousToken = tokens[tokens.length - 1];
37 if (previousToken && previousToken.endsWith('\\')) {
38 // Merge previous token with current one
39 tokens[tokens.length - 1] = `${previousToken.slice(0, -1)} ${token}`;
40 } else {
41 tokens.push(token);
42 }
43 }
44
45 return tokens;
46};
47
48module.exports = {
49 joinCommand,
50 getEscapedCommand,
51 parseCommand
52};
Note: See TracBrowser for help on using the repository browser.