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 fileTake it
123456789101112131415161718192021222324252627282930313233343536373839'use strict';const SPACES_REGEXP = / +/g;
const joinCommand = (file, args = []) => { if (!Array.isArray(args)) { return file; }
return [file, ...args].join(' ');};
// Allow spaces to be escaped by a backslash if not meant as a delimiterconst handleEscaping = (tokens, token, index) => { if (index === 0) { return [token]; }
const previousToken = tokens[tokens.length - 1];
if (previousToken.endsWith('\\')) { return [...tokens.slice(0, -1), `${previousToken.slice(0, -1)} ${token}`]; }
return [...tokens, token];};
// Handle `execa.command()`const parseCommand = command => { return command .trim() .split(SPACES_REGEXP) .reduce(handleEscaping, []);};
module.exports = { joinCommand, parseCommand};