Skip to content
Myr

script.js

This file

Path

lib/script.js

177 lines · 5.1 KiB · JavaScript

Last changed by

Improve handling of `encoding` option (#926)ehmickybefore adoptiond786fcf

History of this file

Take it

Improve input validation (#838)ehmickybefore adoption8ee137c

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

Improve handling of `encoding` option (#926)ehmickybefore adoptiond786fcf

lines 2–3why, one step back
2import {isUint8Array, uint8ArrayToString} from './encoding.js';
3import {isSubprocess} from './utils.js';

Split files (#837)ehmickybefore adoption672c2d7

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

Split files (#791)ehmickybefore adoption932fb26

line 6why, one step back
6

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

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

Split files (#837)ehmickybefore adoption672c2d7

line 8why, one step back
8 function $(templatesOrOptions, ...expressions) {

Improve input validation (#838)ehmickybefore adoption8ee137c

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

Split files (#837)ehmickybefore adoption672c2d7

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

Split files (#791)ehmickybefore adoption932fb26

line 12why, one step back
12

Improve input validation (#838)ehmickybefore adoption8ee137c

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

Split files (#837)ehmickybefore adoption672c2d7

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

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

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

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$();

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

line 40why, one step back
40

Split files (#837)ehmickybefore adoption672c2d7

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

Split files (#791)ehmickybefore adoption932fb26

line 43why, one step back
43

Split files (#837)ehmickybefore adoption672c2d7

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

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

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

Split files (#837)ehmickybefore adoption672c2d7

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

Split files (#791)ehmickybefore adoption932fb26

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

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

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

Split files (#791)ehmickybefore adoption932fb26

line 59why, one step back
59

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

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

Split files (#791)ehmickybefore adoption932fb26

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

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

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

Split files (#791)ehmickybefore adoption932fb26

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

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

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

Split files (#791)ehmickybefore adoption932fb26

line 84why, one step back
84

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

lines 85–128why, one step back
85 const nextTokens = [];
86 let templateStart = 0;87 const leadingWhitespaces = DELIMITERS.has(rawTemplate[0]);8889 for (90 let templateIndex = 0, rawIndex = 0;91 templateIndex < template.length;92 templateIndex += 1, rawIndex += 193 ) {94 const rawCharacter = rawTemplate[rawIndex];95 if (DELIMITERS.has(rawCharacter)) {96 if (templateStart !== templateIndex) {97 nextTokens.push(template.slice(templateStart, templateIndex));98 }99100 templateStart = templateIndex + 1;101 } else if (rawCharacter === '\\') {102 const nextRawCharacter = rawTemplate[rawIndex + 1];103 if (nextRawCharacter === 'u' && rawTemplate[rawIndex + 2] === '{') {104 rawIndex = rawTemplate.indexOf('}', rawIndex + 3);105 } else {106 rawIndex += ESCAPE_LENGTH[nextRawCharacter] ?? 1;107 }108 }109 }110111 const trailingWhitespaces = templateStart === template.length;112 if (!trailingWhitespaces) {113 nextTokens.push(template.slice(templateStart));114 }115116 return {nextTokens, leadingWhitespaces, trailingWhitespaces};117};118119const DELIMITERS = new Set([' ', '\t', '\r', '\n']);120121// Number of characters in backslash escape sequences: \0 \xXX or \uXXXX122// \cX is allowed in RegExps but not in strings123// Octal sequences are not allowed in strict mode124const ESCAPE_LENGTH = {x: 3, u: 5};125126const concatTokens = (tokens, nextTokens, isSeparated) => isSeparated127 || tokens.length === 0128 || nextTokens.length === 0

Split files (#837)ehmickybefore adoption672c2d7

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

Split files (#791)ehmickybefore adoption932fb26

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

Split files (#837)ehmickybefore adoption672c2d7

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

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 150why, one step back
150 && !isSubprocess(expression)

Split files (#837)ehmickybefore adoption672c2d7

lines 151–158why, one step back
151 && 'stdout' in expression
152 ) {153 const typeOfStdout = typeof expression.stdout;154155 if (typeOfStdout === 'string') {156 return expression.stdout;157 }158

Use `Uint8Array` instead of `Buffer` (#915)ehmickybefore adoption410bac3

lines 159–160why, one step back
159 if (isUint8Array(expression.stdout)) {
160 return uint8ArrayToString(expression.stdout);

Split files (#837)ehmickybefore adoption672c2d7

lines 161–166why, one step back
161 }
162163 throw new TypeError(`Unexpected "${typeOfStdout}" stdout in template expression`);164 }165166 throw new TypeError(`Unexpected "${typeOfExpression}" in template expression`);

Split files (#791)ehmickybefore adoption932fb26

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

Split files (#837)ehmickybefore adoption672c2d7

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