sync.js
This file
Path
lib/stdio/sync.js
67 lines · 1.9 KiB · JavaScript
Last changed by
Refactor `stdio` option logic (#639)ehmickybefore adoptiond6b4553
History of this fileTake it
Refactor `stdio` options (#623)ehmickybefore adoption2d76897
line 1why, one step back1import {readFileSync, writeFileSync} from 'node:fs';Fix `stdio` option (#634)ehmickybefore adoption640ae39
line 2why, one step back2import {isStream as isNodeStream} from 'is-stream';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–12why, one step back4import {TYPE_TO_MESSAGE} from './type.js';56// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in sync mode7export const handleInputSync = options => {8 const stdioStreams = handleInput(addPropertiesSync, options);9 addInputOptionSync(stdioStreams, options);10 return stdioStreams;11};12Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 13–16why, one step back13const forbiddenIfStreamSync = ({value, optionName}) => {14 if (isNodeStream(value)) {15 forbiddenIfSync({type: 'nodeStream', optionName});16 }Refactor `stdio` option logic (#639)ehmickybefore adoptiond6b4553
lines 17–18why, one step back1718 return {};Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 19–20why, one step back19};20Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 21–30why, one step back21const forbiddenIfSync = ({type, optionName}) => {22 throw new TypeError(`The \`${optionName}\` option cannot be ${TYPE_TO_MESSAGE[type]} in sync mode.`);23};2425const addPropertiesSync = {26 input: {27 filePath: ({value}) => ({value: readFileSync(value), type: 'stringOrBuffer'}),28 webStream: forbiddenIfSync,29 nodeStream: forbiddenIfSync,30 iterable: forbiddenIfSync,Fix `stdio` option (#634)ehmickybefore adoption640ae39
line 31why, one step back31 native: forbiddenIfStreamSync,Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 32–36why, one step back32 },33 output: {34 webStream: forbiddenIfSync,35 nodeStream: forbiddenIfSync,36 iterable: forbiddenIfSync,Fix `stdio` option (#634)ehmickybefore adoption640ae39
line 37why, one step back37 native: forbiddenIfStreamSync,Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 38–53why, one step back38 },39};4041const addInputOptionSync = (stdioStreams, options) => {42 const inputValue = stdioStreams.find(({type}) => type === 'stringOrBuffer')?.value;43 if (inputValue !== undefined) {44 options.input = inputValue;45 }46};4748// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, after spawning, in sync mode49export const pipeOutputSync = (stdioStreams, result) => {50 if (result.output === null) {51 return;52 }53Refactor `stdio` option logic (#639)ehmickybefore adoptiond6b4553
lines 54–55why, one step back54 for (const stdioStream of stdioStreams) {55 pipeStdioOptionSync(result.output[stdioStream.index], stdioStream);Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 56–67why, one step back56 }57};5859const pipeStdioOptionSync = (result, {type, value, direction}) => {60 if (result === null || direction === 'input') {61 return;62 }6364 if (type === 'filePath') {65 writeFileSync(value, result);66 }67};