Allow newlines in `$` (#843)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent bd547e4 · Browse files · 4 files·+403 added−235 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+403 added−235 removed
- tests:+312 added−221 removed
- other:+91 added−14 removed
Provenance
- Commit
1315486- Tree
6182583- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
bd547e4
4 files changed+403 added−235 removed
modified:docs/scripts.md+24 added
119 unmodified lines120 // Execa121 await $`echo example`;122 ```123 124+### Multiline commands125+126+```sh127+# Bash128+npm run build \129+ --example-flag-one \130+ --example-flag-two131+```132+133+```js134+// zx135+await $`npm run build ${[136+ '--example-flag-one',137+ '--example-flag-two',138+]}`;139+```140+141+```js142+// Execa143+await $`npm run build144+ --example-flag-one145+ --example-flag-two`146+```147+148 ### Subcommands149 150 ```sh151 # Bash552 unmodified linesmodified:lib/script.js+65 added-14 removed
71 unmodified lines72737475767778798081828380818283848586848586878889909192939495969798969798991009910010110310210310410510610710810911011111210511311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916045 unmodified lines71 unmodified lines for (const [index, template] of templates.entries()) { tokens = parseTemplate({templates, expressions, tokens, index, template}); }
if (tokens.length === 0) { throw new TypeError('Template script must not be empty'); }
return tokens;};
const parseTemplate = ({templates, expressions, tokens, index, template}) => { const templateString = template ?? templates.raw[index]; const templateTokens = templateString.split(SPACES_REGEXP).filter(Boolean); const newTokens = concatTokens( tokens, templateTokens, templateString.startsWith(' '), ); if (template === undefined) { throw new TypeError(`Invalid backslash sequence: ${templates.raw[index]}`); }
const {nextTokens, leadingWhitespaces, trailingWhitespaces} = splitByWhitespaces(template, templates.raw[index]); const newTokens = concatTokens(tokens, nextTokens, leadingWhitespaces);
if (index === expressions.length) { return newTokens; }
const expression = expressions[index]; const expressionTokens = Array.isArray(expression) ? expression.map(expression => parseExpression(expression)) : [parseExpression(expression)]; return concatTokens( newTokens, expressionTokens, templateString.endsWith(' '), ); return concatTokens(newTokens, expressionTokens, trailingWhitespaces);};
const SPACES_REGEXP = / +/g;// Like `string.split(/[ \t\r\n]+/)` except newlines and tabs are:// - ignored when input as a backslash sequence like: `echo foo\n bar`// - not ignored when input directly// The only way to distinguish those in JavaScript is to use a tagged template and compare:// - the first array argument, which does not escape backslash sequences// - its `raw` property, which escapes themconst splitByWhitespaces = (template, rawTemplate) => { if (rawTemplate.length === 0) { return {nextTokens: [], leadingWhitespaces: false, trailingWhitespaces: false}; }
const concatTokens = (tokens, nextTokens, isNew) => isNew || tokens.length === 0 || nextTokens.length === 0 const nextTokens = []; let templateStart = 0; const leadingWhitespaces = DELIMITERS.has(rawTemplate[0]);
for ( let templateIndex = 0, rawIndex = 0; templateIndex < template.length; templateIndex += 1, rawIndex += 1 ) { const rawCharacter = rawTemplate[rawIndex]; if (DELIMITERS.has(rawCharacter)) { if (templateStart !== templateIndex) { nextTokens.push(template.slice(templateStart, templateIndex)); }
templateStart = templateIndex + 1; } else if (rawCharacter === '\\') { const nextRawCharacter = rawTemplate[rawIndex + 1]; if (nextRawCharacter === 'u' && rawTemplate[rawIndex + 2] === '{') { rawIndex = rawTemplate.indexOf('}', rawIndex + 3); } else { rawIndex += ESCAPE_LENGTH[nextRawCharacter] ?? 1; } } }
const trailingWhitespaces = templateStart === template.length; if (!trailingWhitespaces) { nextTokens.push(template.slice(templateStart)); }
return {nextTokens, leadingWhitespaces, trailingWhitespaces};};
const DELIMITERS = new Set([' ', '\t', '\r', '\n']);
// Number of characters in backslash escape sequences: \0 \xXX or \uXXXX// \cX is allowed in RegExps but not in strings// Octal sequences are not allowed in strict modeconst ESCAPE_LENGTH = {x: 3, u: 5};
const concatTokens = (tokens, nextTokens, isSeparated) => isSeparated || tokens.length === 0 || nextTokens.length === 0 ? [...tokens, ...nextTokens] : [ ...tokens.slice(0, -1), `${tokens.at(-1)}${nextTokens[0]}`,45 unmodified linesmodified:test/script.js+312 added-221 removed
1 import {spawn} from 'node:child_process';2-import {inspect} from 'node:util';2 import test from 'ava';3 import {isStream} from 'is-stream';4 import {$} from '../index.js';5 import {setFixtureDir} from './helpers/fixtures-dir.js';6 import {foobarString} from './helpers/input.js';7 8 setFixtureDir();9 11-test('$', async t => {12- const {stdout} = await $`echo.js foo bar`;13- t.is(stdout, 'foo\nbar');14-});10+// Workaround since some text editors or IDEs do not allow inputting \r directly11+const escapedCall = string => {12+ const templates = [string];13+ templates.raw = [string];14+ return $(templates);15+};16 16-test('$ accepts options', async t => {17- const {stdout} = await $({stripFinalNewline: true})`noop.js foo`;18- t.is(stdout, 'foo');19-});17+const testScriptStdout = async (t, getChildProcess, expectedStdout) => {18+ const {stdout} = await getChildProcess();19+ t.is(stdout, expectedStdout);20+};21 21-test('$ allows string interpolation', async t => {22- const {stdout} = await $`echo.js foo ${'bar'}`;23- t.is(stdout, 'foo\nbar');24-});22+test('$ executes command', testScriptStdout, () => $`echo.js foo bar`, 'foo\nbar');23+test('$ accepts options', testScriptStdout, () => $({stripFinalNewline: true})`noop.js foo`, 'foo');24+test('$ allows number interpolation', testScriptStdout, () => $`echo.js 1 ${2}`, '1\n2');25+test('$ can concatenate multiple tokens', testScriptStdout, () => $`echo.js ${'foo'}bar${'foo'}`, 'foobarfoo');26+test('$ can use newlines and tab indentations', testScriptStdout, () => $`echo.js foo27+ bar`, 'foo\nbar');28+test('$ can use newlines and space indentations', testScriptStdout, () => $`echo.js foo29+ bar`, 'foo\nbar');30+test('$ can use Windows newlines and tab indentations', testScriptStdout, () => escapedCall('echo.js foo\r\n\tbar'), 'foo\nbar');31+test('$ can use Windows newlines and space indentations', testScriptStdout, () => escapedCall('echo.js foo\r\n bar'), 'foo\nbar');32+test('$ does not ignore comments in expressions', testScriptStdout, () => $`echo.js foo33+ ${/* This is a comment */''}34+ bar35+ ${/* This is another comment */''}36+ baz37+`, 'foo\n\nbar\n\nbaz');38+test('$ allows escaping spaces with interpolation', testScriptStdout, () => $`echo.js ${'foo bar'}`, 'foo bar');39+test('$ allows escaping spaces in commands with interpolation', testScriptStdout, () => $`${'command with space.js'} foo bar`, 'foo\nbar');40+test('$ trims', testScriptStdout, () => $` echo.js foo bar `, 'foo\nbar');41+test('$ allows array interpolation', testScriptStdout, () => $`echo.js ${['foo', 'bar']}`, 'foo\nbar');42+test('$ allows empty array interpolation', testScriptStdout, () => $`echo.js foo ${[]} bar`, 'foo\nbar');43+test('$ allows space escaped values in array interpolation', testScriptStdout, () => $`echo.js ${['foo', 'bar baz']}`, 'foo\nbar baz');44+test('$ can concatenate at the end of tokens followed by an array', testScriptStdout, () => $`echo.js foo${['bar', 'foo']}`, 'foobar\nfoo');45+test('$ can concatenate at the start of tokens followed by an array', testScriptStdout, () => $`echo.js ${['foo', 'bar']}foo`, 'foo\nbarfoo');46+test('$ can concatenate at the start and end of tokens followed by an array', testScriptStdout, () => $`echo.js foo${['bar', 'foo']}bar`, 'foobar\nfoobar');47+test('$ handles escaped newlines', testScriptStdout, () => $`echo.js a\48+b`, 'ab');49+test('$ handles backslashes at end of lines', testScriptStdout, () => $`echo.js a\\50+ b`, 'a\\\nb');51+test('$ handles double backslashes at end of lines', testScriptStdout, () => $`echo.js a\\\\52+ b`, 'a\\\\\nb');53+test('$ handles tokens - a', testScriptStdout, () => $`echo.js a`, 'a');54+test('$ handles expressions - a', testScriptStdout, () => $`echo.js ${'a'}`, 'a');55+test('$ handles tokens - abc', testScriptStdout, () => $`echo.js abc`, 'abc');56+test('$ handles expressions - abc', testScriptStdout, () => $`echo.js ${'abc'}`, 'abc');57+test('$ handles tokens - ""', testScriptStdout, () => $`echo.js`, '');58+test('$ handles expressions - ""', testScriptStdout, () => $`echo.js a ${''} b`, 'a\n\nb');59+test('$ splits tokens - ""', testScriptStdout, () => $`echo.js ab`, 'ab');60+test('$ splits expressions - ""', testScriptStdout, () => $`echo.js ${'a'}${'b'}`, 'ab');61+test('$ concatenates expressions - ""', testScriptStdout, () => $`echo.js a${'b'}c`, 'abc');62+test('$ handles tokens - " "', testScriptStdout, () => $`echo.js `, '');63+test('$ handles expressions - " "', testScriptStdout, () => $`echo.js ${' '}`, ' ');64+test('$ splits tokens - " "', testScriptStdout, () => $`echo.js a b`, 'a\nb');65+test('$ splits expressions - " "', testScriptStdout, () => $`echo.js ${'a'} ${'b'}`, 'a\nb');66+test('$ concatenates tokens - " "', testScriptStdout, () => $`echo.js a `, 'a');67+test('$ concatenates expressions - " "', testScriptStdout, () => $`echo.js ${'a'} `, 'a');68+test('$ handles tokens - " " (2 spaces)', testScriptStdout, () => $`echo.js `, '');69+test('$ handles expressions - " " (2 spaces)', testScriptStdout, () => $`echo.js ${' '}`, ' ');70+test('$ splits tokens - " " (2 spaces)', testScriptStdout, () => $`echo.js a b`, 'a\nb');71+test('$ splits expressions - " " (2 spaces)', testScriptStdout, () => $`echo.js ${'a'} ${'b'}`, 'a\nb');72+test('$ concatenates tokens - " " (2 spaces)', testScriptStdout, () => $`echo.js a `, 'a');73+test('$ concatenates expressions - " " (2 spaces)', testScriptStdout, () => $`echo.js ${'a'} `, 'a');74+test('$ handles tokens - " " (3 spaces)', testScriptStdout, () => $`echo.js `, '');75+test('$ handles expressions - " " (3 spaces)', testScriptStdout, () => $`echo.js ${' '}`, ' ');76+test('$ splits tokens - " " (3 spaces)', testScriptStdout, () => $`echo.js a b`, 'a\nb');77+test('$ splits expressions - " " (3 spaces)', testScriptStdout, () => $`echo.js ${'a'} ${'b'}`, 'a\nb');78+test('$ concatenates tokens - " " (3 spaces)', testScriptStdout, () => $`echo.js a `, 'a');79+test('$ concatenates expressions - " " (3 spaces)', testScriptStdout, () => $`echo.js ${'a'} `, 'a');80+test('$ handles tokens - \\t (no escape)', testScriptStdout, () => $`echo.js `, '');81+test('$ handles expressions - \\t (no escape)', testScriptStdout, () => $`echo.js ${' '}`, '\t');82+test('$ splits tokens - \\t (no escape)', testScriptStdout, () => $`echo.js a b`, 'a\nb');83+test('$ splits expressions - \\t (no escape)', testScriptStdout, () => $`echo.js ${'a'} ${'b'}`, 'a\nb');84+test('$ concatenates tokens - \\t (no escape)', testScriptStdout, () => $`echo.js a b`, 'a\nb');85+test('$ concatenates expressions - \\t (no escape)', testScriptStdout, () => $`echo.js ${'a'} b`, 'a\nb');86+test('$ handles tokens - \\t (escape)', testScriptStdout, () => $`echo.js \t`, '\t');87+test('$ handles expressions - \\t (escape)', testScriptStdout, () => $`echo.js ${'\t'}`, '\t');88+test('$ splits tokens - \\t (escape)', testScriptStdout, () => $`echo.js a\tb`, 'a\tb');89+test('$ splits expressions - \\t (escape)', testScriptStdout, () => $`echo.js ${'a'}\t${'b'}`, 'a\tb');90+test('$ concatenates tokens - \\t (escape)', testScriptStdout, () => $`echo.js \ta\t b`, '\ta\t\nb');91+test('$ concatenates expressions - \\t (escape)', testScriptStdout, () => $`echo.js \t${'a'}\t b`, '\ta\t\nb');92+test('$ handles tokens - \\n (no escape)', testScriptStdout, () => $`echo.js93+ `, '');94+test('$ handles expressions - \\n (no escape)', testScriptStdout, () => $`echo.js ${`95+`} `, '\n');96+test('$ splits tokens - \\n (no escape)', testScriptStdout, () => $`echo.js a97+ b`, 'a\nb');98+test('$ splits expressions - \\n (no escape)', testScriptStdout, () => $`echo.js ${'a'}99+ ${'b'}`, 'a\nb');100+test('$ concatenates tokens - \\n (no escape)', testScriptStdout, () => $`echo.js101+a102+ b`, 'a\nb');103+test('$ concatenates expressions - \\n (no escape)', testScriptStdout, () => $`echo.js104+${'a'}105+ b`, 'a\nb');106+test('$ handles tokens - \\n (escape)', testScriptStdout, () => $`echo.js \n `, '\n');107+test('$ handles expressions - \\n (escape)', testScriptStdout, () => $`echo.js ${'\n'} `, '\n');108+test('$ splits tokens - \\n (escape)', testScriptStdout, () => $`echo.js a\n b`, 'a\n\nb');109+test('$ splits expressions - \\n (escape)', testScriptStdout, () => $`echo.js ${'a'}\n ${'b'}`, 'a\n\nb');110+test('$ concatenates tokens - \\n (escape)', testScriptStdout, () => $`echo.js \na\n b`, '\na\n\nb');111+test('$ concatenates expressions - \\n (escape)', testScriptStdout, () => $`echo.js \n${'a'}\n b`, '\na\n\nb');112+test('$ handles tokens - \\r (no escape)', testScriptStdout, () => escapedCall('echo.js \r '), '');113+test('$ splits tokens - \\r (no escape)', testScriptStdout, () => escapedCall('echo.js a\rb'), 'a\nb');114+test('$ splits expressions - \\r (no escape)', testScriptStdout, () => escapedCall(`echo.js ${'a'}\r${'b'}`), 'a\nb');115+test('$ concatenates tokens - \\r (no escape)', testScriptStdout, () => escapedCall('echo.js \ra\r b'), 'a\nb');116+test('$ concatenates expressions - \\r (no escape)', testScriptStdout, () => escapedCall(`echo.js \r${'a'}\r b`), 'a\nb');117+test('$ splits tokens - \\r (escape)', testScriptStdout, () => $`echo.js a\r b`, 'a\r\nb');118+test('$ splits expressions - \\r (escape)', testScriptStdout, () => $`echo.js ${'a'}\r ${'b'}`, 'a\r\nb');119+test('$ concatenates tokens - \\r (escape)', testScriptStdout, () => $`echo.js \ra\r b`, '\ra\r\nb');120+test('$ concatenates expressions - \\r (escape)', testScriptStdout, () => $`echo.js \r${'a'}\r b`, '\ra\r\nb');121+test('$ handles tokens - \\r\\n (no escape)', testScriptStdout, () => escapedCall('echo.js \r\n '), '');122+test('$ splits tokens - \\r\\n (no escape)', testScriptStdout, () => escapedCall('echo.js a\r\nb'), 'a\nb');123+test('$ splits expressions - \\r\\n (no escape)', testScriptStdout, () => escapedCall(`echo.js ${'a'}\r\n${'b'}`), 'a\nb');124+test('$ concatenates tokens - \\r\\n (no escape)', testScriptStdout, () => escapedCall('echo.js \r\na\r\n b'), 'a\nb');125+test('$ concatenates expressions - \\r\\n (no escape)', testScriptStdout, () => escapedCall(`echo.js \r\n${'a'}\r\n b`), 'a\nb');126+test('$ handles tokens - \\r\\n (escape)', testScriptStdout, () => $`echo.js \r\n `, '\r\n');127+test('$ handles expressions - \\r\\n (escape)', testScriptStdout, () => $`echo.js ${'\r\n'} `, '\r\n');128+test('$ splits tokens - \\r\\n (escape)', testScriptStdout, () => $`echo.js a\r\n b`, 'a\r\n\nb');129+test('$ splits expressions - \\r\\n (escape)', testScriptStdout, () => $`echo.js ${'a'}\r\n ${'b'}`, 'a\r\n\nb');130+test('$ concatenates tokens - \\r\\n (escape)', testScriptStdout, () => $`echo.js \r\na\r\n b`, '\r\na\r\n\nb');131+test('$ concatenates expressions - \\r\\n (escape)', testScriptStdout, () => $`echo.js \r\n${'a'}\r\n b`, '\r\na\r\n\nb');132+/* eslint-disable no-irregular-whitespace */133+test('$ handles expressions - \\f (no escape)', testScriptStdout, () => $`echo.js ${''}`, '\f');134+test('$ splits tokens - \\f (no escape)', testScriptStdout, () => $`echo.js ab`, 'a\fb');135+test('$ splits expressions - \\f (no escape)', testScriptStdout, () => $`echo.js ${'a'}${'b'}`, 'a\fb');136+test('$ concatenates tokens - \\f (no escape)', testScriptStdout, () => $`echo.js a b`, '\fa\f\nb');137+test('$ concatenates expressions - \\f (no escape)', testScriptStdout, () => $`echo.js ${'a'} b`, '\fa\f\nb');138+/* eslint-enable no-irregular-whitespace */139+test('$ handles tokens - \\f (escape)', testScriptStdout, () => $`echo.js \f`, '\f');140+test('$ handles expressions - \\f (escape)', testScriptStdout, () => $`echo.js ${'\f'}`, '\f');141+test('$ splits tokens - \\f (escape)', testScriptStdout, () => $`echo.js a\fb`, 'a\fb');142+test('$ splits expressions - \\f (escape)', testScriptStdout, () => $`echo.js ${'a'}\f${'b'}`, 'a\fb');143+test('$ concatenates tokens - \\f (escape)', testScriptStdout, () => $`echo.js \fa\f b`, '\fa\f\nb');144+test('$ concatenates expressions - \\f (escape)', testScriptStdout, () => $`echo.js \f${'a'}\f b`, '\fa\f\nb');145+test('$ handles tokens - \\', testScriptStdout, () => $`echo.js \\`, '\\');146+test('$ handles expressions - \\', testScriptStdout, () => $`echo.js ${'\\'}`, '\\');147+test('$ splits tokens - \\', testScriptStdout, () => $`echo.js a\\b`, 'a\\b');148+test('$ splits expressions - \\', testScriptStdout, () => $`echo.js ${'a'}\\${'b'}`, 'a\\b');149+test('$ concatenates tokens - \\', testScriptStdout, () => $`echo.js \\a\\ b`, '\\a\\\nb');150+test('$ concatenates expressions - \\', testScriptStdout, () => $`echo.js \\${'a'}\\ b`, '\\a\\\nb');151+test('$ handles tokens - \\\\', testScriptStdout, () => $`echo.js \\\\`, '\\\\');152+test('$ handles expressions - \\\\', testScriptStdout, () => $`echo.js ${'\\\\'}`, '\\\\');153+test('$ splits tokens - \\\\', testScriptStdout, () => $`echo.js a\\\\b`, 'a\\\\b');154+test('$ splits expressions - \\\\', testScriptStdout, () => $`echo.js ${'a'}\\\\${'b'}`, 'a\\\\b');155+test('$ concatenates tokens - \\\\', testScriptStdout, () => $`echo.js \\\\a\\\\ b`, '\\\\a\\\\\nb');156+test('$ concatenates expressions - \\\\', testScriptStdout, () => $`echo.js \\\\${'a'}\\\\ b`, '\\\\a\\\\\nb');157+test('$ handles tokens - `', testScriptStdout, () => $`echo.js \``, '`');158+test('$ handles expressions - `', testScriptStdout, () => $`echo.js ${'`'}`, '`');159+test('$ splits tokens - `', testScriptStdout, () => $`echo.js a\`b`, 'a`b');160+test('$ splits expressions - `', testScriptStdout, () => $`echo.js ${'a'}\`${'b'}`, 'a`b');161+test('$ concatenates tokens - `', testScriptStdout, () => $`echo.js \`a\` b`, '`a`\nb');162+test('$ concatenates expressions - `', testScriptStdout, () => $`echo.js \`${'a'}\` b`, '`a`\nb');163+test('$ handles tokens - \\v', testScriptStdout, () => $`echo.js \v`, '\v');164+test('$ handles expressions - \\v', testScriptStdout, () => $`echo.js ${'\v'}`, '\v');165+test('$ splits tokens - \\v', testScriptStdout, () => $`echo.js a\vb`, 'a\vb');166+test('$ splits expressions - \\v', testScriptStdout, () => $`echo.js ${'a'}\v${'b'}`, 'a\vb');167+test('$ concatenates tokens - \\v', testScriptStdout, () => $`echo.js \va\v b`, '\va\v\nb');168+test('$ concatenates expressions - \\v', testScriptStdout, () => $`echo.js \v${'a'}\v b`, '\va\v\nb');169+test('$ handles tokens - \\u2028', testScriptStdout, () => $`echo.js \u2028`, '\u2028');170+test('$ handles expressions - \\u2028', testScriptStdout, () => $`echo.js ${'\u2028'}`, '\u2028');171+test('$ splits tokens - \\u2028', testScriptStdout, () => $`echo.js a\u2028b`, 'a\u2028b');172+test('$ splits expressions - \\u2028', testScriptStdout, () => $`echo.js ${'a'}\u2028${'b'}`, 'a\u2028b');173+test('$ concatenates tokens - \\u2028', testScriptStdout, () => $`echo.js \u2028a\u2028 b`, '\u2028a\u2028\nb');174+test('$ concatenates expressions - \\u2028', testScriptStdout, () => $`echo.js \u2028${'a'}\u2028 b`, '\u2028a\u2028\nb');175+test('$ handles tokens - \\a', testScriptStdout, () => $`echo.js \a`, 'a');176+test('$ splits tokens - \\a', testScriptStdout, () => $`echo.js a\ab`, 'aab');177+test('$ splits expressions - \\a', testScriptStdout, () => $`echo.js ${'a'}\a${'b'}`, 'aab');178+test('$ concatenates tokens - \\a', testScriptStdout, () => $`echo.js \aa\a b`, 'aaa\nb');179+test('$ concatenates expressions - \\a', testScriptStdout, () => $`echo.js \a${'a'}\a b`, 'aaa\nb');180+test('$ handles tokens - \\cJ', testScriptStdout, () => $`echo.js \cJ`, 'cJ');181+test('$ splits tokens - \\cJ', testScriptStdout, () => $`echo.js a\cJb`, 'acJb');182+test('$ splits expressions - \\cJ', testScriptStdout, () => $`echo.js ${'a'}\cJ${'b'}`, 'acJb');183+test('$ concatenates tokens - \\cJ', testScriptStdout, () => $`echo.js \cJa\cJ b`, 'cJacJ\nb');184+test('$ concatenates expressions - \\cJ', testScriptStdout, () => $`echo.js \cJ${'a'}\cJ b`, 'cJacJ\nb');185+test('$ handles tokens - \\.', testScriptStdout, () => $`echo.js \.`, '.');186+test('$ splits tokens - \\.', testScriptStdout, () => $`echo.js a\.b`, 'a.b');187+test('$ splits expressions - \\.', testScriptStdout, () => $`echo.js ${'a'}\.${'b'}`, 'a.b');188+test('$ concatenates tokens - \\.', testScriptStdout, () => $`echo.js \.a\. b`, '.a.\nb');189+test('$ concatenates expressions - \\.', testScriptStdout, () => $`echo.js \.${'a'}\. b`, '.a.\nb');190+/* eslint-disable unicorn/no-hex-escape */191+test('$ handles tokens - \\x63', testScriptStdout, () => $`echo.js \x63`, 'c');192+test('$ splits tokens - \\x63', testScriptStdout, () => $`echo.js a\x63b`, 'acb');193+test('$ splits expressions - \\x63', testScriptStdout, () => $`echo.js ${'a'}\x63${'b'}`, 'acb');194+test('$ concatenates tokens - \\x63', testScriptStdout, () => $`echo.js \x63a\x63 b`, 'cac\nb');195+test('$ concatenates expressions - \\x63', testScriptStdout, () => $`echo.js \x63${'a'}\x63 b`, 'cac\nb');196+/* eslint-enable unicorn/no-hex-escape */197+test('$ handles tokens - \\u0063', testScriptStdout, () => $`echo.js \u0063`, 'c');198+test('$ splits tokens - \\u0063', testScriptStdout, () => $`echo.js a\u0063b`, 'acb');199+test('$ splits expressions - \\u0063', testScriptStdout, () => $`echo.js ${'a'}\u0063${'b'}`, 'acb');200+test('$ concatenates tokens - \\u0063', testScriptStdout, () => $`echo.js \u0063a\u0063 b`, 'cac\nb');201+test('$ concatenates expressions - \\u0063', testScriptStdout, () => $`echo.js \u0063${'a'}\u0063 b`, 'cac\nb');202+test('$ handles tokens - \\u{1}', testScriptStdout, () => $`echo.js \u{1}`, '\u0001');203+test('$ splits tokens - \\u{1}', testScriptStdout, () => $`echo.js a\u{1}b`, 'a\u0001b');204+test('$ splits expressions - \\u{1}', testScriptStdout, () => $`echo.js ${'a'}\u{1}${'b'}`, 'a\u0001b');205+test('$ concatenates tokens - \\u{1}', testScriptStdout, () => $`echo.js \u{1}a\u{1} b`, '\u0001a\u0001\nb');206+test('$ concatenates expressions - \\u{1}', testScriptStdout, () => $`echo.js \u{1}${'a'}\u{1} b`, '\u0001a\u0001\nb');207+test('$ handles tokens - \\u{63}', testScriptStdout, () => $`echo.js \u{63}`, 'c');208+test('$ splits tokens - \\u{63}', testScriptStdout, () => $`echo.js a\u{63}b`, 'acb');209+test('$ splits expressions - \\u{63}', testScriptStdout, () => $`echo.js ${'a'}\u{63}${'b'}`, 'acb');210+test('$ concatenates tokens - \\u{63}', testScriptStdout, () => $`echo.js \u{63}a\u{63} b`, 'cac\nb');211+test('$ concatenates expressions - \\u{63}', testScriptStdout, () => $`echo.js \u{63}${'a'}\u{63} b`, 'cac\nb');212+test('$ handles tokens - \\u{063}', testScriptStdout, () => $`echo.js \u{063}`, 'c');213+test('$ splits tokens - \\u{063}', testScriptStdout, () => $`echo.js a\u{063}b`, 'acb');214+test('$ splits expressions - \\u{063}', testScriptStdout, () => $`echo.js ${'a'}\u{063}${'b'}`, 'acb');215+test('$ concatenates tokens - \\u{063}', testScriptStdout, () => $`echo.js \u{063}a\u{063} b`, 'cac\nb');216+test('$ concatenates expressions - \\u{063}', testScriptStdout, () => $`echo.js \u{063}${'a'}\u{063} b`, 'cac\nb');217+test('$ handles tokens - \\u{0063}', testScriptStdout, () => $`echo.js \u{0063}`, 'c');218+test('$ splits tokens - \\u{0063}', testScriptStdout, () => $`echo.js a\u{0063}b`, 'acb');219+test('$ splits expressions - \\u{0063}', testScriptStdout, () => $`echo.js ${'a'}\u{0063}${'b'}`, 'acb');220+test('$ concatenates tokens - \\u{0063}', testScriptStdout, () => $`echo.js \u{0063}a\u{0063} b`, 'cac\nb');221+test('$ concatenates expressions - \\u{0063}', testScriptStdout, () => $`echo.js \u{0063}${'a'}\u{0063} b`, 'cac\nb');222+test('$ handles tokens - \\u{00063}', testScriptStdout, () => $`echo.js \u{00063}`, 'c');223+test('$ splits tokens - \\u{00063}', testScriptStdout, () => $`echo.js a\u{00063}b`, 'acb');224+test('$ splits expressions - \\u{00063}', testScriptStdout, () => $`echo.js ${'a'}\u{00063}${'b'}`, 'acb');225+test('$ concatenates tokens - \\u{00063}', testScriptStdout, () => $`echo.js \u{00063}a\u{00063} b`, 'cac\nb');226+test('$ concatenates expressions - \\u{00063}', testScriptStdout, () => $`echo.js \u{00063}${'a'}\u{00063} b`, 'cac\nb');227+test('$ handles tokens - \\u{000063}', testScriptStdout, () => $`echo.js \u{000063}`, 'c');228+test('$ splits tokens - \\u{000063}', testScriptStdout, () => $`echo.js a\u{000063}b`, 'acb');229+test('$ splits expressions - \\u{000063}', testScriptStdout, () => $`echo.js ${'a'}\u{000063}${'b'}`, 'acb');230+test('$ concatenates tokens - \\u{000063}', testScriptStdout, () => $`echo.js \u{000063}a\u{000063} b`, 'cac\nb');231+test('$ concatenates expressions - \\u{000063}', testScriptStdout, () => $`echo.js \u{000063}${'a'}\u{000063} b`, 'cac\nb');232+test('$ handles tokens - \\u{0000063}', testScriptStdout, () => $`echo.js \u{0000063}`, 'c');233+test('$ splits tokens - \\u{0000063}', testScriptStdout, () => $`echo.js a\u{0000063}b`, 'acb');234+test('$ splits expressions - \\u{0000063}', testScriptStdout, () => $`echo.js ${'a'}\u{0000063}${'b'}`, 'acb');235+test('$ concatenates tokens - \\u{0000063}', testScriptStdout, () => $`echo.js \u{0000063}a\u{0000063} b`, 'cac\nb');236+test('$ concatenates expressions - \\u{0000063}', testScriptStdout, () => $`echo.js \u{0000063}${'a'}\u{0000063} b`, 'cac\nb');237+test('$ handles tokens - \\u{0063}}', testScriptStdout, () => $`echo.js \u{0063}}`, 'c}');238+test('$ splits tokens - \\u{0063}}', testScriptStdout, () => $`echo.js a\u{0063}}b`, 'ac}b');239+test('$ splits expressions - \\u{0063}}', testScriptStdout, () => $`echo.js ${'a'}\u{0063}}${'b'}`, 'ac}b');240+test('$ concatenates tokens - \\u{0063}}', testScriptStdout, () => $`echo.js \u{0063}}a\u{0063}} b`, 'c}ac}\nb');241+test('$ concatenates expressions - \\u{0063}}', testScriptStdout, () => $`echo.js \u{0063}}${'a'}\u{0063}} b`, 'c}ac}\nb');242 26-test('$ allows number interpolation', async t => {27- const {stdout} = await $`echo.js 1 ${2}`;28- t.is(stdout, '1\n2');29-});243+const testScriptStdoutSync = (t, getChildProcess, expectedStdout) => {244+ const {stdout} = getChildProcess();245+ t.is(stdout, expectedStdout);246+};247 31-test('$ allows array interpolation', async t => {32- const {stdout} = await $`echo.js ${['foo', 'bar']}`;33- t.is(stdout, 'foo\nbar');34-});248+test('$.sync', testScriptStdoutSync, () => $.sync`echo.js foo bar`, 'foo\nbar');249+test('$.sync can be called $.s', testScriptStdoutSync, () => $.s`echo.js foo bar`, 'foo\nbar');250+test('$.sync accepts options', testScriptStdoutSync, () => $({stripFinalNewline: true}).sync`noop.js foo`, 'foo');251 36-test('$ allows empty array interpolation', async t => {37- const {stdout} = await $`echo.js foo ${[]} bar`;38- t.is(stdout, 'foo\nbar');39-});252+const testReturnInterpolate = async (t, getChildProcess, expectedStdout, options = {}) => {253+ const foo = await $(options)`echo.js foo`;254+ const {stdout} = await getChildProcess(foo);255+ t.is(stdout, expectedStdout);256+};257 41-test('$ allows execa return value interpolation', async t => {42- const foo = await $`echo.js foo`;43- const {stdout} = await $`echo.js ${foo} bar`;44- t.is(stdout, 'foo\nbar');45-});258+test('$ allows execa return value interpolation', testReturnInterpolate, foo => $`echo.js ${foo} bar`, 'foo\nbar');259+test('$ allows execa return value buffer interpolation', testReturnInterpolate, foo => $`echo.js ${foo} bar`, 'foo\nbar', {encoding: 'buffer'});260+test('$ allows execa return value array interpolation', testReturnInterpolate, foo => $`echo.js ${[foo, 'bar']}`, 'foo\nbar');261+test('$ allows execa return value buffer array interpolation', testReturnInterpolate, foo => $`echo.js ${[foo, 'bar']}`, 'foo\nbar', {encoding: 'buffer'});262 47-test('$ allows execa return value array interpolation', async t => {48- const foo = await $`echo.js foo`;49- const {stdout} = await $`echo.js ${[foo, 'bar']}`;50- t.is(stdout, 'foo\nbar');51-});263+const testReturnInterpolateSync = (t, getChildProcess, expectedStdout, options = {}) => {264+ const foo = $(options).sync`echo.js foo`;265+ const {stdout} = getChildProcess(foo);266+ t.is(stdout, expectedStdout);267+};268 53-test('$ allows execa return value buffer interpolation', async t => {54- const foo = await $({encoding: 'buffer'})`echo.js foo`;55- const {stdout} = await $`echo.js ${foo} bar`;56- t.is(stdout, 'foo\nbar');57-});269+test('$.sync allows execa return value interpolation', testReturnInterpolateSync, foo => $.sync`echo.js ${foo} bar`, 'foo\nbar');270+test('$.sync allows execa return value buffer interpolation', testReturnInterpolateSync, foo => $.sync`echo.js ${foo} bar`, 'foo\nbar', {encoding: 'buffer'});271+test('$.sync allows execa return value array interpolation', testReturnInterpolateSync, foo => $.sync`echo.js ${[foo, 'bar']}`, 'foo\nbar');272+test('$.sync allows execa return value buffer array interpolation', testReturnInterpolateSync, foo => $.sync`echo.js ${[foo, 'bar']}`, 'foo\nbar', {encoding: 'buffer'});273 59-test('$ allows execa return value buffer array interpolation', async t => {60- const foo = await $({encoding: 'buffer'})`echo.js foo`;61- const {stdout} = await $`echo.js ${[foo, 'bar']}`;62- t.is(stdout, 'foo\nbar');63-});274+const testInvalidSequence = (t, getChildProcess) => {275+ t.throws(getChildProcess, {message: /Invalid backslash sequence/});276+};277 65-test('$ ignores consecutive spaces', async t => {66- const {stdout} = await $`echo.js foo bar`;67- t.is(stdout, 'foo\nbar');68-});278+test('$ handles invalid escape sequence - \\1', testInvalidSequence, () => $`echo.js \1`);279+test('$ handles invalid escape sequence - \\u', testInvalidSequence, () => $`echo.js \u`);280+test('$ handles invalid escape sequence - \\u0', testInvalidSequence, () => $`echo.js \u0`);281+test('$ handles invalid escape sequence - \\u00', testInvalidSequence, () => $`echo.js \u00`);282+test('$ handles invalid escape sequence - \\u000', testInvalidSequence, () => $`echo.js \u000`);283+test('$ handles invalid escape sequence - \\ug', testInvalidSequence, () => $`echo.js \ug`);284+test('$ handles invalid escape sequence - \\u{', testInvalidSequence, () => $`echo.js \u{`);285+test('$ handles invalid escape sequence - \\u{0000', testInvalidSequence, () => $`echo.js \u{0000`);286+test('$ handles invalid escape sequence - \\u{g}', testInvalidSequence, () => $`echo.js \u{g}`);287+/* eslint-disable unicorn/no-hex-escape */288+test('$ handles invalid escape sequence - \\x', testInvalidSequence, () => $`echo.js \x`);289+test('$ handles invalid escape sequence - \\x0', testInvalidSequence, () => $`echo.js \x0`);290+test('$ handles invalid escape sequence - \\xgg', testInvalidSequence, () => $`echo.js \xgg`);291+/* eslint-enable unicorn/no-hex-escape */292 70-test('$ allows escaping spaces with interpolation', async t => {71- const {stdout} = await $`echo.js ${'foo bar'}`;72- t.is(stdout, 'foo bar');73-});293+const testEmptyScript = (t, getChildProcess) => {294+ t.throws(getChildProcess, {message: /Template script must not be empty/});295+};296 75-test('$ disallows escaping spaces with backslashes', async t => {76- const {stdout} = await $`echo.js foo\\ bar`;77- t.is(stdout, 'foo\\\nbar');78-});297+test('$``', testEmptyScript, () => $``);298+test('$` `', testEmptyScript, () => $` `);299+test('$` ` (2 spaces)', testEmptyScript, () => $` `);300+test('$`\\t`', testEmptyScript, () => $` `);301+test('$`\\n`', testEmptyScript, () => $`302+`);303 80-test('$ allows space escaped values in array interpolation', async t => {81- const {stdout} = await $`echo.js ${['foo', 'bar baz']}`;82- t.is(stdout, 'foo\nbar baz');304+test('$.sync must be used after options binding, not before', t => {305+ t.throws(() => $.sync({})`noop.js`, {message: /Please use/});306 });307 85-test('$ passes newline escape sequence as one argument', async t => {86- const {stdout} = await $`echo.js \n`;87- t.is(stdout, '\n');88-});89-90-test('$ passes newline escape sequence in interpolation as one argument', async t => {91- const {stdout} = await $`echo.js ${'\n'}`;92- t.is(stdout, '\n');93-});94-95-test('$ handles invalid escape sequence', async t => {96- const {stdout} = await $`echo.js \u`;97- t.is(stdout, '\\u');98-});99-100-test('$ can concatenate at the end of tokens', async t => {101- const {stdout} = await $`echo.js foo${'bar'}`;102- t.is(stdout, 'foobar');103-});104-105-test('$ does not concatenate at the end of tokens with a space', async t => {106- const {stdout} = await $`echo.js foo ${'bar'}`;107- t.is(stdout, 'foo\nbar');108-});109-110-test('$ can concatenate at the end of tokens followed by an array', async t => {111- const {stdout} = await $`echo.js foo${['bar', 'foo']}`;112- t.is(stdout, 'foobar\nfoo');113-});114-115-test('$ can concatenate at the start of tokens', async t => {116- const {stdout} = await $`echo.js ${'foo'}bar`;117- t.is(stdout, 'foobar');118-});119-120-test('$ does not concatenate at the start of tokens with a space', async t => {121- const {stdout} = await $`echo.js ${'foo'} bar`;122- t.is(stdout, 'foo\nbar');123-});124-125-test('$ can concatenate at the start of tokens followed by an array', async t => {126- const {stdout} = await $`echo.js ${['foo', 'bar']}foo`;127- t.is(stdout, 'foo\nbarfoo');128-});129-130-test('$ can concatenate at the start and end of tokens followed by an array', async t => {131- const {stdout} = await $`echo.js foo${['bar', 'foo']}bar`;132- t.is(stdout, 'foobar\nfoobar');133-});134-135-test('$ can concatenate multiple tokens', async t => {136- const {stdout} = await $`echo.js ${'foo'}bar${'foo'}`;137- t.is(stdout, 'foobarfoo');138-});139-140-test('$ allows escaping spaces in commands with interpolation', async t => {141- const {stdout} = await $`${'command with space.js'} foo bar`;142- t.is(stdout, 'foo\nbar');143-});144-145-test('$ escapes other whitespaces', async t => {146- const {stdout} = await $`echo.js foo\tbar`;147- t.is(stdout, 'foo\tbar');148-});149-150-test('$ trims', async t => {151- const {stdout} = await $` echo.js foo bar `;152- t.is(stdout, 'foo\nbar');153-});154-308 test('$ must only use options or templates', t => {309 t.throws(() => $(true)`noop.js`, {message: /Please use either/});310 });311 159-test('$.sync', t => {160- const {stdout} = $.sync`echo.js foo bar`;161- t.is(stdout, 'foo\nbar');162-});163-164-test('$.sync can be called $.s', t => {165- const {stdout} = $.s`echo.js foo bar`;166- t.is(stdout, 'foo\nbar');167-});168-169-test('$.sync accepts options', t => {170- const {stdout} = $({stripFinalNewline: true}).sync`noop.js foo`;171- t.is(stdout, 'foo');172-});173-174-test('$.sync must be used after options binding, not before', t => {175- t.throws(() => $.sync({})`noop.js`, {message: /Please use/});176-});177-178-test('$.sync allows execa return value interpolation', t => {179- const foo = $.sync`echo.js foo`;180- const {stdout} = $.sync`echo.js ${foo} bar`;181- t.is(stdout, 'foo\nbar');182-});183-184-test('$.sync allows execa return value array interpolation', t => {185- const foo = $.sync`echo.js foo`;186- const {stdout} = $.sync`echo.js ${[foo, 'bar']}`;187- t.is(stdout, 'foo\nbar');188-});189-190-test('$.sync allows execa return value buffer interpolation', t => {191- const foo = $({encoding: 'buffer'}).sync`echo.js foo`;192- const {stdout} = $.sync`echo.js ${foo} bar`;193- t.is(stdout, 'foo\nbar');194-});195-196-test('$.sync allows execa return value buffer array interpolation', t => {197- const foo = $({encoding: 'buffer'}).sync`echo.js foo`;198- const {stdout} = $.sync`echo.js ${[foo, 'bar']}`;199- t.is(stdout, 'foo\nbar');200-});201-312 test('$.sync must only templates', t => {313 t.throws(() => $.sync(true)`noop.js`, {message: /A template string must be used/});314 });315 206-const invalidExpression = test.macro({207- async exec(t, input, expected) {208- await t.throwsAsync(209- async () => $`echo.js ${input}`,210- {instanceOf: TypeError, message: expected},211- );316+const testInvalidExpression = (t, invalidExpression, execaMethod) => {317+ const expression = typeof invalidExpression === 'function' ? invalidExpression() : invalidExpression;318+ t.throws(319+ () => execaMethod`echo.js ${expression}`,320+ {message: /in template expression/},321+ );322+};323 213- t.throws(214- () => $.sync`echo.js ${input}`,215- {instanceOf: TypeError, message: expected},216- );217- },218- title(prettyInput, input, expected) {219- return `$ APIs throw on invalid '${prettyInput ?? inspect(input)}' expression with '${expected}'`;220- },221-});324+test('$ throws on invalid expression - undefined', testInvalidExpression, undefined, $);325+test('$ throws on invalid expression - null', testInvalidExpression, null, $);326+test('$ throws on invalid expression - true', testInvalidExpression, true, $);327+test('$ throws on invalid expression - {}', testInvalidExpression, {}, $);328+test('$ throws on invalid expression - {foo: "bar"}', testInvalidExpression, {foo: 'bar'}, $);329+test('$ throws on invalid expression - {stdout: undefined}', testInvalidExpression, {stdout: undefined}, $);330+test('$ throws on invalid expression - {stdout: 1}', testInvalidExpression, {stdout: 1}, $);331+test('$ throws on invalid expression - Promise.resolve()', testInvalidExpression, Promise.resolve(), $);332+test('$ throws on invalid expression - Promise.resolve({stdout: "foo"})', testInvalidExpression, Promise.resolve({foo: 'bar'}), $);333+test('$ throws on invalid expression - $', testInvalidExpression, () => $`noop.js`, $);334+test('$ throws on invalid expression - $(options).sync', testInvalidExpression, () => $({stdio: 'ignore'}).sync`noop.js`, $);335+test('$ throws on invalid expression - [undefined]', testInvalidExpression, [undefined], $);336+test('$ throws on invalid expression - [null]', testInvalidExpression, [null], $);337+test('$ throws on invalid expression - [true]', testInvalidExpression, [true], $);338+test('$ throws on invalid expression - [{}]', testInvalidExpression, [{}], $);339+test('$ throws on invalid expression - [{foo: "bar"}]', testInvalidExpression, [{foo: 'bar'}], $);340+test('$ throws on invalid expression - [{stdout: undefined}]', testInvalidExpression, [{stdout: undefined}], $);341+test('$ throws on invalid expression - [{stdout: 1}]', testInvalidExpression, [{stdout: 1}], $);342+test('$ throws on invalid expression - [Promise.resolve()]', testInvalidExpression, [Promise.resolve()], $);343+test('$ throws on invalid expression - [Promise.resolve({stdout: "foo"})]', testInvalidExpression, [Promise.resolve({stdout: 'foo'})], $);344+test('$ throws on invalid expression - [$]', testInvalidExpression, () => [$`noop.js`], $);345+test('$ throws on invalid expression - [$(options).sync]', testInvalidExpression, () => [$({stdio: 'ignore'}).sync`noop.js`], $);346 223-test(invalidExpression, undefined, 'Unexpected "undefined" in template expression');224-test(invalidExpression, [undefined], 'Unexpected "undefined" in template expression');225-226-test(invalidExpression, null, 'Unexpected "object" in template expression');227-test(invalidExpression, [null], 'Unexpected "object" in template expression');228-229-test(invalidExpression, true, 'Unexpected "boolean" in template expression');230-test(invalidExpression, [true], 'Unexpected "boolean" in template expression');231-232-test(invalidExpression, {}, 'Unexpected "object" in template expression');233-test(invalidExpression, [{}], 'Unexpected "object" in template expression');234-235-test(invalidExpression, {foo: 'bar'}, 'Unexpected "object" in template expression');236-test(invalidExpression, [{foo: 'bar'}], 'Unexpected "object" in template expression');237-238-test(invalidExpression, {stdout: undefined}, 'Unexpected "undefined" stdout in template expression');239-test(invalidExpression, [{stdout: undefined}], 'Unexpected "undefined" stdout in template expression');240-241-test(invalidExpression, {stdout: 1}, 'Unexpected "number" stdout in template expression');242-test(invalidExpression, [{stdout: 1}], 'Unexpected "number" stdout in template expression');243-244-test(invalidExpression, Promise.resolve(), 'Unexpected "object" in template expression');245-test(invalidExpression, [Promise.resolve()], 'Unexpected "object" in template expression');246-247-test(invalidExpression, Promise.resolve({stdout: 'foo'}), 'Unexpected "object" in template expression');248-test(invalidExpression, [Promise.resolve({stdout: 'foo'})], 'Unexpected "object" in template expression');249-250-test('$`noop.js`', invalidExpression, $`noop.js`, 'Unexpected "object" in template expression');251-test('[ $`noop.js` ]', invalidExpression, [$`noop.js`], 'Unexpected "object" in template expression');252-253-test('$({stdio: \'ignore\'}).sync`noop.js`', invalidExpression, $({stdio: 'ignore'}).sync`noop.js`, 'Unexpected "undefined" stdout in template expression');254-test('[ $({stdio: \'ignore\'}).sync`noop.js` ]', invalidExpression, [$({stdio: 'ignore'}).sync`noop.js`], 'Unexpected "undefined" stdout in template expression');255-347 test('$ stdin defaults to "inherit"', async t => {348 const {stdout} = await $({input: 'foo'})`stdin-script.js`;349 t.is(stdout, 'foo');350 });108 unmodified linesmodified:readme.md+2 added
254 unmodified lines255 This is the preferred method when executing multiple commands in a script file.256 257 The `command` string can inject any `${value}` with the following types: string, number, [`childProcess`](#childprocess) or an array of those types. For example: `` $`echo one ${'two'} ${3} ${['four', 'five']}` ``. For `${childProcess}`, the process's `stdout` is used.258 259+The `command` string can use [multiple lines and indentation](docs/scripts.md#multiline-commands).260+261 For more information, please see [this section](#scripts-interface) and [this page](docs/scripts.md).262 263 #### $(options)264 749 unmodified lines