-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstdio.ts
More file actions
65 lines (62 loc) · 2.42 KB
/
stdio.ts
File metadata and controls
65 lines (62 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* @fileoverview Stdio configuration helpers for `spawn` callers.
*
* `isStdioType` is dual-purpose:
* - One arg: validate that a value is a known stdio mode
* (`'pipe'` / `'ignore'` / `'inherit'` / `'overlapped'`).
* - Two args: check whether the caller's stdio config matches a
* specific mode. Useful in spinner-pause logic — the spinner only
* stops when the child writes to a non-piped stream that would
* otherwise interleave with spinner redraws.
*
* Two-arg behavior special-cases `null` / `undefined` ↔ `'pipe'`
* because Node.js defaults unspecified entries to `'pipe'`. The
* three-element-array branch handles the common `[in, out, err]`
* tuple where all three streams use the same mode.
*/
import { isArray } from '../arrays/predicates'
import type { StdioType } from './types'
/**
* Check if stdio configuration matches a specific type.
* When called with one argument, validates if it's a valid stdio type.
* When called with two arguments, checks if the stdio config matches the specified type.
*
* @param {string | string[]} stdio - Stdio configuration to check
* @param {StdioType | undefined} type - Expected stdio type (optional)
* @returns {boolean} `true` if stdio matches the type or is valid
*
* @example
* // Check if valid stdio type
* isStdioType('pipe') // true
* isStdioType('invalid') // false
*
* @example
* // Check if stdio matches specific type
* isStdioType('pipe', 'pipe') // true
* isStdioType(['pipe', 'pipe', 'pipe'], 'pipe') // true
* isStdioType('ignore', 'pipe') // false
*/
/*@__NO_SIDE_EFFECTS__*/
export function isStdioType(
stdio: string | string[],
type?: StdioType | undefined,
): boolean {
// If called with one argument, check if it's a valid stdio type.
// biome-ignore lint/complexity/noArguments: Function overload detection for single vs two-arg calls.
if (arguments.length === 1) {
const validTypes = ['pipe', 'ignore', 'inherit', 'overlapped']
return typeof stdio === 'string' && validTypes.includes(stdio)
}
// Original two-argument behavior.
// Accept null/undefined as equivalent to 'pipe' because Node.js defaults
// unspecified stdio entries to 'pipe'. Tests explicitly cover this contract.
return (
stdio === type ||
((stdio === null || stdio === undefined) && type === 'pipe') ||
(isArray(stdio) &&
stdio.length > 2 &&
stdio[0] === type &&
stdio[1] === type &&
stdio[2] === type)
)
}