Skip to content
Myr

command.js

This file

Path

lib/command.js

120 lines · 3.0 KiB · JavaScript

Last changed by

Fix argument concatenation with `$` (#553)ehmickybefore adoption07585d0

History of this file

Take it

Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251

lines 1–3why, one step back
1import {Buffer} from 'node:buffer';
2import {ChildProcess} from 'node:child_process';3

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

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

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

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

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

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

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

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

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

lines 10–11why, one step back
10};
11

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

lines 12–15why, one step back
12const NO_ESCAPE_REGEXP = /^[\w.-]+$/;
13const DOUBLE_QUOTES_REGEXP = /"/g;1415const escapeArg = arg => {

Fix error message when user passes a single array argument (#468)ehmickybefore adoption2b9c0e1

line 16why, one step back
16 if (typeof arg !== 'string' || NO_ESCAPE_REGEXP.test(arg)) {

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

lines 17–22why, one step back
17 return arg;
18 }1920 return `"${arg.replace(DOUBLE_QUOTES_REGEXP, '\\"')}"`;21};22

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

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

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

line 24why, one step back
24

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

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

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

lines 26–28why, one step back
26
27const SPACES_REGEXP = / +/g;28

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

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

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

lines 31–43why, one step back
31 const tokens = [];
32 for (const token of command.trim().split(SPACES_REGEXP)) {33 // Allow spaces to be escaped by a backslash if not meant as a delimiter34 const previousToken = tokens[tokens.length - 1];35 if (previousToken && previousToken.endsWith('\\')) {36 // Merge previous token with current one37 tokens[tokens.length - 1] = `${previousToken.slice(0, -1)} ${token}`;38 } else {39 tokens.push(token);40 }41 }4243 return tokens;

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

line 44why, one step back
44};

Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c

line 45why, one step back
45

Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251

lines 46–78why, one step back
46const parseExpression = expression => {
47 const typeOfExpression = typeof expression;4849 if (typeOfExpression === 'string') {50 return expression;51 }5253 if (typeOfExpression === 'number') {54 return String(expression);55 }5657 if (58 typeOfExpression === 'object'59 && expression !== null60 && !(expression instanceof ChildProcess)61 && 'stdout' in expression62 ) {63 const typeOfStdout = typeof expression.stdout;6465 if (typeOfStdout === 'string') {66 return expression.stdout;67 }6869 if (Buffer.isBuffer(expression.stdout)) {70 return expression.stdout.toString();71 }7273 throw new TypeError(`Unexpected "${typeOfStdout}" stdout in template expression`);74 }7576 throw new TypeError(`Unexpected "${typeOfExpression}" in template expression`);77};78

Fix argument concatenation with `$` (#553)ehmickybefore adoption07585d0

lines 79–87why, one step back
79const concatTokens = (tokens, nextTokens, isNew) => isNew || tokens.length === 0 || nextTokens.length === 0
80 ? [...tokens, ...nextTokens]81 : [82 ...tokens.slice(0, -1),83 `${tokens[tokens.length - 1]}${nextTokens[0]}`,84 ...nextTokens.slice(1),85 ];8687const parseTemplate = ({templates, expressions, tokens, index, template}) => {

Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c

lines 88–89why, one step back
88 const templateString = template ?? templates.raw[index];
89 const templateTokens = templateString.split(SPACES_REGEXP).filter(Boolean);

Fix argument concatenation with `$` (#553)ehmickybefore adoption07585d0

lines 90–94why, one step back
90 const newTokens = concatTokens(
91 tokens,92 templateTokens,93 templateString.startsWith(' '),94 );

Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c

lines 95–96why, one step back
95
96 if (index === expressions.length) {

Fix argument concatenation with `$` (#553)ehmickybefore adoption07585d0

line 97why, one step back
97 return newTokens;

Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c

lines 98–100why, one step back
98 }
99100 const expression = expressions[index];

Fix argument concatenation with `$` (#553)ehmickybefore adoption07585d0

lines 101–109why, one step back
101 const expressionTokens = Array.isArray(expression)
102 ? expression.map(expression => parseExpression(expression))103 : [parseExpression(expression)];104 return concatTokens(105 newTokens,106 expressionTokens,107 templateString.endsWith(' '),108 );109};

Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c

line 110why, one step back
110

Fix argument concatenation with `$` (#553)ehmickybefore adoption07585d0

lines 111–118why, one step back
111export const parseTemplates = (templates, expressions) => {
112 let tokens = [];113114 for (const [index, template] of templates.entries()) {115 tokens = parseTemplate({templates, expressions, tokens, index, template});116 }117118 return tokens;

Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c

lines 119–120why, one step back
119};
120