command.js
This file
Path
lib/command.js
32 lines · 726 bytes · JavaScript
Last changed by
Avoid using `Array#reduce` (#436)Federicobefore adoptioncfcc2dc
History of this fileTake it
Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b
lines 1–13why, one step back1'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// Handle `execa.command()`13const parseCommand = command => {Avoid using `Array#reduce` (#436)Federicobefore adoptioncfcc2dc
lines 14–26why, one step back14 const tokens = [];15 for (const token of command.trim().split(SPACES_REGEXP)) {16 // Allow spaces to be escaped by a backslash if not meant as a delimiter17 const previousToken = tokens[tokens.length - 1];18 if (previousToken && previousToken.endsWith('\\')) {19 // Merge previous token with current one20 tokens[tokens.length - 1] = `${previousToken.slice(0, -1)} ${token}`;21 } else {22 tokens.push(token);23 }24 }2526 return tokens;Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b
lines 27–32why, one step back27};2829module.exports = {30 joinCommand,31 parseCommand32};