command.js
This file
Path
lib/command.js
119 lines · 2.9 KiB · JavaScript
Last changed by
Require Node.js 16 (#569)ehmickybefore adoption65992b4
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–14why, one step back12const NO_ESCAPE_REGEXP = /^[\w.-]+$/;1314const escapeArg = arg => {Fix error message when user passes a single array argument (#468)ehmickybefore adoption2b9c0e1
line 15why, one step back15 if (typeof arg !== 'string' || NO_ESCAPE_REGEXP.test(arg)) {Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc
lines 16–18why, one step back16 return arg;17 }18Require Node.js 16 (#569)ehmickybefore adoption65992b4
line 19why, one step back19 return `"${arg.replaceAll('"', '\\"')}"`;Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc
lines 20–21why, one step back20};21Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 22why, one step back22export const joinCommand = (file, args) => normalizeArgs(file, args).join(' ');Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc
line 23why, one step back23Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 24why, one step back24export const getEscapedCommand = (file, args) => normalizeArgs(file, args).map(arg => escapeArg(arg)).join(' ');Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc
lines 25–27why, one step back2526const SPACES_REGEXP = / +/g;27Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
lines 28–29why, one step back28// Handle `execaCommand()`29export const parseCommand = command => {Avoid using `Array#reduce` (#436)Federicobefore adoptioncfcc2dc
lines 30–32why, one step back30 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 delimiterRequire Node.js 16 (#569)ehmickybefore adoption65992b4
line 33why, one step back33 const previousToken = tokens.at(-1);Avoid using `Array#reduce` (#436)Federicobefore adoptioncfcc2dc
lines 34–42why, one step back34 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;Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b
line 43why, one step back43};Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c
line 44why, one step back44Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251
lines 45–77why, one step back45const 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 }6768 if (Buffer.isBuffer(expression.stdout)) {69 return expression.stdout.toString();70 }7172 throw new TypeError(`Unexpected "${typeOfStdout}" stdout in template expression`);73 }7475 throw new TypeError(`Unexpected "${typeOfExpression}" in template expression`);76};77Fix argument concatenation with `$` (#553)ehmickybefore adoption07585d0
lines 78–81why, one step back78const concatTokens = (tokens, nextTokens, isNew) => isNew || tokens.length === 0 || nextTokens.length === 079 ? [...tokens, ...nextTokens]80 : [81 ...tokens.slice(0, -1),Require Node.js 16 (#569)ehmickybefore adoption65992b4
line 82why, one step back82 `${tokens.at(-1)}${nextTokens[0]}`,Fix argument concatenation with `$` (#553)ehmickybefore adoption07585d0
lines 83–86why, one step back83 ...nextTokens.slice(1),84 ];8586const parseTemplate = ({templates, expressions, tokens, index, template}) => {Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c
lines 87–88why, one step back87 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 back89 const newTokens = concatTokens(90 tokens,91 templateTokens,92 templateString.startsWith(' '),93 );Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c
lines 94–95why, one step back9495 if (index === expressions.length) {Fix argument concatenation with `$` (#553)ehmickybefore adoption07585d0
line 96why, one step back96 return newTokens;Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c
lines 97–99why, one step back97 }9899 const expression = expressions[index];Fix argument concatenation with `$` (#553)ehmickybefore adoption07585d0
lines 100–108why, one step back100 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 back109Fix argument concatenation with `$` (#553)ehmickybefore adoption07585d0
lines 110–117why, one step back110export 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 back118};119