output-sync.js
This file
Path
lib/stdio/output-sync.js
104 lines · 2.8 KiB · JavaScript
Last changed by
Fix `lines` option (#957)ehmickybefore adoption15ab4e6
History of this fileTake it
Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
line 1why, one step back1import {writeFileSync} from 'node:fs';Add support for `all` option to `execaSync()` (#956)ehmickybefore adoption63b5ac4
line 2why, one step back2import {joinToString, joinToUint8Array, bufferToUint8Array, isUint8Array, concatUint8Arrays} from './uint-array.js';Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
line 3why, one step back3import {getGenerators, runGeneratorsSync} from './generator.js';Fix `lines` option (#957)ehmickybefore adoption15ab4e6
line 4why, one step back4import {splitLinesSync} from './split.js';Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc
line 5why, one step back5import {FILE_TYPES} from './type.js';Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 6–14why, one step back67// Apply `stdout`/`stderr` options, after spawning, in sync mode8export const transformOutputSync = (fileDescriptors, {output}, options) => {9 if (output === null) {10 return {output: Array.from({length: 3})};11 }1213 const state = {};14 const transformedOutput = output.map((result, fdNumber) =>Add support for `lines` option with `execaSync()` (#951)ehmickybefore adoption88bff1f
line 15why, one step back15 transformOutputResultSync({result, fileDescriptors, fdNumber, state}, options));Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 16–18why, one step back16 return {output: transformedOutput, ...state};17};18Fix `lines` option (#957)ehmickybefore adoption15ab4e6
line 19why, one step back19const transformOutputResultSync = ({result, fileDescriptors, fdNumber, state}, {buffer, encoding, lines, stripFinalNewline}) => {Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
line 20why, one step back20 if (result === null) {Add support for `all` option to `execaSync()` (#956)ehmickybefore adoption63b5ac4
line 21why, one step back21 return;Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 22–23why, one step back22 }23Fix `lines` option (#957)ehmickybefore adoption15ab4e6
line 24why, one step back24 const {stdioItems, objectMode} = fileDescriptors[fdNumber];Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
line 25why, one step back25 const uint8ArrayResult = bufferToUint8Array(result);Simplify implementation (#955)ehmickybefore adoption8ad9ee7
line 26why, one step back26 const generators = getGenerators(stdioItems);Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
line 27why, one step back27 const chunks = runOutputGeneratorsSync([uint8ArrayResult], generators, state);Fix `lines` option (#957)ehmickybefore adoption15ab4e6
line 28why, one step back28 const {serializedResult, finalResult} = serializeChunks({chunks, objectMode, encoding, lines, stripFinalNewline});Add support for `buffer: false` to `execaSync()` (#953)ehmickybefore adoptiond0052ac
line 29why, one step back29 const returnedResult = buffer ? finalResult : undefined;Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 30–32why, one step back3031 try {32 if (state.error === undefined) {Simplify implementation (#955)ehmickybefore adoption8ad9ee7
line 33why, one step back33 writeToFiles(serializedResult, stdioItems);Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 34–35why, one step back34 }35Add support for `buffer: false` to `execaSync()` (#953)ehmickybefore adoptiond0052ac
line 36why, one step back36 return returnedResult;Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 37–38why, one step back37 } catch (error) {38 state.error = error;Add support for `buffer: false` to `execaSync()` (#953)ehmickybefore adoptiond0052ac
line 39why, one step back39 return returnedResult;Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 40–51why, one step back40 }41};4243const runOutputGeneratorsSync = (chunks, generators, state) => {44 try {45 return runGeneratorsSync(chunks, generators);46 } catch (error) {47 state.error = error;48 return chunks;49 }50};51Fix `lines` option (#957)ehmickybefore adoption15ab4e6
line 52why, one step back52const serializeChunks = ({chunks, objectMode, encoding, lines, stripFinalNewline}) => {Simplify implementation (#955)ehmickybefore adoption8ad9ee7
line 53why, one step back53 if (objectMode) {Add support for `lines` option with `execaSync()` (#951)ehmickybefore adoption88bff1f
line 54why, one step back54 return {finalResult: chunks};Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 55–56why, one step back55 }56Fix `lines` option (#957)ehmickybefore adoption15ab4e6
lines 57–67why, one step back57 if (encoding === 'buffer') {58 const serializedResult = joinToUint8Array(chunks);59 return {serializedResult, finalResult: serializedResult};60 }6162 const serializedResult = joinToString(chunks);63 if (!lines) {64 return {serializedResult, finalResult: serializedResult};65 }6667 return {serializedResult, finalResult: splitLinesSync(serializedResult, !stripFinalNewline)};Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 68–69why, one step back68};69Simplify implementation (#955)ehmickybefore adoption8ad9ee7
lines 70–71why, one step back70const writeToFiles = (serializedResult, stdioItems) => {71 for (const {type, path} of stdioItems) {Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc
lines 72–73why, one step back72 if (FILE_TYPES.has(type)) {73 writeFileSync(path, serializedResult);Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 74–75why, one step back74 }75 }Add support for `all` option to `execaSync()` (#956)ehmickybefore adoption63b5ac4
lines 76–103why, one step back76};7778export const getAllSync = ([, stdout, stderr], {all}) => {79 if (!all) {80 return;81 }8283 if (stdout === undefined) {84 return stderr;85 }8687 if (stderr === undefined) {88 return stdout;89 }9091 if (Array.isArray(stdout)) {92 return Array.isArray(stderr) ? [...stdout, ...stderr] : [...stdout, stderr];93 }9495 if (Array.isArray(stderr)) {96 return [stdout, ...stderr];97 }9899 if (isUint8Array(stdout) && isUint8Array(stderr)) {100 return concatUint8Arrays([stdout, stderr]);101 }102103 return `${stdout}${stderr}`;Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
line 104why, one step back104};