Skip to content
Myr

command.js

This file

Path

lib/command.js

118 lines · 2.9 KiB · JavaScript

Last changed by

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Require Node.js 16 (#569)ehmickybefore adoption65992b4

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

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

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

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

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

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

line 22why, one step back
22

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

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

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

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

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

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

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

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

Require Node.js 16 (#569)ehmickybefore adoption65992b4

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

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

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

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

line 42why, one step back
42};

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

line 43why, one step back
43

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

lines 44–66why, one step back
44const parseExpression = expression => {
45 const typeOfExpression = typeof expression;4647 if (typeOfExpression === 'string') {48 return expression;49 }5051 if (typeOfExpression === 'number') {52 return String(expression);53 }5455 if (56 typeOfExpression === 'object'57 && expression !== null58 && !(expression instanceof ChildProcess)59 && 'stdout' in expression60 ) {61 const typeOfStdout = typeof expression.stdout;6263 if (typeOfStdout === 'string') {64 return expression.stdout;65 }66

Make `buffer` encoding option return `Uint8Array` instead of `Buffer` (#586)Youngbefore adoption1afeb81

lines 67–68why, one step back
67 if (ArrayBuffer.isView(expression.stdout)) {
68 return new TextDecoder().decode(expression.stdout);

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

lines 69–76why, one step back
69 }
7071 throw new TypeError(`Unexpected "${typeOfStdout}" stdout in template expression`);72 }7374 throw new TypeError(`Unexpected "${typeOfExpression}" in template expression`);75};76

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

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

Require Node.js 16 (#569)ehmickybefore adoption65992b4

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

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

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

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

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

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

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

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

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

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

line 95why, one step back
95 return newTokens;

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

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

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

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

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

line 108why, one step back
108

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

lines 109–116why, one step back
109export const parseTemplates = (templates, expressions) => {
110 let tokens = [];111112 for (const [index, template] of templates.entries()) {113 tokens = parseTemplate({templates, expressions, tokens, index, template});114 }115116 return tokens;

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

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