-
-
Notifications
You must be signed in to change notification settings - Fork 227
/
type.d.ts
164 lines (144 loc) · 5.03 KB
/
type.d.ts
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import type {Readable, Writable} from 'node:stream';
import type {
Not,
And,
Or,
Unless,
AndUnless,
} from '../utils';
import type {
GeneratorTransform,
GeneratorTransformFull,
DuplexTransform,
WebTransform,
} from '../transform/normalize';
type IsStandardStream<FdNumber extends string> = FdNumber extends keyof StandardStreams ? true : false;
export type StandardStreams = readonly ['stdin', 'stdout', 'stderr'];
// When `options.stdin|stdout|stderr|stdio` is set to one of those values, no stream is created
export type NoStreamStdioOption<FdNumber extends string> =
| 'ignore'
| 'inherit'
| 'ipc'
| number
| Readable
| Writable
| Unless<IsStandardStream<FdNumber>, undefined>
| readonly [NoStreamStdioOption<FdNumber>];
// `options.stdio` when it is not an array
type SimpleStdioOption<
IsSync extends boolean,
IsExtra extends boolean,
IsArray extends boolean,
> =
| undefined
| 'pipe'
| Unless<And<And<Not<IsSync>, IsArray>, IsExtra>, 'inherit'>
| Unless<IsArray, 'ignore'>
| Unless<IsSync, 'overlapped'>;
// Values available in both `options.stdin|stdio` and `options.stdout|stderr|stdio`
type CommonStdioOption<
IsSync extends boolean,
IsExtra extends boolean,
IsArray extends boolean,
> =
| SimpleStdioOption<IsSync, IsExtra, IsArray>
| URL
| {readonly file: string}
| GeneratorTransform<IsSync>
| GeneratorTransformFull<IsSync>
| Unless<And<Not<IsSync>, IsArray>, 3 | 4 | 5 | 6 | 7 | 8 | 9>
| Unless<Or<IsSync, IsArray>, 'ipc'>
| Unless<IsSync, DuplexTransform | WebTransform | TransformStream>;
// Synchronous iterables excluding strings, Uint8Arrays and Arrays
type IterableObject<IsArray extends boolean> = Iterable<unknown>
& object
& {readonly BYTES_PER_ELEMENT?: never}
& AndUnless<IsArray, {readonly lastIndexOf?: never}>;
// `process.stdin|stdout|stderr` are `Duplex` with a `fd` property.
// This ensures they can only be passed to `stdin`/`stdout`/`stderr`, based on their direction.
type ProcessStdinFd = {readonly fd?: 0};
type ProcessStdoutStderrFd = {readonly fd?: 1 | 2};
// Values available only in `options.stdin|stdio`
type InputStdioOption<
IsSync extends boolean,
IsExtra extends boolean,
IsArray extends boolean,
> =
| 0
| Unless<And<IsSync, IsExtra>, Uint8Array | IterableObject<IsArray>>
| Unless<And<IsSync, IsArray>, Readable & ProcessStdinFd>
| Unless<IsSync, (AsyncIterable<unknown> & ProcessStdinFd) | ReadableStream>;
// Values available only in `options.stdout|stderr|stdio`
type OutputStdioOption<
IsSync extends boolean,
IsArray extends boolean,
> =
| 1
| 2
| Unless<And<IsSync, IsArray>, Writable & ProcessStdoutStderrFd>
| Unless<IsSync, WritableStream>;
// `options.stdin` array items
type StdinSingleOption<
IsSync extends boolean = boolean,
IsExtra extends boolean = boolean,
IsArray extends boolean = boolean,
> =
| CommonStdioOption<IsSync, IsExtra, IsArray>
| InputStdioOption<IsSync, IsExtra, IsArray>;
// `options.stdin`
export type StdinOptionCommon<
IsSync extends boolean = boolean,
IsExtra extends boolean = boolean,
> =
| StdinSingleOption<IsSync, IsExtra, false>
| ReadonlyArray<StdinSingleOption<IsSync, IsExtra, true>>;
// `options.stdin`, async
export type StdinOption = StdinOptionCommon<false, false>;
// `options.stdin`, sync
export type StdinSyncOption = StdinOptionCommon<true, false>;
// `options.stdout|stderr` array items
type StdoutStderrSingleOption<
IsSync extends boolean = boolean,
IsExtra extends boolean = boolean,
IsArray extends boolean = boolean,
> =
| CommonStdioOption<IsSync, IsExtra, IsArray>
| OutputStdioOption<IsSync, IsArray>;
// `options.stdout|stderr`
export type StdoutStderrOptionCommon<
IsSync extends boolean = boolean,
IsExtra extends boolean = boolean,
> =
| StdoutStderrSingleOption<IsSync, IsExtra, false>
| ReadonlyArray<StdoutStderrSingleOption<IsSync, IsExtra, true>>;
// `options.stdout|stderr`, async
export type StdoutStderrOption = StdoutStderrOptionCommon<false, false>;
// `options.stdout|stderr`, sync
export type StdoutStderrSyncOption = StdoutStderrOptionCommon<true, false>;
// `options.stdio[3+]`
type StdioExtraOptionCommon<IsSync extends boolean = boolean> =
| StdinOptionCommon<IsSync, true>
| StdoutStderrOptionCommon<IsSync, true>;
// `options.stdin|stdout|stderr|stdio` array items
export type StdioSingleOption<
IsSync extends boolean = boolean,
IsExtra extends boolean = boolean,
IsArray extends boolean = boolean,
> =
| StdinSingleOption<IsSync, IsExtra, IsArray>
| StdoutStderrSingleOption<IsSync, IsExtra, IsArray>;
// `options.stdin|stdout|stderr|stdio`
export type StdioOptionCommon<IsSync extends boolean = boolean> =
| StdinOptionCommon<IsSync>
| StdoutStderrOptionCommon<IsSync>;
// `options.stdio` when it is an array
export type StdioOptionsArray<IsSync extends boolean = boolean> = readonly [
StdinOptionCommon<IsSync, false>,
StdoutStderrOptionCommon<IsSync, false>,
StdoutStderrOptionCommon<IsSync, false>,
...ReadonlyArray<StdioExtraOptionCommon<IsSync>>,
];
// `options.stdio`
export type StdioOptionsProperty<IsSync extends boolean = boolean> =
| SimpleStdioOption<IsSync, false, false>
| StdioOptionsArray<IsSync>;