Type the `std*` generator transform `chunk` argument (#1247)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sindre Sorhus · committed by GitHub · parent 487a3a4 · Browse files · 12 files·+469 added−98 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
12 files+469 added−98 removed
- other:+469 added−98 removed
Provenance
- Commit
0a7b4f8- Tree
199cea9- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
487a3a4
12 files changed+469 added−98 removed
modified:docs/api.md+1 added-1 removed
1286 unmodified lines1287 1288 _Type:_ `boolean`\1289 _Default:_ `false`1290 1291-If `true`, keep newlines in each `line` argument. Also, this allows multiple `yield`s to produces a single line.1291+If `true`, keep newlines in each `line` argument. Also, this allows multiple `yield`s to produce a single line.1292 1293 [More info.](lines.md#transforms)1294 1295 ### transformOptions.objectMode12 unmodified linesmodified:test-d/methods/script.test-d.ts+7 added
39 unmodified lines40414243444546474849505152535410 unmodified lines39 unmodified linesexpectAssignable<{stdout: Uint8Array}>(await $({encoding: 'buffer'})`unicorns`);expectAssignable<{stdout: Uint8Array}>(await $({})({encoding: 'buffer'})`unicorns`);expectAssignable<{stdout: Uint8Array}>(await $({encoding: 'buffer'})({})`unicorns`);
const uint8ArrayGenerator = function * (chunk: Uint8Array) { yield chunk;};
expectError($({encoding: 'utf8', stdout: uint8ArrayGenerator}));$({encoding: 'buffer', stdout: uint8ArrayGenerator});
expectType<Result<{}>>(await $`${'unicorns'}`);expectType<Result<{}>>(await $`unicorns ${'foo'}`);expectType<Result<{}>>(await $`unicorns ${'foo'} ${'bar'}`);expectType<Result<{}>>(await $`unicorns ${1}`);10 unmodified linesadded:test-d/stdio/option/generator-binary-uint-array.test-d.ts+202 added
1+import {expectError, expectAssignable, expectNotAssignable} from 'tsd';2+import {3+ execa,4+ execaSync,5+ type StdinOption,6+ type StdinSyncOption,7+ type StdoutStderrOption,8+ type StdoutStderrSyncOption,9+} from '../../../index.js';10+11+// With `binary: true`, the `chunk` argument is an `Uint8Array`.12+const uint8ArrayBinary = {13+ * transform(chunk: Uint8Array) {14+ yield chunk;15+ },16+ binary: true,17+} as const;18+19+await execa('unicorns', {stdin: uint8ArrayBinary});20+execaSync('unicorns', {stdin: uint8ArrayBinary});21+await execa('unicorns', {stdin: [uint8ArrayBinary]});22+execaSync('unicorns', {stdin: [uint8ArrayBinary]});23+await execa('unicorns', {stdout: uint8ArrayBinary});24+execaSync('unicorns', {stdout: uint8ArrayBinary});25+await execa('unicorns', {stderr: uint8ArrayBinary});26+execaSync('unicorns', {stderr: uint8ArrayBinary});27+await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', uint8ArrayBinary]});28+29+expectAssignable<StdinOption>(uint8ArrayBinary);30+expectAssignable<StdinSyncOption>(uint8ArrayBinary);31+expectAssignable<StdinOption>([uint8ArrayBinary]);32+expectAssignable<StdinSyncOption>([uint8ArrayBinary]);33+expectAssignable<StdoutStderrOption>(uint8ArrayBinary);34+expectAssignable<StdoutStderrSyncOption>(uint8ArrayBinary);35+expectAssignable<StdoutStderrOption>([uint8ArrayBinary]);36+expectAssignable<StdoutStderrSyncOption>([uint8ArrayBinary]);37+38+// `objectMode: true` takes precedence over `binary: true` for stdin, so the `chunk` argument is `unknown`.39+const uint8ArrayBinaryObjectMode = {40+ * transform(chunk: Uint8Array) {41+ yield chunk;42+ },43+ binary: true,44+ objectMode: true,45+} as const;46+47+const stdinObjectModeBinary = {48+ * transform(chunk: unknown) {49+ yield chunk;50+ },51+ binary: true,52+ objectMode: true,53+} as const;54+55+expectError(await execa('unicorns', {stdin: uint8ArrayBinaryObjectMode}));56+expectError(execaSync('unicorns', {stdin: uint8ArrayBinaryObjectMode}));57+await execa('unicorns', {stdout: uint8ArrayBinaryObjectMode});58+execaSync('unicorns', {stdout: uint8ArrayBinaryObjectMode});59+await execa('unicorns', {stderr: uint8ArrayBinaryObjectMode});60+execaSync('unicorns', {stderr: uint8ArrayBinaryObjectMode});61+62+await execa('unicorns', {stdin: stdinObjectModeBinary});63+execaSync('unicorns', {stdin: stdinObjectModeBinary});64+65+expectNotAssignable<StdinOption>(uint8ArrayBinaryObjectMode);66+expectNotAssignable<StdinSyncOption>(uint8ArrayBinaryObjectMode);67+expectAssignable<StdoutStderrOption>(uint8ArrayBinaryObjectMode);68+expectAssignable<StdoutStderrSyncOption>(uint8ArrayBinaryObjectMode);69+expectAssignable<StdinOption>(stdinObjectModeBinary);70+expectAssignable<StdinSyncOption>(stdinObjectModeBinary);71+72+const stdoutAnnotatedBinaryObjectMode: StdoutStderrOption = {73+ * transform(chunk: Uint8Array) {74+ yield chunk;75+ },76+ binary: true,77+ objectMode: true,78+};79+80+expectAssignable<StdoutStderrOption>(stdoutAnnotatedBinaryObjectMode);81+82+// A `string` argument is invalid when `binary: true`.83+const stringBinary = {84+ * transform(line: string) {85+ yield line;86+ },87+ binary: true,88+} as const;89+90+const stringBinaryObjectMode = {91+ * transform(line: string) {92+ yield line;93+ },94+ binary: true,95+ objectMode: true,96+} as const;97+98+expectError(await execa('unicorns', {stdin: stringBinary}));99+expectError(execaSync('unicorns', {stdin: stringBinary}));100+expectError(await execa('unicorns', {stdin: [stringBinary]}));101+expectError(execaSync('unicorns', {stdin: [stringBinary]}));102+expectError(await execa('unicorns', {stdout: stringBinary}));103+expectError(execaSync('unicorns', {stdout: stringBinary}));104+expectError(await execa('unicorns', {stderr: stringBinary}));105+expectError(execaSync('unicorns', {stderr: stringBinary}));106+expectError(await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', stringBinary]}));107+expectError(await execa('unicorns', {stdout: stringBinaryObjectMode}));108+expectError(execaSync('unicorns', {stdout: stringBinaryObjectMode}));109+expectError(await execa('unicorns', {stderr: stringBinaryObjectMode}));110+expectError(execaSync('unicorns', {stderr: stringBinaryObjectMode}));111+112+expectNotAssignable<StdinOption>(stringBinary);113+expectNotAssignable<StdinSyncOption>(stringBinary);114+expectNotAssignable<StdinOption>([stringBinary]);115+expectNotAssignable<StdinSyncOption>([stringBinary]);116+expectNotAssignable<StdoutStderrOption>(stringBinary);117+expectNotAssignable<StdoutStderrSyncOption>(stringBinary);118+expectNotAssignable<StdoutStderrOption>([stringBinary]);119+expectNotAssignable<StdoutStderrSyncOption>([stringBinary]);120+expectNotAssignable<StdoutStderrOption>(stringBinaryObjectMode);121+expectNotAssignable<StdoutStderrSyncOption>(stringBinaryObjectMode);122+123+// An `Uint8Array` argument is invalid without `binary: true`, since the `chunk` is then a `string`.124+const uint8ArrayLine = {125+ * transform(chunk: Uint8Array) {126+ yield chunk;127+ },128+} as const;129+130+expectError(await execa('unicorns', {stdin: uint8ArrayLine}));131+expectError(execaSync('unicorns', {stdin: uint8ArrayLine}));132+expectError(await execa('unicorns', {stdin: [uint8ArrayLine]}));133+expectError(execaSync('unicorns', {stdin: [uint8ArrayLine]}));134+expectError(await execa('unicorns', {stdout: uint8ArrayLine}));135+expectError(execaSync('unicorns', {stdout: uint8ArrayLine}));136+expectError(await execa('unicorns', {stderr: uint8ArrayLine}));137+expectError(execaSync('unicorns', {stderr: uint8ArrayLine}));138+expectError(await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', uint8ArrayLine]}));139+140+expectNotAssignable<StdinOption>(uint8ArrayLine);141+expectNotAssignable<StdinSyncOption>(uint8ArrayLine);142+expectNotAssignable<StdinOption>([uint8ArrayLine]);143+expectNotAssignable<StdinSyncOption>([uint8ArrayLine]);144+expectNotAssignable<StdoutStderrOption>(uint8ArrayLine);145+expectNotAssignable<StdoutStderrSyncOption>(uint8ArrayLine);146+expectNotAssignable<StdoutStderrOption>([uint8ArrayLine]);147+expectNotAssignable<StdoutStderrSyncOption>([uint8ArrayLine]);148+149+// Binary encodings also make output transforms receive binary chunks.150+await execa('unicorns', {encoding: 'buffer', stdin: uint8ArrayLine});151+execaSync('unicorns', {encoding: 'buffer', stdin: uint8ArrayLine});152+await execa('unicorns', {encoding: 'buffer', stdout: uint8ArrayLine});153+execaSync('unicorns', {encoding: 'buffer', stdout: uint8ArrayLine});154+await execa('unicorns', {encoding: 'buffer', stderr: uint8ArrayLine});155+execaSync('unicorns', {encoding: 'buffer', stderr: uint8ArrayLine});156+await execa('unicorns', {encoding: 'buffer', stdio: [uint8ArrayLine, uint8ArrayLine, uint8ArrayLine, uint8ArrayLine]});157+execaSync('unicorns', {encoding: 'buffer', stdio: [uint8ArrayLine, uint8ArrayLine, uint8ArrayLine, uint8ArrayLine]});158+159+// A bare generator function always receives string lines unless `encoding` is binary.160+const uint8ArrayGenerator = function * (chunk: Uint8Array) {161+ yield chunk;162+};163+164+expectError(await execa('unicorns', {stdin: uint8ArrayGenerator}));165+expectError(execaSync('unicorns', {stdin: uint8ArrayGenerator}));166+expectError(await execa('unicorns', {stdout: uint8ArrayGenerator}));167+expectError(execaSync('unicorns', {stdout: uint8ArrayGenerator}));168+expectError(await execa('unicorns', {stderr: uint8ArrayGenerator}));169+expectError(execaSync('unicorns', {stderr: uint8ArrayGenerator}));170+expectError(await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', uint8ArrayGenerator]}));171+172+expectNotAssignable<StdinOption>(uint8ArrayGenerator);173+expectNotAssignable<StdinSyncOption>(uint8ArrayGenerator);174+expectNotAssignable<StdoutStderrOption>(uint8ArrayGenerator);175+expectNotAssignable<StdoutStderrSyncOption>(uint8ArrayGenerator);176+177+await execa('unicorns', {encoding: 'buffer', stdin: uint8ArrayGenerator});178+execaSync('unicorns', {encoding: 'buffer', stdin: uint8ArrayGenerator});179+await execa('unicorns', {encoding: 'buffer', stdout: uint8ArrayGenerator});180+execaSync('unicorns', {encoding: 'buffer', stdout: uint8ArrayGenerator});181+await execa('unicorns', {encoding: 'buffer', stderr: uint8ArrayGenerator});182+execaSync('unicorns', {encoding: 'buffer', stderr: uint8ArrayGenerator});183+await execa('unicorns', {encoding: 'buffer', stdio: [uint8ArrayGenerator, uint8ArrayGenerator, uint8ArrayGenerator, uint8ArrayGenerator]});184+execaSync('unicorns', {encoding: 'buffer', stdio: [uint8ArrayGenerator, uint8ArrayGenerator, uint8ArrayGenerator, uint8ArrayGenerator]});185+186+// Conversely, a binary `encoding` makes the `chunk` a `Uint8Array`, so a `string` transform is rejected.187+const stringGenerator = function * (line: string) {188+ yield line;189+};190+191+expectError(await execa('unicorns', {encoding: 'buffer', stdin: stringGenerator}));192+expectError(execaSync('unicorns', {encoding: 'buffer', stdin: stringGenerator}));193+expectError(await execa('unicorns', {encoding: 'buffer', stdout: stringGenerator}));194+expectError(execaSync('unicorns', {encoding: 'buffer', stdout: stringGenerator}));195+196+// This applies to every binary `encoding`, not just `'buffer'`.197+await execa('unicorns', {encoding: 'hex', stdout: uint8ArrayGenerator});198+execaSync('unicorns', {encoding: 'hex', stdout: uint8ArrayGenerator});199+await execa('unicorns', {encoding: 'base64', stdout: uint8ArrayGenerator});200+execaSync('unicorns', {encoding: 'base64', stdout: uint8ArrayGenerator});201+expectError(await execa('unicorns', {encoding: 'hex', stdout: stringGenerator}));202+expectError(execaSync('unicorns', {encoding: 'hex', stdout: stringGenerator}));modified:test-d/stdio/option/generator-object-full.test-d.ts+50 added-1 removed
1123458 unmodified lines141516171819202122232425262728293031323334353637382 unmodified lines414243444546474849505152535455565758596061626364656667686970717273747576774 unmodified lines8283848586878889909192939495969798import {expectError, expectAssignable} from 'tsd';import {expectError, expectAssignable, expectNotAssignable} from 'tsd';import { execa, execaSync, type StdinOption,8 unmodified lines }, objectMode: true,} as const;
const stringInputObjectGeneratorFull = { * transform(line: string) { yield JSON.parse(line) as object; }, objectMode: true,} as const;
await execa('unicorns', {stdin: objectGeneratorFull});execaSync('unicorns', {stdin: objectGeneratorFull});await execa('unicorns', {stdin: [objectGeneratorFull]});execaSync('unicorns', {stdin: [objectGeneratorFull]});
expectError(await execa('unicorns', {stdin: stringInputObjectGeneratorFull}));expectError(execaSync('unicorns', {stdin: stringInputObjectGeneratorFull}));expectError(await execa('unicorns', {stdin: [stringInputObjectGeneratorFull]}));expectError(execaSync('unicorns', {stdin: [stringInputObjectGeneratorFull]}));
await execa('unicorns', {stdout: objectGeneratorFull});execaSync('unicorns', {stdout: objectGeneratorFull});await execa('unicorns', {stdout: [objectGeneratorFull]});execaSync('unicorns', {stdout: [objectGeneratorFull]});2 unmodified linesexecaSync('unicorns', {stderr: objectGeneratorFull});await execa('unicorns', {stderr: [objectGeneratorFull]});execaSync('unicorns', {stderr: [objectGeneratorFull]});
await execa('unicorns', {stdout: stringInputObjectGeneratorFull});execaSync('unicorns', {stdout: stringInputObjectGeneratorFull});await execa('unicorns', {stdout: [stringInputObjectGeneratorFull]});execaSync('unicorns', {stdout: [stringInputObjectGeneratorFull]});
// The array index does not determine the pipeline position, so a `string`-input transform is valid at any position.await execa('unicorns', {stdout: [stringInputObjectGeneratorFull, objectGeneratorFull]});execaSync('unicorns', {stdout: [stringInputObjectGeneratorFull, objectGeneratorFull]});await execa('unicorns', {stdout: [objectGeneratorFull, stringInputObjectGeneratorFull]});execaSync('unicorns', {stdout: [objectGeneratorFull, stringInputObjectGeneratorFull]});
// A transform preceded by a non-transform target still receives subprocess `string` lines.await execa('unicorns', {stdout: ['pipe', stringInputObjectGeneratorFull]});execaSync('unicorns', {stdout: ['pipe', stringInputObjectGeneratorFull]});
// A non-tuple array variable stays assignable.const stdoutArray = [stringInputObjectGeneratorFull];await execa('unicorns', {stdout: stdoutArray});execaSync('unicorns', {stdout: stdoutArray});
await execa('unicorns', {stderr: stringInputObjectGeneratorFull});execaSync('unicorns', {stderr: stringInputObjectGeneratorFull});await execa('unicorns', {stderr: [stringInputObjectGeneratorFull]});execaSync('unicorns', {stderr: [stringInputObjectGeneratorFull]});await execa('unicorns', {stderr: [stringInputObjectGeneratorFull, objectGeneratorFull]});execaSync('unicorns', {stderr: [stringInputObjectGeneratorFull, objectGeneratorFull]});await execa('unicorns', {stderr: [objectGeneratorFull, stringInputObjectGeneratorFull]});execaSync('unicorns', {stderr: [objectGeneratorFull, stringInputObjectGeneratorFull]});
expectError(await execa('unicorns', {stdio: objectGeneratorFull}));expectError(execaSync('unicorns', {stdio: objectGeneratorFull}));
await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', objectGeneratorFull]});4 unmodified linesexpectAssignable<StdinOption>(objectGeneratorFull);expectAssignable<StdinSyncOption>(objectGeneratorFull);expectAssignable<StdinOption>([objectGeneratorFull]);expectAssignable<StdinSyncOption>([objectGeneratorFull]);expectNotAssignable<StdinOption>(stringInputObjectGeneratorFull);expectNotAssignable<StdinSyncOption>(stringInputObjectGeneratorFull);expectNotAssignable<StdinOption>([stringInputObjectGeneratorFull]);expectNotAssignable<StdinSyncOption>([stringInputObjectGeneratorFull]);
expectAssignable<StdoutStderrOption>(objectGeneratorFull);expectAssignable<StdoutStderrSyncOption>(objectGeneratorFull);expectAssignable<StdoutStderrOption>([objectGeneratorFull]);expectAssignable<StdoutStderrSyncOption>([objectGeneratorFull]);expectAssignable<StdoutStderrOption>(stringInputObjectGeneratorFull);expectAssignable<StdoutStderrSyncOption>(stringInputObjectGeneratorFull);expectAssignable<StdoutStderrOption>([stringInputObjectGeneratorFull]);expectAssignable<StdoutStderrSyncOption>([stringInputObjectGeneratorFull]);modified:test-d/stdio/option/generator-string-full.test-d.ts+25 added-25 removed
1123457 unmodified lines131415161718192017181920212223242522232425262728293027282930313233343536373835363738394041424340414243444546474845464748import {expectError, expectNotAssignable} from 'tsd';import {expectError, expectAssignable} from 'tsd';import { execa, execaSync, type StdinOption,7 unmodified lines yield line; },} as const;
expectError(await execa('unicorns', {stdin: stringGeneratorFull}));expectError(execaSync('unicorns', {stdin: stringGeneratorFull}));expectError(await execa('unicorns', {stdin: [stringGeneratorFull]}));expectError(execaSync('unicorns', {stdin: [stringGeneratorFull]}));await execa('unicorns', {stdin: stringGeneratorFull});execaSync('unicorns', {stdin: stringGeneratorFull});await execa('unicorns', {stdin: [stringGeneratorFull]});execaSync('unicorns', {stdin: [stringGeneratorFull]});
expectError(await execa('unicorns', {stdout: stringGeneratorFull}));expectError(execaSync('unicorns', {stdout: stringGeneratorFull}));expectError(await execa('unicorns', {stdout: [stringGeneratorFull]}));expectError(execaSync('unicorns', {stdout: [stringGeneratorFull]}));await execa('unicorns', {stdout: stringGeneratorFull});execaSync('unicorns', {stdout: stringGeneratorFull});await execa('unicorns', {stdout: [stringGeneratorFull]});execaSync('unicorns', {stdout: [stringGeneratorFull]});
expectError(await execa('unicorns', {stderr: stringGeneratorFull}));expectError(execaSync('unicorns', {stderr: stringGeneratorFull}));expectError(await execa('unicorns', {stderr: [stringGeneratorFull]}));expectError(execaSync('unicorns', {stderr: [stringGeneratorFull]}));await execa('unicorns', {stderr: stringGeneratorFull});execaSync('unicorns', {stderr: stringGeneratorFull});await execa('unicorns', {stderr: [stringGeneratorFull]});execaSync('unicorns', {stderr: [stringGeneratorFull]});
expectError(await execa('unicorns', {stdio: stringGeneratorFull}));expectError(execaSync('unicorns', {stdio: stringGeneratorFull}));
expectError(await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', stringGeneratorFull]}));expectError(execaSync('unicorns', {stdio: ['pipe', 'pipe', 'pipe', stringGeneratorFull]}));expectError(await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', [stringGeneratorFull]]}));expectError(execaSync('unicorns', {stdio: ['pipe', 'pipe', 'pipe', [stringGeneratorFull]]}));await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', stringGeneratorFull]});execaSync('unicorns', {stdio: ['pipe', 'pipe', 'pipe', stringGeneratorFull]});await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', [stringGeneratorFull]]});execaSync('unicorns', {stdio: ['pipe', 'pipe', 'pipe', [stringGeneratorFull]]});
expectNotAssignable<StdinOption>(stringGeneratorFull);expectNotAssignable<StdinSyncOption>(stringGeneratorFull);expectNotAssignable<StdinOption>([stringGeneratorFull]);expectNotAssignable<StdinSyncOption>([stringGeneratorFull]);expectAssignable<StdinOption>(stringGeneratorFull);expectAssignable<StdinSyncOption>(stringGeneratorFull);expectAssignable<StdinOption>([stringGeneratorFull]);expectAssignable<StdinSyncOption>([stringGeneratorFull]);
expectNotAssignable<StdoutStderrOption>(stringGeneratorFull);expectNotAssignable<StdoutStderrSyncOption>(stringGeneratorFull);expectNotAssignable<StdoutStderrOption>([stringGeneratorFull]);expectNotAssignable<StdoutStderrSyncOption>([stringGeneratorFull]);expectAssignable<StdoutStderrOption>(stringGeneratorFull);expectAssignable<StdoutStderrSyncOption>(stringGeneratorFull);expectAssignable<StdoutStderrOption>([stringGeneratorFull]);expectAssignable<StdoutStderrSyncOption>([stringGeneratorFull]);modified:test-d/stdio/option/generator-string.test-d.ts+36 added-25 removed
1-import {expectError, expectNotAssignable} from 'tsd';1+import {expectError, expectAssignable} from 'tsd';2 import {3 execa,4 execaSync,5 type StdinOption,5 unmodified lines11 const stringGenerator = function * (line: string) {12 yield line;13 };14 15-expectError(await execa('unicorns', {stdin: stringGenerator}));16-expectError(execaSync('unicorns', {stdin: stringGenerator}));17-expectError(await execa('unicorns', {stdin: [stringGenerator]}));18-expectError(execaSync('unicorns', {stdin: [stringGenerator]}));15+await execa('unicorns', {stdin: stringGenerator});16+execaSync('unicorns', {stdin: stringGenerator});17+await execa('unicorns', {stdin: [stringGenerator]});18+execaSync('unicorns', {stdin: [stringGenerator]});19 20-expectError(await execa('unicorns', {stdout: stringGenerator}));21-expectError(execaSync('unicorns', {stdout: stringGenerator}));22-expectError(await execa('unicorns', {stdout: [stringGenerator]}));23-expectError(execaSync('unicorns', {stdout: [stringGenerator]}));20+await execa('unicorns', {stdout: stringGenerator});21+execaSync('unicorns', {stdout: stringGenerator});22+await execa('unicorns', {stdout: [stringGenerator]});23+execaSync('unicorns', {stdout: [stringGenerator]});24 25-expectError(await execa('unicorns', {stderr: stringGenerator}));26-expectError(execaSync('unicorns', {stderr: stringGenerator}));27-expectError(await execa('unicorns', {stderr: [stringGenerator]}));28-expectError(execaSync('unicorns', {stderr: [stringGenerator]}));25+await execa('unicorns', {stderr: stringGenerator});26+execaSync('unicorns', {stderr: stringGenerator});27+await execa('unicorns', {stderr: [stringGenerator]});28+execaSync('unicorns', {stderr: [stringGenerator]});29 30 expectError(await execa('unicorns', {stdio: stringGenerator}));31 expectError(execaSync('unicorns', {stdio: stringGenerator}));32 33-expectError(await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', stringGenerator]}));34-expectError(execaSync('unicorns', {stdio: ['pipe', 'pipe', 'pipe', stringGenerator]}));35-expectError(await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', [stringGenerator]]}));36-expectError(execaSync('unicorns', {stdio: ['pipe', 'pipe', 'pipe', [stringGenerator]]}));33+await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', stringGenerator]});34+execaSync('unicorns', {stdio: ['pipe', 'pipe', 'pipe', stringGenerator]});35+await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', [stringGenerator]]});36+execaSync('unicorns', {stdio: ['pipe', 'pipe', 'pipe', [stringGenerator]]});37 38-expectNotAssignable<StdinOption>(stringGenerator);39-expectNotAssignable<StdinSyncOption>(stringGenerator);40-expectNotAssignable<StdinOption>([stringGenerator]);41-expectNotAssignable<StdinSyncOption>([stringGenerator]);38+expectAssignable<StdinOption>(stringGenerator);39+expectAssignable<StdinSyncOption>(stringGenerator);40+expectAssignable<StdinOption>([stringGenerator]);41+expectAssignable<StdinSyncOption>([stringGenerator]);42 43-expectNotAssignable<StdoutStderrOption>(stringGenerator);44-expectNotAssignable<StdoutStderrSyncOption>(stringGenerator);45-expectNotAssignable<StdoutStderrOption>([stringGenerator]);46-expectNotAssignable<StdoutStderrSyncOption>([stringGenerator]);43+expectAssignable<StdoutStderrOption>(stringGenerator);44+expectAssignable<StdoutStderrSyncOption>(stringGenerator);45+expectAssignable<StdoutStderrOption>([stringGenerator]);46+expectAssignable<StdoutStderrSyncOption>([stringGenerator]);47+48+// The `chunk` argument is typed, but the return type stays `unknown`: a transform can yield both `string` and `Uint8Array`.49+const mixedYieldGenerator = function * (line: string) {50+ yield line;51+ yield new TextEncoder().encode(line);52+};53+54+expectAssignable<StdinOption>(mixedYieldGenerator);55+expectAssignable<StdinSyncOption>(mixedYieldGenerator);56+expectAssignable<StdoutStderrOption>(mixedYieldGenerator);57+expectAssignable<StdoutStderrSyncOption>(mixedYieldGenerator);added:test-d/stdio/option/generator-widened.test-d.ts+45 added
1+import {expectAssignable, expectNotAssignable} from 'tsd';2+import {3+ type StdinOption,4+ type StdinSyncOption,5+ type StdoutStderrOption,6+ type StdoutStderrSyncOption,7+} from '../../../index.js';8+9+// Without `as const`, `binary`/`objectMode` widen to `boolean`. Since the transform mode is unknown at compile time, the `chunk` argument must be `unknown`.10+11+const widenedBinary = {12+ * transform(line: unknown) {13+ yield line;14+ },15+ binary: true,16+};17+18+expectAssignable<StdinOption>(widenedBinary);19+expectAssignable<StdinSyncOption>(widenedBinary);20+expectAssignable<StdoutStderrOption>(widenedBinary);21+expectAssignable<StdoutStderrSyncOption>(widenedBinary);22+23+const widenedObjectMode = {24+ * transform(line: unknown) {25+ yield line;26+ },27+ objectMode: true,28+};29+30+expectAssignable<StdinOption>(widenedObjectMode);31+expectAssignable<StdinSyncOption>(widenedObjectMode);32+expectAssignable<StdoutStderrOption>(widenedObjectMode);33+expectAssignable<StdoutStderrSyncOption>(widenedObjectMode);34+35+const widenedBinaryString = {36+ * transform(line: string) {37+ yield line;38+ },39+ binary: true,40+};41+42+expectNotAssignable<StdinOption>(widenedBinaryString);43+expectNotAssignable<StdinSyncOption>(widenedBinaryString);44+expectNotAssignable<StdoutStderrOption>(widenedBinaryString);45+expectNotAssignable<StdoutStderrSyncOption>(widenedBinaryString);modified:types/arguments/encoding-option.d.ts+1 added-1 removed
1 type DefaultEncodingOption = 'utf8';2-type TextEncodingOption =2+export type TextEncodingOption =3 | DefaultEncodingOption4 | 'utf16le';5 6 export type BufferEncodingOption = 'buffer';15 unmodified linesmodified:types/arguments/options.d.ts+19 added-9 removed
3 unmodified lines4 import type {Message} from '../ipc.js';5 import type {StdinOptionCommon, StdoutStderrOptionCommon, StdioOptionsProperty} from '../stdio/type.js';6 import type {VerboseOption} from '../verbose.js';7 import type {FdGenericOption} from './specific.js';8-import type {EncodingOption} from './encoding-option.js';8+import type {BinaryEncodingOption, EncodingOption, TextEncodingOption} from './encoding-option.js';9 10-export type CommonOptions<IsSync extends boolean = boolean> = {10+type ChunkForEncoding<Encoding extends EncodingOption> = Encoding extends BinaryEncodingOption ? Uint8Array : string;11+12+export type CommonOptions<13+ IsSync extends boolean = boolean,14+ Encoding extends EncodingOption = EncodingOption,15+> = {16 /**17 Prefer locally installed binaries when looking for a binary to execute.18 19 @default `true` with `$`, `false` otherwise91 unmodified lines111 This can be an array of values such as `['inherit', 'pipe']` or `[fileUrl, 'pipe']`.112 113 @default `'inherit'` with `$`, `'pipe'` otherwise114 */110- readonly stdin?: StdinOptionCommon<IsSync>;115+ readonly stdin?: StdinOptionCommon<IsSync, boolean, ChunkForEncoding<Encoding>>;116 117 /**118 How to setup the subprocess' [standard output](https://en.wikipedia.org/wiki/Standard_streams#Standard_input_(stdin)). This can be `'pipe'`, `'overlapped'`, `'ignore`, `'inherit'`, a file descriptor integer, a Node.js `Writable` stream, a web `WritableStream`, a `{ file: 'path' }` object, a file URL, a generator function, a `Duplex` or a web `TransformStream`.119 120 This can be an array of values such as `['inherit', 'pipe']` or `[fileUrl, 'pipe']`.121 122 @default 'pipe'123 */119- readonly stdout?: StdoutStderrOptionCommon<IsSync>;124+ readonly stdout?: StdoutStderrOptionCommon<IsSync, boolean, ChunkForEncoding<Encoding>>;125 126 /**127 How to setup the subprocess' [standard error](https://en.wikipedia.org/wiki/Standard_streams#Standard_input_(stdin)). This can be `'pipe'`, `'overlapped'`, `'ignore`, `'inherit'`, a file descriptor integer, a Node.js `Writable` stream, a web `WritableStream`, a `{ file: 'path' }` object, a file URL, a generator function, a `Duplex` or a web `TransformStream`.128 129 This can be an array of values such as `['inherit', 'pipe']` or `[fileUrl, 'pipe']`.130 131 @default 'pipe'132 */128- readonly stderr?: StdoutStderrOptionCommon<IsSync>;133+ readonly stderr?: StdoutStderrOptionCommon<IsSync, boolean, ChunkForEncoding<Encoding>>;134 135 /**136 Like the `stdin`, `stdout` and `stderr` options but for all [file descriptors](https://en.wikipedia.org/wiki/File_descriptor) at once. For example, `{stdio: ['ignore', 'pipe', 'pipe']}` is the same as `{stdin: 'ignore', stdout: 'pipe', stderr: 'pipe'}`.137 2 unmodified lines140 The array can have more than 3 items, to create additional file descriptors beyond `stdin`/`stdout`/`stderr`.141 142 @default 'pipe'143 */139- readonly stdio?: StdioOptionsProperty<IsSync>;144+ readonly stdio?: StdioOptionsProperty<IsSync, ChunkForEncoding<Encoding>>;145 146 /**147 Add a `subprocess.all` stream and a `result.all` property. They contain the combined/interleaved output of the subprocess' `stdout` and `stderr`.148 11 unmodified lines160 The output is available with `result.stdout`, `result.stderr` and `result.stdio`.161 162 @default 'utf8'163 */159- readonly encoding?: EncodingOption;164+ readonly encoding?: Encoding;165 166 /**167 Set `result.stdout`, `result.stderr`, `result.all` and `result.stdio` as arrays of strings, splitting the subprocess' output into lines.168 210 unmodified lines379 // Different values for stdout and stderr380 await execa({verbose: {stdout: 'none', stderr: 'full'}})`npm run build`;381 ```382 */378-export type Options = CommonOptions<false>;383+type TextOptions<IsSync extends boolean> = CommonOptions<IsSync, TextEncodingOption | undefined>;384+type BinaryOptions<IsSync extends boolean> = Omit<CommonOptions<IsSync, BinaryEncodingOption>, 'encoding'> & {385+ readonly encoding: BinaryEncodingOption;386+};387 388+export type Options = TextOptions<false> | BinaryOptions<false>;389+390 /**391 Subprocess options, with synchronous methods.392 393 Some options are related to the subprocess output: `verbose`, `lines`, `stripFinalNewline`, `buffer`, `maxBuffer`. By default, those options apply to all file descriptors (`stdout`, `stderr`, etc.). A plain object can be passed instead to apply them to only `stdout`, `stderr`, `all` (both stdout and stderr), `ipc`, `fd3`, etc.7 unmodified lines401 // Different values for stdout and stderr402 execaSync({verbose: {stdout: 'none', stderr: 'full'}})`npm run build`;403 ```404 */395-export type SyncOptions = CommonOptions<true>;405+export type SyncOptions = TextOptions<true> | BinaryOptions<true>;406 407 export type StricterOptions<408 WideOptions extends CommonOptions,409 StrictOptions extends CommonOptions,3 unmodified linesmodified:types/methods/script.d.ts+1 added-1 removed
63 unmodified lines64 & {s: ExecaScriptSyncMethod<OptionsType>};65 66 // `$(options)` binding67 type ExecaScriptBind<OptionsType extends CommonOptions> =68- <NewOptionsType extends CommonOptions = {}>(options: NewOptionsType)68+ <NewOptionsType extends Options = {}>(options: NewOptionsType)69 => ExecaScriptMethod<OptionsType & NewOptionsType>;70 71 // `$`command`` template syntax72 type ExecaScriptTemplate<OptionsType extends CommonOptions> =45 unmodified linesmodified:types/stdio/type.d.ts+37 added-25 removed
50 unmodified lines51 type CommonStdioOption<52 IsSync extends boolean,53 IsExtra extends boolean,54 IsArray extends boolean,55+ ObjectModeChunk = unknown,56+ TransformChunk = string,57 > =58+ // TypeScript cannot contextually type inline full generator transform objects through this broad stdio union because the union also includes `{transform: Duplex | TransformStream}` wrapper objects. Users should annotate `chunk` when assigning an inline object directly to `StdinOption`, `StdoutStderrOption`, `Options['stdout']`, etc. Keep `GeneratorTransformFull` mode branches narrow, but do not add a broad full-object fallback here to try to recover contextual typing.56- SimpleStdioOption<IsSync, IsExtra, IsArray> | URL | GeneratorTransform<IsSync> | GeneratorTransformFull<IsSync> | Unless<And<Not<IsSync>, IsArray>, 3 | 4 | 5 | 6 | 7 | 8 | 9> | Unless<IsSync, DuplexTransform | WebTransform | TransformStream> | NativePipeStdioOption<IsSync, IsExtra> | {readonly file: string; readonly append?: boolean};59+ SimpleStdioOption<IsSync, IsExtra, IsArray> | URL | GeneratorTransform<IsSync, TransformChunk> | GeneratorTransformFull<IsSync, ObjectModeChunk, TransformChunk> | Unless<And<Not<IsSync>, IsArray>, 3 | 4 | 5 | 6 | 7 | 8 | 9> | Unless<IsSync, DuplexTransform | WebTransform | TransformStream> | NativePipeStdioOption<IsSync, IsExtra> | {readonly file: string; readonly append?: boolean};60 61 // Synchronous iterables excluding strings, Uint8Arrays and Arrays62 type IterableObject<IsArray extends boolean> = Iterable<unknown>63 & object30 unmodified lines94 type StdinSingleOption<95 IsSync extends boolean,96 IsExtra extends boolean,97 IsArray extends boolean,98+ TransformChunk = string,99 > =96- | CommonStdioOption<IsSync, IsExtra, IsArray>100+ | CommonStdioOption<IsSync, IsExtra, IsArray, unknown, TransformChunk>101 | InputStdioOption<IsSync, IsExtra, IsArray>;102 103 // `options.stdin`104 export type StdinOptionCommon<105 IsSync extends boolean = boolean,106 IsExtra extends boolean = boolean,103-> =104- | StdinSingleOption<IsSync, IsExtra, false>105- | ReadonlyArray<StdinSingleOption<IsSync, IsExtra, true>>;107+ TransformChunk = string,108+> = TransformChunk extends unknown109+ ? | StdinSingleOption<IsSync, IsExtra, false, TransformChunk>110+ | ReadonlyArray<StdinSingleOption<IsSync, IsExtra, true, TransformChunk>>111+ : never;112 113 // `options.stdin`, async114 export type StdinOption = StdinOptionCommon<false, false>;115 // `options.stdin`, sync3 unmodified lines119 type StdoutStderrSingleOption<120 IsSync extends boolean,121 IsExtra extends boolean,122 IsArray extends boolean,123+ TransformChunk = string,124 > =118- | CommonStdioOption<IsSync, IsExtra, IsArray>125+ | CommonStdioOption<IsSync, IsExtra, IsArray, TransformChunk, TransformChunk>126 | OutputStdioOption<IsSync, IsArray>;127 128 // `options.stdout|stderr`129+// In `objectMode`, the `chunk` argument of every array item is typed as `string`, even though only the first transform in the pipeline receives subprocess lines. The array index cannot be used to infer the pipeline position, since non-transform items are filtered out and transforms are reordered at runtime.130 export type StdoutStderrOptionCommon<131 IsSync extends boolean = boolean,132 IsExtra extends boolean = boolean,125-> =126- | StdoutStderrSingleOption<IsSync, IsExtra, false>127- | ReadonlyArray<StdoutStderrSingleOption<IsSync, IsExtra, true>>;133+ TransformChunk = string,134+> = TransformChunk extends unknown135+ ? | StdoutStderrSingleOption<IsSync, IsExtra, false, TransformChunk>136+ | ReadonlyArray<StdoutStderrSingleOption<IsSync, IsExtra, true, TransformChunk>>137+ : never;138 139 // `options.stdout|stderr`, async140 export type StdoutStderrOption = StdoutStderrOptionCommon<false, false>;141 // `options.stdout|stderr`, sync142 export type StdoutStderrSyncOption = StdoutStderrOptionCommon<true, false>;143 144 // `options.stdio[3+]`135-type StdioExtraOptionCommon<IsSync extends boolean> =136- | StdinOptionCommon<IsSync, true>137- | StdoutStderrOptionCommon<IsSync, true>;145+type StdioExtraOptionCommon<IsSync extends boolean, TransformChunk = string> =146+ | StdinOptionCommon<IsSync, true, TransformChunk>147+ | StdoutStderrOptionCommon<IsSync, true, TransformChunk>;148 149 // `options.stdin|stdout|stderr|stdio` array items150 type StdioSingleOption<151 IsSync extends boolean = boolean,152 IsExtra extends boolean = boolean,153 IsArray extends boolean = boolean,154+ TransformChunk = string,155 > =145- | StdinSingleOption<IsSync, IsExtra, IsArray>146- | StdoutStderrSingleOption<IsSync, IsExtra, IsArray>;156+ | StdinSingleOption<IsSync, IsExtra, IsArray, TransformChunk>157+ | StdoutStderrSingleOption<IsSync, IsExtra, IsArray, TransformChunk>;158 159 // Get `options.stdin|stdout|stderr|stdio` items if it is an array, else keep as is160 export type StdioSingleOptionItems<StdioOptionType> = StdioOptionType extends readonly StdioSingleOption[]161 ? StdioOptionType[number]162 : StdioOptionType;163 164 // `options.stdin|stdout|stderr|stdio`154-export type StdioOptionCommon<IsSync extends boolean = boolean> =155- | StdinOptionCommon<IsSync>156- | StdoutStderrOptionCommon<IsSync>;165+export type StdioOptionCommon<IsSync extends boolean = boolean, TransformChunk = string> =166+ | StdinOptionCommon<IsSync, boolean, TransformChunk>167+ | StdoutStderrOptionCommon<IsSync, boolean, TransformChunk>;168 169 // `options.stdio` when it is an array159-export type StdioOptionsArray<IsSync extends boolean = boolean> = readonly [160- StdinOptionCommon<IsSync, false>,161- StdoutStderrOptionCommon<IsSync, false>,162- StdoutStderrOptionCommon<IsSync, false>,163- ...ReadonlyArray<StdioExtraOptionCommon<IsSync>>,170+export type StdioOptionsArray<IsSync extends boolean = boolean, TransformChunk = string> = readonly [171+ StdinOptionCommon<IsSync, false, TransformChunk>,172+ StdoutStderrOptionCommon<IsSync, false, TransformChunk>,173+ StdoutStderrOptionCommon<IsSync, false, TransformChunk>,174+ ...ReadonlyArray<StdioExtraOptionCommon<IsSync, TransformChunk>>,175 ];176 177 // `options.stdio`167-export type StdioOptionsProperty<IsSync extends boolean = boolean> =168- | SimpleStdioOption<IsSync, false, false>169- | StdioOptionsArray<IsSync>;178+export type StdioOptionsProperty<IsSync extends boolean = boolean, TransformChunk = string> = TransformChunk extends unknown179+ ? | SimpleStdioOption<IsSync, false, false>180+ | StdioOptionsArray<IsSync, TransformChunk>181+ : never;182 183 export {};modified:types/transform/normalize.d.ts+45 added-10 removed
1 unmodified line2 import type {Duplex} from 'node:stream';3 import type {Unless} from '../utils.js';4 5 // `options.std*: Generator`6-// @todo Use `string`, `Uint8Array` or `unknown` for both the argument and the return type, based on whether `encoding: 'buffer'` and `objectMode: true` are used.6+// The `chunk` argument's type is based on the transform's mode:7+// - `binary: true` or binary `encoding`, with `objectMode: true` -> `unknown` for stdin, `Uint8Array` for stdout/stderr8+// - `binary: true` or binary `encoding` -> `Uint8Array`9+// - `objectMode: true` -> `unknown` for stdin, `string` for stdout/stderr10+// - otherwise -> `string`11+// The return type is kept as `unknown` since a transform can always yield either a `string` or an `Uint8Array`.12 // See https://github.com/sindresorhus/execa/issues/6948-export type GeneratorTransform<IsSync extends boolean> = (chunk: unknown) =>13+export type GeneratorTransform<IsSync extends boolean, Chunk = string> = (chunk: Chunk) =>14 | Unless<IsSync, AsyncGenerator<unknown, void, void>>15 | Generator<unknown, void, void>;16+type GeneratorTransformReturn<IsSync extends boolean> = ReturnType<GeneratorTransform<IsSync>>;17 type GeneratorFinal<IsSync extends boolean> = () =>18 | Unless<IsSync, AsyncGenerator<unknown, void, void>>19 | Generator<unknown, void, void>;20 3 unmodified lines24 */25 readonly objectMode?: boolean;26 };27 22-/**23-A transform or an array of transforms can be passed to the `stdin`, `stdout`, `stderr` or `stdio` option.24-25-A transform is either a generator function or a plain object with the following members.26-*/27-export type GeneratorTransformFull<IsSync extends boolean> = TransformCommon & {28+// A `GeneratorTransformFull` shape whose `transform` narrows the `chunk` argument to `Chunk`.29+// The mode-independent documentation lives here.30+type GeneratorTransformBase<IsSync extends boolean, Chunk> = TransformCommon & {31 /**32 Map or filter the input or output of the subprocess.33 */31- readonly transform: GeneratorTransform<IsSync>;34+ readonly transform: GeneratorTransform<IsSync, Chunk>;35 36 /**37 Create additional lines after the last one.38 */4 unmodified lines43 */44 readonly binary?: boolean;45 46 /**44- If `true`, keep newlines in each `line` argument. Also, this allows multiple `yield`s to produces a single line.47+ If `true`, keep newlines in each `line` argument. Also, this allows multiple `yield`s to produce a single line.48 */49 readonly preserveNewlines?: boolean;50 };51 52+// For stdout/stderr, this literal option pair accepts explicitly typed `Uint8Array` callbacks, matching runtime binary chunks, while still allowing stdin-style `unknown` object-mode callbacks.53+type GeneratorTransformBinaryObjectMode<IsSync extends boolean, ObjectModeChunk> = ObjectModeChunk extends string54+ ? Omit<GeneratorTransformBase<IsSync, unknown>, 'transform' | 'objectMode' | 'binary'> & {55+ /**56+ Map or filter the input or output of the subprocess.57+ */58+ transform(chunk: Uint8Array): GeneratorTransformReturn<IsSync>;59+ // eslint-disable-next-line @typescript-eslint/unified-signatures -- Combining these as `unknown | Uint8Array` would collapse to `unknown` and reject explicitly typed `Uint8Array` callbacks.60+ transform(chunk: unknown): GeneratorTransformReturn<IsSync>;61+62+ readonly objectMode: true;63+ readonly binary: true;64+ }65+ : GeneratorTransformBase<IsSync, unknown> & {readonly objectMode: true; readonly binary: true};66+67+/**68+A transform or an array of transforms can be passed to the `stdin`, `stdout`, `stderr` or `stdio` option.69+70+A transform is either a generator function or a plain object with the following members.71+*/72+export type GeneratorTransformFull<IsSync extends boolean, ObjectModeChunk = unknown, TransformChunk = string> =73+ // Non-literal `binary` or `objectMode` values are accepted at runtime, but their mode is unknown at compile time.74+ | (GeneratorTransformBase<IsSync, unknown> & {readonly objectMode?: boolean; readonly binary?: boolean})75+ // `binary: true` without `objectMode` -> the `chunk` argument is an `Uint8Array`.76+ | (GeneratorTransformBase<IsSync, Uint8Array> & {readonly objectMode?: false; readonly binary: true})77+ // `binary: true` with `objectMode` -> the `chunk` argument is `unknown` for stdin, `Uint8Array` for stdout/stderr.78+ | GeneratorTransformBinaryObjectMode<IsSync, ObjectModeChunk>79+ // `binary: false` without `objectMode` -> the `chunk` argument depends on the subprocess encoding.80+ | (GeneratorTransformBase<IsSync, TransformChunk> & {readonly objectMode?: false; readonly binary?: false})81+ // `objectMode: true` with `binary: false` -> the `chunk` argument depends on the stdio direction.82+ | (GeneratorTransformBase<IsSync, ObjectModeChunk> & {readonly objectMode: true; readonly binary?: false});83+84 // `options.std*: Duplex`85 export type DuplexTransform = TransformCommon & {86 readonly transform: Duplex;87 };7 unmodified lines