Skip to content
Myr

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 file

Take it

Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc

line 1why, one step back
1const normalizeArgs = (file, args = []) => {

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

line 2why, one step back
2 if (!Array.isArray(args)) {

Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc

line 3why, one step back
3 return [file];

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

lines 4–5why, one step back
4 }
5

Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc

line 6why, one step back
6 return [file, ...args];

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

lines 7–8why, one step back
7};
8

Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc

lines 9–12why, one step back
9const 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 back
13 if (typeof arg !== 'string' || NO_ESCAPE_REGEXP.test(arg)) {

Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc

lines 14–19why, one step back
14 return arg;
15 }1617 return `"${arg.replace(DOUBLE_QUOTES_REGEXP, '\\"')}"`;18};19

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 20why, one step back
20export const joinCommand = (file, args) => normalizeArgs(file, args).join(' ');

Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc

line 21why, one step back
21

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 22why, one step back
22export const getEscapedCommand = (file, args) => normalizeArgs(file, args).map(arg => escapeArg(arg)).join(' ');

Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc

lines 23–25why, one step back
23
24const SPACES_REGEXP = / +/g;25

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

lines 26–27why, one step back
26// Handle `execaCommand()`
27export const parseCommand = command => {

Avoid using `Array#reduce` (#436)Federicobefore adoptioncfcc2dc

lines 28–40why, one step back
28 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 back
41};