Skip to content
Myr

sync.js

This file

Path

lib/stdio/sync.js

80 lines · 2.5 KiB · JavaScript

Last changed by

Stricter values for the `encoding` option (#928)ehmickybefore adoption477bbc4

History of this file

Take it

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

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

Stricter values for the `encoding` option (#928)ehmickybefore adoption477bbc4

line 2why, one step back
2import {bufferToUint8Array, uint8ArrayToString} 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

line 7why, one step back
7export const handleInputSync = (options, verboseInfo) => {

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 8–10why, one step back
8 const {fileDescriptors} = handleInput(addPropertiesSync, options, verboseInfo, true);
9 addInputOptionSync(fileDescriptors, options);10 return fileDescriptors;

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,

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 22–24why, one step back
22 webStream: forbiddenIfSync,
23 nodeStream: forbiddenIfSync,24 iterable: forbiddenIfSync,

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 25–27why, one step back
25 string: ({value}) => ({contents: value}),
26 uint8Array: ({value}) => ({contents: value}),27 native() {},

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 28–29why, one step back
28 },
29 output: {

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

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 31–32why, one step back
31 fileUrl: ({value}) => ({path: value}),
32 filePath: ({value: {file}}) => ({path: file}),

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 33–35why, one step back
33 webStream: forbiddenIfSync,
34 nodeStream: forbiddenIfSync,35 iterable: forbiddenIfSync,

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 36why, one step back
36 string: forbiddenIfSync,

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

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 38why, one step back
38 native() {},

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 39–41why, one step back
39 },
40};41

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 42–48why, one step back
42const addInputOptionSync = (fileDescriptors, options) => {
43 const allContents = fileDescriptors44 .filter(({direction}) => direction === 'input')45 .flatMap(({stdioItems}) => stdioItems)46 .map(({contents}) => contents)47 .filter(contents => contents !== undefined);48 if (allContents.length === 0) {

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

line 49why, one step back
49 return;

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

line 50why, one step back
50 }

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

line 51why, one step back
51

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 52–54why, one step back
52 options.input = allContents.length === 1
53 ? allContents[0]54 : allContents.map(contents => serializeContents(contents)).join('');

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

line 55why, one step back
55};

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

line 56why, one step back
56

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 57why, one step back
57const serializeContents = contents => typeof contents === 'string' ? contents : uint8ArrayToString(contents);

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 60why, one step back
60export const pipeOutputSync = (fileDescriptors, {output}) => {

Refactor `reject` option (#868)ehmickybefore adoption1f8a55c

line 61why, one step back
61 if (output === null) {

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 62–64why, one step back
62 return;
63 }64

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 65–67why, one step back
65 for (const {stdioItems, fdNumber, direction} of fileDescriptors) {
66 for (const {type, path} of stdioItems) {67 pipeStdioItemSync(output[fdNumber], type, path, direction);

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

line 68why, one step back
68 }

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 69–71why, one step back
69 }
70};71

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 72why, one step back
72const pipeStdioItemSync = (result, type, path, direction) => {

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 73–76why, one step back
73 if (result === null || direction === 'input') {
74 return;75 }76

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

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 78why, one step back
78 writeFileSync(path, result);

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 79–80why, one step back
79 }
80};