Skip to content
Myr

command.js

This file

Path

lib/command.js

119 lines · 3.0 KiB · JavaScript

Last changed by

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

History of this file

Take it

The walk stopped after 200 commits: lines older than 56b749d point at it.

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

line 1why, one step back
1import {ChildProcess} from 'node:child_process';

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

line 2why, one step back
2import {isBinary, binaryToString} from './stdio/utils.js';

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

line 3why, one step back
3

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

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

Add `all` option (#353)Tiago Nápolibefore adoption56b749d

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

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

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

Add `all` option (#353)Tiago Nápolibefore adoption56b749d

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

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

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

Add `all` option (#353)Tiago Nápolibefore adoption56b749d

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

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

lines 12–14why, one step back
12const NO_ESCAPE_REGEXP = /^[\w.-]+$/;
1314const escapeArg = arg => {

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

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

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

lines 16–18why, one step back
16 return arg;
17 }18

Require Node.js 16 (#569)ehmickybefore adoption65992b4

line 19why, one step back
19 return `"${arg.replaceAll('"', '\\"')}"`;

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

lines 20–21why, one step back
20};
21

Refactor duplicate code between `execa()` and `execaSync()` (#648)ehmickybefore adoption4fb84f0

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

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

line 23why, one step back
23

Refactor duplicate code between `execa()` and `execaSync()` (#648)ehmickybefore adoption4fb84f0

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

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

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

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

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

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

lines 30–32why, one step back
30 const tokens = [];
31 for (const token of command.trim().split(SPACES_REGEXP)) {32 // Allow spaces to be escaped by a backslash if not meant as a delimiter

Require Node.js 16 (#569)ehmickybefore adoption65992b4

line 33why, one step back
33 const previousToken = tokens.at(-1);

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

lines 34–42why, one step back
34 if (previousToken && previousToken.endsWith('\\')) {
35 // Merge previous token with current one36 tokens[tokens.length - 1] = `${previousToken.slice(0, -1)} ${token}`;37 } else {38 tokens.push(token);39 }40 }4142 return tokens;

Add `all` option (#353)Tiago Nápolibefore adoption56b749d

line 43why, one step back
43};

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

line 44why, one step back
44

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

lines 45–67why, one step back
45const parseExpression = expression => {
46 const typeOfExpression = typeof expression;4748 if (typeOfExpression === 'string') {49 return expression;50 }5152 if (typeOfExpression === 'number') {53 return String(expression);54 }5556 if (57 typeOfExpression === 'object'58 && expression !== null59 && !(expression instanceof ChildProcess)60 && 'stdout' in expression61 ) {62 const typeOfStdout = typeof expression.stdout;6364 if (typeOfStdout === 'string') {65 return expression.stdout;66 }67

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 68–69why, one step back
68 if (isBinary(expression.stdout)) {
69 return binaryToString(expression.stdout);

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

lines 70–77why, one step back
70 }
7172 throw new TypeError(`Unexpected "${typeOfStdout}" stdout in template expression`);73 }7475 throw new TypeError(`Unexpected "${typeOfExpression}" in template expression`);76};77

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

lines 78–81why, one step back
78const concatTokens = (tokens, nextTokens, isNew) => isNew || tokens.length === 0 || nextTokens.length === 0
79 ? [...tokens, ...nextTokens]80 : [81 ...tokens.slice(0, -1),

Require Node.js 16 (#569)ehmickybefore adoption65992b4

line 82why, one step back
82 `${tokens.at(-1)}${nextTokens[0]}`,

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

lines 83–86why, one step back
83 ...nextTokens.slice(1),
84 ];8586const parseTemplate = ({templates, expressions, tokens, index, template}) => {

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

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

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

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

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

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

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

line 96why, one step back
96 return newTokens;

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

lines 97–99why, one step back
97 }
9899 const expression = expressions[index];

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

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

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

line 109why, one step back
109

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

lines 110–117why, one step back
110export const parseTemplates = (templates, expressions) => {
111 let tokens = [];112113 for (const [index, template] of templates.entries()) {114 tokens = parseTemplate({templates, expressions, tokens, index, template});115 }116117 return tokens;

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

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