Skip to content
Myr

sync.js

This file

Path

lib/stdio/sync.js

70 lines · 2.3 KiB · JavaScript

Last changed by

Print output with `verbose` option (#884)ehmickybefore adoption6fda30f

History of this file

Take it

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

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

Split files (#837)ehmickybefore adoption672c2d7

line 2why, one step back
2import {bufferToUint8Array, binaryToString} from '../utils.js';

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

line 3why, one step back
3import {handleInput} from './handle.js';

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 4–6why, one step back
4import {TYPE_TO_MESSAGE} from './type.js';
56// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in sync mode

Print output with `verbose` option (#884)ehmickybefore adoption6fda30f

lines 7–8why, one step back
7export const handleInputSync = (options, verboseInfo) => {
8 const {stdioStreamsGroups} = handleInput(addPropertiesSync, options, verboseInfo, true);

Simplify `std*` options logic (#691)ehmickybefore adoption98b0ee3

lines 9–10why, one step back
9 addInputOptionSync(stdioStreamsGroups, options);
10 return stdioStreamsGroups;

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 11–18why, one step back
11};
1213const forbiddenIfSync = ({type, optionName}) => {14 throw new TypeError(`The \`${optionName}\` option cannot be ${TYPE_TO_MESSAGE[type]} in sync mode.`);15};1617const addPropertiesSync = {18 input: {

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

line 19why, one step back
19 generator: forbiddenIfSync,

Improve syntax of `std*: filePath` option (#671)ehmickybefore adoption90e0e13

lines 20–21why, one step back
20 fileUrl: ({value}) => ({value: bufferToUint8Array(readFileSync(value)), type: 'uint8Array'}),
21 filePath: ({value}) => ({value: bufferToUint8Array(readFileSync(value.file)), type: 'uint8Array'}),

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 22–26why, one step back
22 webStream: forbiddenIfSync,
23 nodeStream: forbiddenIfSync,24 iterable: forbiddenIfSync,25 },26 output: {

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

line 27why, one step back
27 generator: forbiddenIfSync,

Improve syntax of `std*: filePath` option (#671)ehmickybefore adoption90e0e13

line 28why, one step back
28 filePath: ({value}) => ({value: value.file}),

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 29–31why, one step back
29 webStream: forbiddenIfSync,
30 nodeStream: forbiddenIfSync,31 iterable: forbiddenIfSync,

Allow `stdin: uint8Array` option (#670)ehmickybefore adoption2ac81d4

line 32why, one step back
32 uint8Array: forbiddenIfSync,

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

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

Simplify `std*` options logic (#691)ehmickybefore adoption98b0ee3

line 36why, one step back
36const addInputOptionSync = (stdioStreamsGroups, options) => {

Allow Node.js streams with synchronous methods (#708)ehmickybefore adoption86bdadf

line 37why, one step back
37 const inputs = stdioStreamsGroups.flat().filter(({direction, type}) => direction === 'input' && (type === 'string' || type === 'uint8Array'));

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 38–39why, one step back
38 if (inputs.length === 0) {
39 return;

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

line 40why, one step back
40 }

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 41–43why, one step back
41
42 options.input = inputs.length === 143 ? inputs[0].value

Validate `input` option's type (#668)ehmickybefore adoption65742c6

line 44why, one step back
44 : inputs.map(stdioStream => serializeInput(stdioStream)).join('');

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

line 45why, one step back
45};

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

line 46why, one step back
46

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

line 47why, one step back
47const serializeInput = ({type, value}) => type === 'string' ? value : binaryToString(value);

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 48–49why, one step back
48
49// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, after spawning, in sync mode

Refactor `reject` option (#868)ehmickybefore adoption1f8a55c

lines 50–51why, one step back
50export const pipeOutputSync = (stdioStreamsGroups, {output}) => {
51 if (output === null) {

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 52–54why, one step back
52 return;
53 }54

Simplify `std*` options logic (#691)ehmickybefore adoption98b0ee3

lines 55–56why, one step back
55 for (const stdioStreams of stdioStreamsGroups) {
56 for (const stdioStream of stdioStreams) {

Refactor `reject` option (#868)ehmickybefore adoption1f8a55c

line 57why, one step back
57 pipeStdioOptionSync(output[stdioStream.fdNumber], stdioStream);

Simplify `std*` options logic (#691)ehmickybefore adoption98b0ee3

line 58why, one step back
58 }

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 59–66why, one step back
59 }
60};6162const pipeStdioOptionSync = (result, {type, value, direction}) => {63 if (result === null || direction === 'input') {64 return;65 }66

Improve syntax of `std*: filePath` option (#671)ehmickybefore adoption90e0e13

line 67why, one step back
67 if (type === 'fileUrl' || type === 'filePath') {

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 68–70why, one step back
68 writeFileSync(value, result);
69 }70};