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 fileTake it
Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251
lines 1–3why, one step back1import {Buffer} from 'node:buffer';2import {ChildProcess} from 'node:child_process';3Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc
line 4why, one step back4const normalizeArgs = (file, args = []) => {Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b
line 5why, one step back5 if (!Array.isArray(args)) {Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc
line 6why, one step back6 return [file];Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b
lines 7–8why, one step back7 }8Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc
line 9why, one step back9 return [file, ...args];Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b
lines 10–11why, one step back10};11Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc
lines 12–15why, one step back12const 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 back16 if (typeof arg !== 'string' || NO_ESCAPE_REGEXP.test(arg)) {Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc
lines 17–22why, one step back17 return arg;18 }1920 return `"${arg.replace(DOUBLE_QUOTES_REGEXP, '\\"')}"`;21};22Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 23why, one step back23export const joinCommand = (file, args) => normalizeArgs(file, args).join(' ');Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc
line 24why, one step back24Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 25why, one step back25export const getEscapedCommand = (file, args) => normalizeArgs(file, args).map(arg => escapeArg(arg)).join(' ');Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc
lines 26–28why, one step back2627const SPACES_REGEXP = / +/g;28Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
lines 29–30why, one step back29// Handle `execaCommand()`30export const parseCommand = command => {Avoid using `Array#reduce` (#436)Federicobefore adoptioncfcc2dc
lines 31–43why, one step back31 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 back44};Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c
line 45why, one step back45Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251
lines 46–78why, one step back46const 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};78Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c
lines 79–89why, one step back79const 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 back90 ? [...templateTokens, ...expression.map(expression => parseExpression(expression))]91 : [...templateTokens, parseExpression(expression)];Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c
lines 92–96why, one step back92};9394export const parseTemplates = (templates, expressions) => templates.flatMap(95 (template, index) => parseTemplate(template, index, templates, expressions),96);