sync.js
This file
Path
lib/stdio/sync.js
73 lines · 2.0 KiB · JavaScript
Last changed by
Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25
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–26why, 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: {Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25
line 27why, one step back27 filePath: ({value}) => ({value: readFileSync(value, 'utf8'), type: 'stringOrBuffer'}),Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 28–30why, one step back28 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–41why, one step back38 },39};4041const addInputOptionSync = (stdioStreams, options) => {Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25
lines 42–44why, one step back42 const inputs = stdioStreams.filter(({type}) => type === 'stringOrBuffer');43 if (inputs.length === 0) {44 return;Refactor `stdio` options (#623)ehmickybefore adoption2d76897
line 45why, one step back45 }Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25
lines 46–49why, one step back4647 options.input = inputs.length === 148 ? inputs[0].value49 : inputs.map(({value}) => serializeInput(value)).join('');Refactor `stdio` options (#623)ehmickybefore adoption2d76897
line 50why, one step back50};Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25
lines 51–52why, one step back5152const serializeInput = value => typeof value === 'string' ? value : new TextDecoder().decode(value);Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 53–59why, one step back5354// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, after spawning, in sync mode55export const pipeOutputSync = (stdioStreams, result) => {56 if (result.output === null) {57 return;58 }59Refactor `stdio` option logic (#639)ehmickybefore adoptiond6b4553
lines 60–61why, one step back60 for (const stdioStream of stdioStreams) {61 pipeStdioOptionSync(result.output[stdioStream.index], stdioStream);Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 62–73why, one step back62 }63};6465const pipeStdioOptionSync = (result, {type, value, direction}) => {66 if (result === null || direction === 'input') {67 return;68 }6970 if (type === 'filePath') {71 writeFileSync(value, result);72 }73};