-
Notifications
You must be signed in to change notification settings - Fork 237
/
child_process.dart
260 lines (239 loc) · 6.77 KB
/
child_process.dart
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import 'dart:async';
import 'dart:js_interop';
import 'dart:typed_data';
import 'package:actions/actions.dart';
import 'package:actions/src/node/process_manager.dart';
import 'package:actions/src/util.dart';
/// Wraps https://nodejs.org/api/child_process.html
@JS()
external ChildProcess get childProcess;
@JS()
@anonymous
extension type ChildProcess(JSObject it) {
@JS('spawn')
external NodeChildProcess _spawn(
String command,
JSArray args,
_ChildProcessOptions options,
);
@JS('exec')
external NodeChildProcess _exec(
String command,
_ChildProcessOptions options, [
JSFunction callback,
] // (error: Error, stdout: string, stderr: string) => void
);
@JS('execSync')
external JSUint8Array? _execSync(
String command,
_ChildProcessOptions options,
);
Future<ProcessResult> exec(
String command,
List<String> args, {
String? workingDirectory,
Map<String, String>? environment,
bool includeParentEnvironment = true,
bool runInShell = false,
JSAny? stdin,
}) {
final completer = Completer<ProcessResult>();
late NodeChildProcess child;
child = _exec(
<String>[command, ...args].join(' '),
_ChildProcessOptions(
cwd: workingDirectory,
env: <String, String?>{
...?environment,
if (includeParentEnvironment) ...process.env,
}.jsify() as JSObject,
encoding: 'utf8',
shell: runInShell ? '/bin/sh' : null,
),
(JSError? error, String stdout, String stderr) {
if (error != null) {
return completer.completeError(
ProcessException(command, args, error.message),
);
}
completer.complete(
ProcessResult(
child.pid ?? -1,
child.exitCode!,
stdout,
stderr,
),
);
}.toJS,
);
return completer.future;
}
ProcessResult execSync(
String command,
List<String> args, {
String? workingDirectory,
Map<String, String>? environment,
bool includeParentEnvironment = true,
bool runInShell = false,
bool echoOutput = false,
}) {
try {
final stdout = _execSync(
<String>[command, ...args].join(' '),
_ChildProcessOptions(
cwd: workingDirectory,
env: <String, String?>{
...?environment,
if (includeParentEnvironment) ...process.env,
}.jsify() as JSObject,
shell: runInShell ? '/bin/sh' : null,
encoding: 'buffer',
stdio: <JSAny?>[
null,
if (echoOutput) 'inherit'.toJS else null,
if (echoOutput) 'inherit'.toJS else null,
].toJS,
),
);
return ProcessResult(
-1,
0,
stdout?.toDart ?? Uint8List(0),
Uint8List(0),
);
} on Object catch (e) {
final message = switch (e) {
final JSError e => e.message,
_ => e.toString(),
};
throw ProcessException(command, args, message);
}
}
NodeChildProcess spawn(
String command,
List<String> args, {
String? workingDirectory,
Map<String, String>? environment,
bool includeParentEnvironment = true,
bool runInShell = false,
ProcessStartMode mode = ProcessStartMode.normal,
JSAny? stdin,
}) {
final jsMode = switch (mode) {
ProcessStartMode.normal => 'pipe',
ProcessStartMode.detached => 'ignore',
ProcessStartMode.detachedWithStdio => 'pipe',
ProcessStartMode.inheritStdio => 'inherit',
_ => unreachable,
}
.toJS;
return _spawn(
command,
args.map((arg) => arg.toJS).toList().toJS,
_ChildProcessOptions(
cwd: workingDirectory,
env: {
...?environment,
if (includeParentEnvironment) ...process.env,
}.jsify() as JSObject,
detached: mode == ProcessStartMode.detached ||
mode == ProcessStartMode.detachedWithStdio,
stdio: <JSAny>[
// stdin
stdin ?? jsMode,
// stdout
jsMode,
// stderr
jsMode,
].toJS,
shell: runInShell ? '/bin/sh' : null,
),
);
}
}
@JS()
@anonymous
extension type _ChildProcessOptions._(JSObject it) {
external factory _ChildProcessOptions({
String? cwd,
JSObject? env,
JSAny? stdio,
bool? detached,
String? shell,
String? encoding,
});
}
@JS()
@anonymous
extension type NodeChildProcess._(JSObject it) implements EventEmitter {
Future<void> get onSpawn => once('spawn');
Future<JSObject> get onError => once('error');
Future<JSNumber> get onExit => once('exit');
Future<void> get onClose => once('close');
external bool kill([String signal]);
/// This is only set once the process has exited.
external int? get exitCode;
external int? get pid;
external NodeWriteableStream? get stdin;
external NodeReadableStream? get stdout;
external NodeReadableStream? get stderr;
}
@JS()
@anonymous
extension type NodeReadableStream._(JSObject it) implements EventEmitter {
Stream<List<int>> get stream {
final controller = StreamController<List<int>>(sync: true);
void onData(JSUint8Array chunk) {
if (controller.isClosed) return;
controller.add(chunk.toDart);
}
void onError(JSObject error) {
if (controller.isClosed) return;
controller
..addError(error)
..close();
}
void onDone([_]) {
if (controller.isClosed) return;
controller.close();
}
controller
..onListen = () {
addListener('data', onData.toJS);
unawaited(once('close').then(onDone));
unawaited(once<JSObject>('error').then(onError));
}
..onCancel = () => removeListener('data', onData.toJS);
return controller.stream;
}
}
@JS()
extension type EventEmitter._(JSObject it) implements JSObject {
external void addListener(String eventName, JSFunction? listener);
external void removeListener(String eventName, JSFunction? listener);
@JS('once')
external void _once(String eventName, JSFunction listener);
Future<T> once<T extends JSAny?>(String eventName) {
final completer = Completer<T>();
_once(
eventName,
// Callbacks may be called with 0-3 args. This will generate
// stubs for each of the argument counts, mimicking a variadic
// JS function.
//
// We are really only concerned with the first, though.
([JSAny? arg0, JSAny? arg1, JSAny? arg2]) {
completer.complete(arg0 as T);
}.toJS,
);
return completer.future;
}
}
@JS()
@anonymous
extension type NodeWriteableStream._(JSObject it) {
external void write(JSUint8Array chunk,
[String? encoding, JSFunction flushCallback]);
}