command.js
This file
Path
lib/command.js
41 lines · 1.1 KiB · JavaScript
Last changed by
Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
History of this fileTake it
Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc
line 1why, one step back1const normalizeArgs = (file, args = []) => {Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b
line 2why, one step back2 if (!Array.isArray(args)) {Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc
line 3why, one step back3 return [file];Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b
lines 4–5why, one step back4 }5Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc
line 6why, one step back6 return [file, ...args];Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b
lines 7–8why, one step back7};8Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc
lines 9–12why, one step back9const NO_ESCAPE_REGEXP = /^[\w.-]+$/;10const DOUBLE_QUOTES_REGEXP = /"/g;1112const escapeArg = arg => {Fix error message when user passes a single array argument (#468)ehmickybefore adoption2b9c0e1
line 13why, one step back13 if (typeof arg !== 'string' || NO_ESCAPE_REGEXP.test(arg)) {Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc
lines 14–19why, one step back14 return arg;15 }1617 return `"${arg.replace(DOUBLE_QUOTES_REGEXP, '\\"')}"`;18};19Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 20why, one step back20export const joinCommand = (file, args) => normalizeArgs(file, args).join(' ');Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc
line 21why, one step back21Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 22why, one step back22export const getEscapedCommand = (file, args) => normalizeArgs(file, args).map(arg => escapeArg(arg)).join(' ');Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc
lines 23–25why, one step back2324const SPACES_REGEXP = / +/g;25Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
lines 26–27why, one step back26// Handle `execaCommand()`27export const parseCommand = command => {Avoid using `Array#reduce` (#436)Federicobefore adoptioncfcc2dc
lines 28–40why, one step back28 const tokens = [];29 for (const token of command.trim().split(SPACES_REGEXP)) {30 // Allow spaces to be escaped by a backslash if not meant as a delimiter31 const previousToken = tokens[tokens.length - 1];32 if (previousToken && previousToken.endsWith('\\')) {33 // Merge previous token with current one34 tokens[tokens.length - 1] = `${previousToken.slice(0, -1)} ${token}`;35 } else {36 tokens.push(token);37 }38 }3940 return tokens;Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b
line 41why, one step back41};