Skip to content
Myr

command.js

This file

Path

lib/command.js

38 lines · 738 bytes · JavaScript

Last changed by

Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b

History of this file

Take it

Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b

lines 1–38why, one step back
1'use strict';
2const SPACES_REGEXP = / +/g;34const joinCommand = (file, args = []) => {5 if (!Array.isArray(args)) {6 return file;7 }89 return [file, ...args].join(' ');10};1112// Allow spaces to be escaped by a backslash if not meant as a delimiter13const handleEscaping = (tokens, token, index) => {14 if (index === 0) {15 return [token];16 }1718 const previousToken = tokens[tokens.length - 1];1920 if (previousToken.endsWith('\\')) {21 return [...tokens.slice(0, -1), `${previousToken.slice(0, -1)} ${token}`];22 }2324 return [...tokens, token];25};2627// Handle `execa.command()`28const parseCommand = command => {29 return command30 .trim()31 .split(SPACES_REGEXP)32 .reduce(handleEscaping, []);33};3435module.exports = {36 joinCommand,37 parseCommand38};