Skip to content
Myr

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 file

Take it

Improve input validation (#838)ehmickybefore adoption8ee137c

line 1why, one step back
1import isPlainObject from 'is-plain-obj';

`.pipe()` shortcut for `$` (#840)ehmickybefore adoption316f562

line 2why, one step back
2import {isBinary, binaryToString, isChildProcess, isExecaChildProcess} from './utils.js';

Split files (#837)ehmickybefore adoption672c2d7

lines 3–4why, one step back
3import {execa} from './async.js';
4import {execaSync} from './sync.js';

Split files (#791)ehmickybefore adoption932fb26

line 5why, one step back
5

Split files (#837)ehmickybefore adoption672c2d7

lines 6–7why, one step back
6const create$ = options => {
7 function $(templatesOrOptions, ...expressions) {

Improve input validation (#838)ehmickybefore adoption8ee137c

line 8why, one step back
8 if (isPlainObject(templatesOrOptions)) {

Split files (#837)ehmickybefore adoption672c2d7

lines 9–10why, one step back
9 return create$({...options, ...templatesOrOptions});
10 }

Split files (#791)ehmickybefore adoption932fb26

line 11why, one step back
11

Improve input validation (#838)ehmickybefore adoption8ee137c

lines 12–15why, one step back
12 if (!Array.isArray(templatesOrOptions)) {
13 throw new TypeError('Please use either $(option) or $`command`.');14 }15

Split files (#837)ehmickybefore adoption672c2d7

line 16why, one step back
16 const [file, ...args] = parseTemplates(templatesOrOptions, expressions);

`.pipe()` shortcut for `$` (#840)ehmickybefore adoption316f562

line 17why, one step back
17 const childProcess = execa(file, args, normalizeScriptOptions(options));

Fix calling script `.pipe` multiple times (#846)ehmickybefore adoptionbd547e4

line 18why, one step back
18 return scriptWithPipe(childProcess);

Split files (#791)ehmickybefore adoption932fb26

lines 19–20why, one step back
19 }
20

Split files (#837)ehmickybefore adoption672c2d7

line 21why, one step back
21 $.sync = (templates, ...expressions) => {

Improve input validation (#838)ehmickybefore adoption8ee137c

line 22why, one step back
22 if (isPlainObject(templates)) {

Split files (#837)ehmickybefore adoption672c2d7

line 23why, one step back
23 throw new TypeError('Please use $(options).sync`command` instead of $.sync(options)`command`.');

Improve input validation (#838)ehmickybefore adoption8ee137c

lines 24–27why, one step back
24 }
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 back
28 }
29

Split files (#837)ehmickybefore adoption672c2d7

lines 30–32why, one step back
30 const [file, ...args] = parseTemplates(templates, expressions);
31 return execaSync(file, args, normalizeScriptOptions(options));32 };

Split files (#791)ehmickybefore adoption932fb26

line 33why, one step back
33

Split files (#837)ehmickybefore adoption672c2d7

line 34why, one step back
34 $.s = $.sync;

Split files (#791)ehmickybefore adoption932fb26

line 35why, one step back
35

Split files (#837)ehmickybefore adoption672c2d7

line 36why, one step back
36 return $;

Split files (#791)ehmickybefore adoption932fb26

lines 37–38why, one step back
37};
38

Split files (#837)ehmickybefore adoption672c2d7

line 39why, one step back
39export const $ = create$();

Split files (#791)ehmickybefore adoption932fb26

line 40why, one step back
40

Fix calling script `.pipe` multiple times (#846)ehmickybefore adoptionbd547e4

lines 41–48why, one step back
41const 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 back
49 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 }53

Fix calling script `.pipe` multiple times (#846)ehmickybefore adoptionbd547e4

line 54why, one step back
54 return pipeMethod(firstArgument, ...args);

`.pipe()` shortcut for `$` (#840)ehmickybefore adoption316f562

lines 55–57why, one step back
55 }
5657 if (isPlainObject(firstArgument)) {

Fix calling script `.pipe` multiple times (#846)ehmickybefore adoptionbd547e4

line 58why, one step back
58 return scriptPipe.bind(undefined, pipeMethod, {...options, ...firstArgument});

`.pipe()` shortcut for `$` (#840)ehmickybefore adoption316f562

lines 59–65why, one step back
59 }
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 back
66 return pipeMethod(childProcess, options);

`.pipe()` shortcut for `$` (#840)ehmickybefore adoption316f562

lines 67–68why, one step back
67};
68

Split files (#837)ehmickybefore adoption672c2d7

lines 69–70why, one step back
69const parseTemplates = (templates, expressions) => {
70 let tokens = [];

Split files (#791)ehmickybefore adoption932fb26

line 71why, one step back
71

Split files (#837)ehmickybefore adoption672c2d7

lines 72–78why, one step back
72 for (const [index, template] of templates.entries()) {
73 tokens = parseTemplate({templates, expressions, tokens, index, template});74 }7576 return tokens;77};78

Split files (#791)ehmickybefore adoption932fb26

lines 79–102why, one step back
79const 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};102

Split files (#837)ehmickybefore adoption672c2d7

line 103why, one step back
103const SPACES_REGEXP = / +/g;

Split files (#791)ehmickybefore adoption932fb26

line 104why, one step back
104

Split files (#837)ehmickybefore adoption672c2d7

lines 105–117why, one step back
105const concatTokens = (tokens, nextTokens, isNew) => isNew || tokens.length === 0 || nextTokens.length === 0
106 ? [...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 back
118 }
119

Split files (#837)ehmickybefore adoption672c2d7

lines 120–126why, one step back
120 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 back
127 && !isChildProcess(expression)

Split files (#837)ehmickybefore adoption672c2d7

lines 128–143why, one step back
128 && 'stdout' in expression
129 ) {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 back
144};
145

Split files (#837)ehmickybefore adoption672c2d7

lines 146–154why, one step back
146const 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 : {};