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 fileTake it
Improve input validation (#838)ehmickybefore adoption8ee137c
line 1why, one step back1import isPlainObject from 'is-plain-obj';Improve handling of `encoding` option (#926)ehmickybefore adoptiond786fcf
lines 2–3why, one step back2import {isUint8Array, uint8ArrayToString} from './encoding.js';3import {isSubprocess} from './utils.js';Split files (#837)ehmickybefore adoption672c2d7
lines 4–5why, one step back4import {execa} from './async.js';5import {execaSync} from './sync.js';Split files (#791)ehmickybefore adoption932fb26
line 6why, one step back6Improve `.pipe()` method (#859)ehmickybefore adoption2ef9e86
line 7why, one step back7export const create$ = options => {Split files (#837)ehmickybefore adoption672c2d7
line 8why, one step back8 function $(templatesOrOptions, ...expressions) {Improve input validation (#838)ehmickybefore adoption8ee137c
line 9why, one step back9 if (isPlainObject(templatesOrOptions)) {Split files (#837)ehmickybefore adoption672c2d7
lines 10–11why, one step back10 return create$({...options, ...templatesOrOptions});11 }Split files (#791)ehmickybefore adoption932fb26
line 12why, one step back12Improve input validation (#838)ehmickybefore adoption8ee137c
lines 13–16why, one step back13 if (!Array.isArray(templatesOrOptions)) {14 throw new TypeError('Please use either $(option) or $`command`.');15 }16Split files (#837)ehmickybefore adoption672c2d7
line 17why, one step back17 const [file, ...args] = parseTemplates(templatesOrOptions, expressions);Improve `.pipe()` method (#859)ehmickybefore adoption2ef9e86
line 18why, one step back18 return execa(file, args, normalizeScriptOptions(options));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$();`.pipe()` shortcut for `$` (#840)ehmickybefore adoption316f562
line 40why, one step back40Split files (#837)ehmickybefore adoption672c2d7
lines 41–42why, one step back41const parseTemplates = (templates, expressions) => {42 let tokens = [];Split files (#791)ehmickybefore adoption932fb26
line 43why, one step back43Split files (#837)ehmickybefore adoption672c2d7
lines 44–47why, one step back44 for (const [index, template] of templates.entries()) {45 tokens = parseTemplate({templates, expressions, tokens, index, template});46 }47Allow newlines in `$` (#843)ehmickybefore adoption1315486
lines 48–51why, one step back48 if (tokens.length === 0) {49 throw new TypeError('Template script must not be empty');50 }51Split files (#837)ehmickybefore adoption672c2d7
lines 52–54why, one step back52 return tokens;53};54Split files (#791)ehmickybefore adoption932fb26
line 55why, one step back55const parseTemplate = ({templates, expressions, tokens, index, template}) => {Allow newlines in `$` (#843)ehmickybefore adoption1315486
lines 56–58why, one step back56 if (template === undefined) {57 throw new TypeError(`Invalid backslash sequence: ${templates.raw[index]}`);58 }Split files (#791)ehmickybefore adoption932fb26
line 59why, one step back59Allow newlines in `$` (#843)ehmickybefore adoption1315486
lines 60–62why, one step back60 const {nextTokens, leadingWhitespaces, trailingWhitespaces} = splitByWhitespaces(template, templates.raw[index]);61 const newTokens = concatTokens(tokens, nextTokens, leadingWhitespaces);62Split files (#791)ehmickybefore adoption932fb26
lines 63–70why, one step back63 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 back71 return concatTokens(newTokens, expressionTokens, trailingWhitespaces);Split files (#791)ehmickybefore adoption932fb26
lines 72–73why, one step back72};73Allow newlines in `$` (#843)ehmickybefore adoption1315486
lines 74–83why, one step back74// 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 back84Allow newlines in `$` (#843)ehmickybefore adoption1315486
lines 85–128why, one step back85 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 === 0Split files (#837)ehmickybefore adoption672c2d7
lines 129–140why, one step back129 ? [...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 back141 }142Split files (#837)ehmickybefore adoption672c2d7
lines 143–149why, one step back143 if (typeOfExpression === 'number') {144 return String(expression);145 }146147 if (148 typeOfExpression === 'object'149 && expression !== nullRename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 150why, one step back150 && !isSubprocess(expression)Split files (#837)ehmickybefore adoption672c2d7
lines 151–158why, one step back151 && 'stdout' in expression152 ) {153 const typeOfStdout = typeof expression.stdout;154155 if (typeOfStdout === 'string') {156 return expression.stdout;157 }158Use `Uint8Array` instead of `Buffer` (#915)ehmickybefore adoption410bac3
lines 159–160why, one step back159 if (isUint8Array(expression.stdout)) {160 return uint8ArrayToString(expression.stdout);Split files (#837)ehmickybefore adoption672c2d7
lines 161–166why, one step back161 }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 back167};168Split files (#837)ehmickybefore adoption672c2d7
lines 169–177why, one step back169const 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 : {};