command.js
This file
Path
lib/command.js
118 lines · 2.9 KiB · JavaScript
Last changed by
Make `buffer` encoding option return `Uint8Array` instead of `Buffer` (#586)Youngbefore adoption1afeb81
History of this fileTake it
Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251
lines 1–2why, one step back1import {ChildProcess} from 'node:child_process';2Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc
line 3why, one step back3const normalizeArgs = (file, args = []) => {Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b
line 4why, one step back4 if (!Array.isArray(args)) {Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc
line 5why, one step back5 return [file];Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b
lines 6–7why, one step back6 }7Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc
line 8why, one step back8 return [file, ...args];Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b
lines 9–10why, one step back9};10Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc
lines 11–13why, one step back11const NO_ESCAPE_REGEXP = /^[\w.-]+$/;1213const escapeArg = arg => {Fix error message when user passes a single array argument (#468)ehmickybefore adoption2b9c0e1
line 14why, one step back14 if (typeof arg !== 'string' || NO_ESCAPE_REGEXP.test(arg)) {Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc
lines 15–17why, one step back15 return arg;16 }17Require Node.js 16 (#569)ehmickybefore adoption65992b4
line 18why, one step back18 return `"${arg.replaceAll('"', '\\"')}"`;Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc
lines 19–20why, one step back19};20Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 21why, one step back21export const joinCommand = (file, args) => normalizeArgs(file, args).join(' ');Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc
line 22why, one step back22Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 23why, one step back23export const getEscapedCommand = (file, args) => normalizeArgs(file, args).map(arg => escapeArg(arg)).join(' ');Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc
lines 24–26why, one step back2425const SPACES_REGEXP = / +/g;26Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
lines 27–28why, one step back27// Handle `execaCommand()`28export const parseCommand = command => {Avoid using `Array#reduce` (#436)Federicobefore adoptioncfcc2dc
lines 29–31why, one step back29 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 delimiterRequire Node.js 16 (#569)ehmickybefore adoption65992b4
line 32why, one step back32 const previousToken = tokens.at(-1);Avoid using `Array#reduce` (#436)Federicobefore adoptioncfcc2dc
lines 33–41why, one step back33 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;Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b
line 42why, one step back42};Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c
line 43why, one step back43Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251
lines 44–66why, one step back44const 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 }66Make `buffer` encoding option return `Uint8Array` instead of `Buffer` (#586)Youngbefore adoption1afeb81
lines 67–68why, one step back67 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 back69 }7071 throw new TypeError(`Unexpected "${typeOfStdout}" stdout in template expression`);72 }7374 throw new TypeError(`Unexpected "${typeOfExpression}" in template expression`);75};76Fix argument concatenation with `$` (#553)ehmickybefore adoption07585d0
lines 77–80why, one step back77const concatTokens = (tokens, nextTokens, isNew) => isNew || tokens.length === 0 || nextTokens.length === 078 ? [...tokens, ...nextTokens]79 : [80 ...tokens.slice(0, -1),Require Node.js 16 (#569)ehmickybefore adoption65992b4
line 81why, one step back81 `${tokens.at(-1)}${nextTokens[0]}`,Fix argument concatenation with `$` (#553)ehmickybefore adoption07585d0
lines 82–85why, one step back82 ...nextTokens.slice(1),83 ];8485const parseTemplate = ({templates, expressions, tokens, index, template}) => {Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c
lines 86–87why, one step back86 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 back88 const newTokens = concatTokens(89 tokens,90 templateTokens,91 templateString.startsWith(' '),92 );Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c
lines 93–94why, one step back9394 if (index === expressions.length) {Fix argument concatenation with `$` (#553)ehmickybefore adoption07585d0
line 95why, one step back95 return newTokens;Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c
lines 96–98why, one step back96 }9798 const expression = expressions[index];Fix argument concatenation with `$` (#553)ehmickybefore adoption07585d0
lines 99–107why, one step back99 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 back108Fix argument concatenation with `$` (#553)ehmickybefore adoption07585d0
lines 109–116why, one step back109export 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 back117};118