Skip to content
Myr

script.js

This file

Path

lib/script.js

176 lines · 5.1 KiB · JavaScript

Last changed by

Improve `.pipe()` method (#859)ehmickybefore adoption2ef9e86

History of this file

Take it

Improve input validation (#838)ehmickybefore adoption8ee137c

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

Fix errors thrown by `.pipe()` (#854)ehmickybefore adoption46ba4b4

line 2why, one step back
2import {isBinary, binaryToString, isChildProcess} 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

Improve `.pipe()` method (#859)ehmickybefore adoption2ef9e86

line 6why, one step back
6export const create$ = options => {

Split files (#837)ehmickybefore adoption672c2d7

line 7why, one step back
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);

Improve `.pipe()` method (#859)ehmickybefore adoption2ef9e86

line 17why, one step back
17 return execa(file, args, normalizeScriptOptions(options));

Split files (#791)ehmickybefore adoption932fb26

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

Split files (#837)ehmickybefore adoption672c2d7

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

Improve input validation (#838)ehmickybefore adoption8ee137c

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

Split files (#837)ehmickybefore adoption672c2d7

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

Improve input validation (#838)ehmickybefore adoption8ee137c

lines 23–26why, one step back
23 }
2425 if (!Array.isArray(templates)) {26 throw new TypeError('A template string must be used: $.sync`command`.');

Split files (#791)ehmickybefore adoption932fb26

lines 27–28why, one step back
27 }
28

Split files (#837)ehmickybefore adoption672c2d7

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

Split files (#791)ehmickybefore adoption932fb26

line 32why, one step back
32

Split files (#837)ehmickybefore adoption672c2d7

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

Split files (#791)ehmickybefore adoption932fb26

line 34why, one step back
34

Split files (#837)ehmickybefore adoption672c2d7

line 35why, one step back
35 return $;

Split files (#791)ehmickybefore adoption932fb26

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

Split files (#837)ehmickybefore adoption672c2d7

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

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

line 39why, one step back
39

Split files (#837)ehmickybefore adoption672c2d7

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

Split files (#791)ehmickybefore adoption932fb26

line 42why, one step back
42

Split files (#837)ehmickybefore adoption672c2d7

lines 43–46why, one step back
43 for (const [index, template] of templates.entries()) {
44 tokens = parseTemplate({templates, expressions, tokens, index, template});45 }46

Allow newlines in `$` (#843)ehmickybefore adoption1315486

lines 47–50why, one step back
47 if (tokens.length === 0) {
48 throw new TypeError('Template script must not be empty');49 }50

Split files (#837)ehmickybefore adoption672c2d7

lines 51–53why, one step back
51 return tokens;
52};53

Split files (#791)ehmickybefore adoption932fb26

line 54why, one step back
54const parseTemplate = ({templates, expressions, tokens, index, template}) => {

Allow newlines in `$` (#843)ehmickybefore adoption1315486

lines 55–57why, one step back
55 if (template === undefined) {
56 throw new TypeError(`Invalid backslash sequence: ${templates.raw[index]}`);57 }

Split files (#791)ehmickybefore adoption932fb26

line 58why, one step back
58

Allow newlines in `$` (#843)ehmickybefore adoption1315486

lines 59–61why, one step back
59 const {nextTokens, leadingWhitespaces, trailingWhitespaces} = splitByWhitespaces(template, templates.raw[index]);
60 const newTokens = concatTokens(tokens, nextTokens, leadingWhitespaces);61

Split files (#791)ehmickybefore adoption932fb26

lines 62–69why, one step back
62 if (index === expressions.length) {
63 return newTokens;64 }6566 const expression = expressions[index];67 const expressionTokens = Array.isArray(expression)68 ? expression.map(expression => parseExpression(expression))69 : [parseExpression(expression)];

Allow newlines in `$` (#843)ehmickybefore adoption1315486

line 70why, one step back
70 return concatTokens(newTokens, expressionTokens, trailingWhitespaces);

Split files (#791)ehmickybefore adoption932fb26

lines 71–72why, one step back
71};
72

Allow newlines in `$` (#843)ehmickybefore adoption1315486

lines 73–82why, one step back
73// Like `string.split(/[ \t\r\n]+/)` except newlines and tabs are:
74// - ignored when input as a backslash sequence like: `echo foo\n bar`75// - not ignored when input directly76// The only way to distinguish those in JavaScript is to use a tagged template and compare:77// - the first array argument, which does not escape backslash sequences78// - its `raw` property, which escapes them79const splitByWhitespaces = (template, rawTemplate) => {80 if (rawTemplate.length === 0) {81 return {nextTokens: [], leadingWhitespaces: false, trailingWhitespaces: false};82 }

Split files (#791)ehmickybefore adoption932fb26

line 83why, one step back
83

Allow newlines in `$` (#843)ehmickybefore adoption1315486

lines 84–127why, one step back
84 const nextTokens = [];
85 let templateStart = 0;86 const leadingWhitespaces = DELIMITERS.has(rawTemplate[0]);8788 for (89 let templateIndex = 0, rawIndex = 0;90 templateIndex < template.length;91 templateIndex += 1, rawIndex += 192 ) {93 const rawCharacter = rawTemplate[rawIndex];94 if (DELIMITERS.has(rawCharacter)) {95 if (templateStart !== templateIndex) {96 nextTokens.push(template.slice(templateStart, templateIndex));97 }9899 templateStart = templateIndex + 1;100 } else if (rawCharacter === '\\') {101 const nextRawCharacter = rawTemplate[rawIndex + 1];102 if (nextRawCharacter === 'u' && rawTemplate[rawIndex + 2] === '{') {103 rawIndex = rawTemplate.indexOf('}', rawIndex + 3);104 } else {105 rawIndex += ESCAPE_LENGTH[nextRawCharacter] ?? 1;106 }107 }108 }109110 const trailingWhitespaces = templateStart === template.length;111 if (!trailingWhitespaces) {112 nextTokens.push(template.slice(templateStart));113 }114115 return {nextTokens, leadingWhitespaces, trailingWhitespaces};116};117118const DELIMITERS = new Set([' ', '\t', '\r', '\n']);119120// Number of characters in backslash escape sequences: \0 \xXX or \uXXXX121// \cX is allowed in RegExps but not in strings122// Octal sequences are not allowed in strict mode123const ESCAPE_LENGTH = {x: 3, u: 5};124125const concatTokens = (tokens, nextTokens, isSeparated) => isSeparated126 || tokens.length === 0127 || nextTokens.length === 0

Split files (#837)ehmickybefore adoption672c2d7

lines 128–139why, one step back
128 ? [...tokens, ...nextTokens]
129 : [130 ...tokens.slice(0, -1),131 `${tokens.at(-1)}${nextTokens[0]}`,132 ...nextTokens.slice(1),133 ];134135const parseExpression = expression => {136 const typeOfExpression = typeof expression;137138 if (typeOfExpression === 'string') {139 return expression;

Split files (#791)ehmickybefore adoption932fb26

lines 140–141why, one step back
140 }
141

Split files (#837)ehmickybefore adoption672c2d7

lines 142–148why, one step back
142 if (typeOfExpression === 'number') {
143 return String(expression);144 }145146 if (147 typeOfExpression === 'object'148 && expression !== null

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

line 149why, one step back
149 && !isChildProcess(expression)

Split files (#837)ehmickybefore adoption672c2d7

lines 150–165why, one step back
150 && 'stdout' in expression
151 ) {152 const typeOfStdout = typeof expression.stdout;153154 if (typeOfStdout === 'string') {155 return expression.stdout;156 }157158 if (isBinary(expression.stdout)) {159 return binaryToString(expression.stdout);160 }161162 throw new TypeError(`Unexpected "${typeOfStdout}" stdout in template expression`);163 }164165 throw new TypeError(`Unexpected "${typeOfExpression}" in template expression`);

Split files (#791)ehmickybefore adoption932fb26

lines 166–167why, one step back
166};
167

Split files (#837)ehmickybefore adoption672c2d7

lines 168–176why, one step back
168const normalizeScriptOptions = (options = {}) => ({
169 preferLocal: true,170 ...normalizeScriptStdin(options),171 ...options,172});173174const normalizeScriptStdin = ({input, inputFile, stdio}) => input === undefined && inputFile === undefined && stdio === undefined175 ? {stdin: 'inherit'}176 : {};