Skip to content
Myr

command.js

This file

Path

lib/command.js

96 lines · 2.5 KiB · JavaScript

Last changed by

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

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

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

lines 79–89why, one step back
79const parseTemplate = (template, index, templates, expressions) => {
80 const templateString = template ?? templates.raw[index];81 const templateTokens = templateString.split(SPACES_REGEXP).filter(Boolean);8283 if (index === expressions.length) {84 return templateTokens;85 }8687 const expression = expressions[index];8889 return Array.isArray(expression)

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

lines 90–91why, one step back
90 ? [...templateTokens, ...expression.map(expression => parseExpression(expression))]
91 : [...templateTokens, parseExpression(expression)];

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

lines 92–96why, one step back
92};
9394export const parseTemplates = (templates, expressions) => templates.flatMap(95 (template, index) => parseTemplate(template, index, templates, expressions),96);