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 fileTake it
Refactor `stdio` options (#623)ehmickybefore adoption2d76897
line 1why, one step back1import {readFileSync, writeFileSync} from 'node:fs';Split files (#837)ehmickybefore adoption672c2d7
line 2why, one step back2import {bufferToUint8Array, binaryToString} from '../utils.js';Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297
line 3why, one step back3import {handleInput} from './handle.js';Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 4–6why, one step back4import {TYPE_TO_MESSAGE} from './type.js';56// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in sync modePrint output with `verbose` option (#884)ehmickybefore adoption6fda30f
lines 7–8why, one step back7export const handleInputSync = (options, verboseInfo) => {8 const {stdioStreamsGroups} = handleInput(addPropertiesSync, options, verboseInfo, true);Simplify `std*` options logic (#691)ehmickybefore adoption98b0ee3
lines 9–10why, one step back9 addInputOptionSync(stdioStreamsGroups, options);10 return stdioStreamsGroups;Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 11–18why, one step back11};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 back19 generator: forbiddenIfSync,Improve syntax of `std*: filePath` option (#671)ehmickybefore adoption90e0e13
lines 20–21why, one step back20 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 back22 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 back27 generator: forbiddenIfSync,Improve syntax of `std*: filePath` option (#671)ehmickybefore adoption90e0e13
line 28why, one step back28 filePath: ({value}) => ({value: value.file}),Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 29–31why, one step back29 webStream: forbiddenIfSync,30 nodeStream: forbiddenIfSync,31 iterable: forbiddenIfSync,Allow `stdin: uint8Array` option (#670)ehmickybefore adoption2ac81d4
line 32why, one step back32 uint8Array: forbiddenIfSync,Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 33–35why, one step back33 },34};35Simplify `std*` options logic (#691)ehmickybefore adoption98b0ee3
line 36why, one step back36const addInputOptionSync = (stdioStreamsGroups, options) => {Allow Node.js streams with synchronous methods (#708)ehmickybefore adoption86bdadf
line 37why, one step back37 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 back38 if (inputs.length === 0) {39 return;Refactor `stdio` options (#623)ehmickybefore adoption2d76897
line 40why, one step back40 }Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25
lines 41–43why, one step back4142 options.input = inputs.length === 143 ? inputs[0].valueValidate `input` option's type (#668)ehmickybefore adoption65742c6
line 44why, one step back44 : inputs.map(stdioStream => serializeInput(stdioStream)).join('');Refactor `stdio` options (#623)ehmickybefore adoption2d76897
line 45why, one step back45};Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25
line 46why, one step back46Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
line 47why, one step back47const serializeInput = ({type, value}) => type === 'string' ? value : binaryToString(value);Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 48–49why, one step back4849// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, after spawning, in sync modeRefactor `reject` option (#868)ehmickybefore adoption1f8a55c
lines 50–51why, one step back50export const pipeOutputSync = (stdioStreamsGroups, {output}) => {51 if (output === null) {Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 52–54why, one step back52 return;53 }54Simplify `std*` options logic (#691)ehmickybefore adoption98b0ee3
lines 55–56why, one step back55 for (const stdioStreams of stdioStreamsGroups) {56 for (const stdioStream of stdioStreams) {Refactor `reject` option (#868)ehmickybefore adoption1f8a55c
line 57why, one step back57 pipeStdioOptionSync(output[stdioStream.fdNumber], stdioStream);Simplify `std*` options logic (#691)ehmickybefore adoption98b0ee3
line 58why, one step back58 }Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 59–66why, one step back59 }60};6162const pipeStdioOptionSync = (result, {type, value, direction}) => {63 if (result === null || direction === 'input') {64 return;65 }66Improve syntax of `std*: filePath` option (#671)ehmickybefore adoption90e0e13
line 67why, one step back67 if (type === 'fileUrl' || type === 'filePath') {Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 68–70why, one step back68 writeFileSync(value, result);69 }70};