Skip to content
Myr

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 file

Take it

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

line 1why, one step back
1import {writeFileSync} from 'node:fs';

Add support for `all` option to `execaSync()` (#956)ehmickybefore adoption63b5ac4

line 2why, one step back
2import {joinToString, joinToUint8Array, bufferToUint8Array, isUint8Array, concatUint8Arrays} from './uint-array.js';

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

line 3why, one step back
3import {getGenerators, runGeneratorsSync} from './generator.js';

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

line 4why, one step back
4import {splitLinesSync} from './split.js';

Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc

line 5why, one step back
5import {FILE_TYPES} from './type.js';

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 6–14why, one step back
6
7// 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 back
15 transformOutputResultSync({result, fileDescriptors, fdNumber, state}, options));

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 16–18why, one step back
16 return {output: transformedOutput, ...state};
17};18

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

line 19why, one step back
19const transformOutputResultSync = ({result, fileDescriptors, fdNumber, state}, {buffer, encoding, lines, stripFinalNewline}) => {

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

line 20why, one step back
20 if (result === null) {

Add support for `all` option to `execaSync()` (#956)ehmickybefore adoption63b5ac4

line 21why, one step back
21 return;

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 22–23why, one step back
22 }
23

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

line 24why, one step back
24 const {stdioItems, objectMode} = fileDescriptors[fdNumber];

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

line 25why, one step back
25 const uint8ArrayResult = bufferToUint8Array(result);

Simplify implementation (#955)ehmickybefore adoption8ad9ee7

line 26why, one step back
26 const generators = getGenerators(stdioItems);

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

line 27why, one step back
27 const chunks = runOutputGeneratorsSync([uint8ArrayResult], generators, state);

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

line 28why, one step back
28 const {serializedResult, finalResult} = serializeChunks({chunks, objectMode, encoding, lines, stripFinalNewline});

Add support for `buffer: false` to `execaSync()` (#953)ehmickybefore adoptiond0052ac

line 29why, one step back
29 const returnedResult = buffer ? finalResult : undefined;

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 30–32why, one step back
30
31 try {32 if (state.error === undefined) {

Simplify implementation (#955)ehmickybefore adoption8ad9ee7

line 33why, one step back
33 writeToFiles(serializedResult, stdioItems);

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 34–35why, one step back
34 }
35

Add support for `buffer: false` to `execaSync()` (#953)ehmickybefore adoptiond0052ac

line 36why, one step back
36 return returnedResult;

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 37–38why, one step back
37 } catch (error) {
38 state.error = error;

Add support for `buffer: false` to `execaSync()` (#953)ehmickybefore adoptiond0052ac

line 39why, one step back
39 return returnedResult;

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 40–51why, one step back
40 }
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};51

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

line 52why, one step back
52const serializeChunks = ({chunks, objectMode, encoding, lines, stripFinalNewline}) => {

Simplify implementation (#955)ehmickybefore adoption8ad9ee7

line 53why, one step back
53 if (objectMode) {

Add support for `lines` option with `execaSync()` (#951)ehmickybefore adoption88bff1f

line 54why, one step back
54 return {finalResult: chunks};

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 55–56why, one step back
55 }
56

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

lines 57–67why, one step back
57 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 back
68};
69

Simplify implementation (#955)ehmickybefore adoption8ad9ee7

lines 70–71why, one step back
70const 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 back
72 if (FILE_TYPES.has(type)) {
73 writeFileSync(path, serializedResult);

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 74–75why, one step back
74 }
75 }

Add support for `all` option to `execaSync()` (#956)ehmickybefore adoption63b5ac4

lines 76–103why, one step back
76};
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 back
104};