script.js
This file
Path
lib/script.js
154 lines · 4.3 KiB · JavaScript
Last changed by
Fix calling script `.pipe` multiple times (#846)ehmickybefore adoptionbd547e4
History of this fileTake it
Improve input validation (#838)ehmickybefore adoption8ee137c
line 1why, one step back1import isPlainObject from 'is-plain-obj';`.pipe()` shortcut for `$` (#840)ehmickybefore adoption316f562
line 2why, one step back2import {isBinary, binaryToString, isChildProcess, isExecaChildProcess} from './utils.js';Split files (#837)ehmickybefore adoption672c2d7
lines 3–4why, one step back3import {execa} from './async.js';4import {execaSync} from './sync.js';Split files (#791)ehmickybefore adoption932fb26
line 5why, one step back5Split files (#837)ehmickybefore adoption672c2d7
lines 6–7why, one step back6const create$ = options => {7 function $(templatesOrOptions, ...expressions) {Improve input validation (#838)ehmickybefore adoption8ee137c
line 8why, one step back8 if (isPlainObject(templatesOrOptions)) {Split files (#837)ehmickybefore adoption672c2d7
lines 9–10why, one step back9 return create$({...options, ...templatesOrOptions});10 }Split files (#791)ehmickybefore adoption932fb26
line 11why, one step back11Improve input validation (#838)ehmickybefore adoption8ee137c
lines 12–15why, one step back12 if (!Array.isArray(templatesOrOptions)) {13 throw new TypeError('Please use either $(option) or $`command`.');14 }15Split files (#837)ehmickybefore adoption672c2d7
line 16why, one step back16 const [file, ...args] = parseTemplates(templatesOrOptions, expressions);`.pipe()` shortcut for `$` (#840)ehmickybefore adoption316f562
line 17why, one step back17 const childProcess = execa(file, args, normalizeScriptOptions(options));Fix calling script `.pipe` multiple times (#846)ehmickybefore adoptionbd547e4
line 18why, one step back18 return scriptWithPipe(childProcess);Split files (#791)ehmickybefore adoption932fb26
lines 19–20why, one step back19 }20Split files (#837)ehmickybefore adoption672c2d7
line 21why, one step back21 $.sync = (templates, ...expressions) => {Improve input validation (#838)ehmickybefore adoption8ee137c
line 22why, one step back22 if (isPlainObject(templates)) {Split files (#837)ehmickybefore adoption672c2d7
line 23why, one step back23 throw new TypeError('Please use $(options).sync`command` instead of $.sync(options)`command`.');Improve input validation (#838)ehmickybefore adoption8ee137c
lines 24–27why, one step back24 }2526 if (!Array.isArray(templates)) {27 throw new TypeError('A template string must be used: $.sync`command`.');Split files (#791)ehmickybefore adoption932fb26
lines 28–29why, one step back28 }29Split files (#837)ehmickybefore adoption672c2d7
lines 30–32why, one step back30 const [file, ...args] = parseTemplates(templates, expressions);31 return execaSync(file, args, normalizeScriptOptions(options));32 };Split files (#791)ehmickybefore adoption932fb26
line 33why, one step back33Split files (#837)ehmickybefore adoption672c2d7
line 34why, one step back34 $.s = $.sync;Split files (#791)ehmickybefore adoption932fb26
line 35why, one step back35Split files (#837)ehmickybefore adoption672c2d7
line 36why, one step back36 return $;Split files (#791)ehmickybefore adoption932fb26
lines 37–38why, one step back37};38Split files (#837)ehmickybefore adoption672c2d7
line 39why, one step back39export const $ = create$();Split files (#791)ehmickybefore adoption932fb26
line 40why, one step back40Fix calling script `.pipe` multiple times (#846)ehmickybefore adoptionbd547e4
lines 41–48why, one step back41const scriptWithPipe = returnValue => {42 const originalPipe = returnValue.pipe.bind(returnValue);43 const pipeMethod = (...args) => scriptWithPipe(originalPipe(...args));44 returnValue.pipe = scriptPipe.bind(undefined, pipeMethod, {});45 return returnValue;46};4748const scriptPipe = (pipeMethod, options, firstArgument, ...args) => {`.pipe()` shortcut for `$` (#840)ehmickybefore adoption316f562
lines 49–53why, one step back49 if (isExecaChildProcess(firstArgument)) {50 if (Object.keys(options).length > 0) {51 throw new TypeError('Please use .pipe(options)`command` or .pipe($(options)`command`) instead of .pipe(options)($`command`).');52 }53Fix calling script `.pipe` multiple times (#846)ehmickybefore adoptionbd547e4
line 54why, one step back54 return pipeMethod(firstArgument, ...args);`.pipe()` shortcut for `$` (#840)ehmickybefore adoption316f562
lines 55–57why, one step back55 }5657 if (isPlainObject(firstArgument)) {Fix calling script `.pipe` multiple times (#846)ehmickybefore adoptionbd547e4
line 58why, one step back58 return scriptPipe.bind(undefined, pipeMethod, {...options, ...firstArgument});`.pipe()` shortcut for `$` (#840)ehmickybefore adoption316f562
lines 59–65why, one step back59 }6061 if (!Array.isArray(firstArgument)) {62 throw new TypeError('The first argument must be a template string, an options object, or an Execa child process.');63 }6465 const childProcess = create$({...options, stdin: 'pipe'})(firstArgument, ...args);Fix calling script `.pipe` multiple times (#846)ehmickybefore adoptionbd547e4
line 66why, one step back66 return pipeMethod(childProcess, options);`.pipe()` shortcut for `$` (#840)ehmickybefore adoption316f562
lines 67–68why, one step back67};68Split files (#837)ehmickybefore adoption672c2d7
lines 69–70why, one step back69const parseTemplates = (templates, expressions) => {70 let tokens = [];Split files (#791)ehmickybefore adoption932fb26
line 71why, one step back71Split files (#837)ehmickybefore adoption672c2d7
lines 72–78why, one step back72 for (const [index, template] of templates.entries()) {73 tokens = parseTemplate({templates, expressions, tokens, index, template});74 }7576 return tokens;77};78Split files (#791)ehmickybefore adoption932fb26
lines 79–102why, one step back79const parseTemplate = ({templates, expressions, tokens, index, template}) => {80 const templateString = template ?? templates.raw[index];81 const templateTokens = templateString.split(SPACES_REGEXP).filter(Boolean);82 const newTokens = concatTokens(83 tokens,84 templateTokens,85 templateString.startsWith(' '),86 );8788 if (index === expressions.length) {89 return newTokens;90 }9192 const expression = expressions[index];93 const expressionTokens = Array.isArray(expression)94 ? expression.map(expression => parseExpression(expression))95 : [parseExpression(expression)];96 return concatTokens(97 newTokens,98 expressionTokens,99 templateString.endsWith(' '),100 );101};102Split files (#837)ehmickybefore adoption672c2d7
line 103why, one step back103const SPACES_REGEXP = / +/g;Split files (#791)ehmickybefore adoption932fb26
line 104why, one step back104Split files (#837)ehmickybefore adoption672c2d7
lines 105–117why, one step back105const concatTokens = (tokens, nextTokens, isNew) => isNew || tokens.length === 0 || nextTokens.length === 0106 ? [...tokens, ...nextTokens]107 : [108 ...tokens.slice(0, -1),109 `${tokens.at(-1)}${nextTokens[0]}`,110 ...nextTokens.slice(1),111 ];112113const parseExpression = expression => {114 const typeOfExpression = typeof expression;115116 if (typeOfExpression === 'string') {117 return expression;Split files (#791)ehmickybefore adoption932fb26
lines 118–119why, one step back118 }119Split files (#837)ehmickybefore adoption672c2d7
lines 120–126why, one step back120 if (typeOfExpression === 'number') {121 return String(expression);122 }123124 if (125 typeOfExpression === 'object'126 && expression !== null`.pipe()` shortcut for `$` (#840)ehmickybefore adoption316f562
line 127why, one step back127 && !isChildProcess(expression)Split files (#837)ehmickybefore adoption672c2d7
lines 128–143why, one step back128 && 'stdout' in expression129 ) {130 const typeOfStdout = typeof expression.stdout;131132 if (typeOfStdout === 'string') {133 return expression.stdout;134 }135136 if (isBinary(expression.stdout)) {137 return binaryToString(expression.stdout);138 }139140 throw new TypeError(`Unexpected "${typeOfStdout}" stdout in template expression`);141 }142143 throw new TypeError(`Unexpected "${typeOfExpression}" in template expression`);Split files (#791)ehmickybefore adoption932fb26
lines 144–145why, one step back144};145Split files (#837)ehmickybefore adoption672c2d7
lines 146–154why, one step back146const normalizeScriptOptions = (options = {}) => ({147 preferLocal: true,148 ...normalizeScriptStdin(options),149 ...options,150});151152const normalizeScriptStdin = ({input, inputFile, stdio}) => input === undefined && inputFile === undefined && stdio === undefined153 ? {stdin: 'inherit'}154 : {};