(
command: ChildProcess.Command,
options?: RunStreamOptions,
)
| 212 | }) |
| 213 | |
| 214 | const runStream = ( |
| 215 | command: ChildProcess.Command, |
| 216 | options?: RunStreamOptions, |
| 217 | ): Stream.Stream<string, AppProcessError> => { |
| 218 | const description = describeCommand(command) |
| 219 | const okExitCodes = options?.okExitCodes |
| 220 | const built: Stream.Stream<string, AppProcessError | PlatformError> = Stream.unwrap( |
| 221 | Effect.gen(function* () { |
| 222 | const handle = yield* spawner.spawn(command) |
| 223 | const stderrFiber = yield* Effect.forkScoped( |
| 224 | collectStream(handle.stderr, options?.maxErrorBytes).pipe(Effect.map((x) => x.buffer.toString("utf8"))), |
| 225 | ) |
| 226 | const source = options?.includeStderr === true ? handle.all : handle.stdout |
| 227 | const lines = source.pipe( |
| 228 | Stream.decodeText, |
| 229 | Stream.splitLines, |
| 230 | Stream.filter((line) => line.length > 0), |
| 231 | ) |
| 232 | const tail = Stream.unwrap( |
| 233 | Effect.gen(function* () { |
| 234 | const code = yield* handle.exitCode |
| 235 | if (okExitCodes && okExitCodes.length > 0 && !okExitCodes.includes(code)) { |
| 236 | const stderr = yield* Fiber.join(stderrFiber) |
| 237 | return Stream.fail(new AppProcessError({ command: description, exitCode: code, stderr })) |
| 238 | } |
| 239 | return Stream.empty |
| 240 | }), |
| 241 | ) |
| 242 | return Stream.concat(lines, tail) as Stream.Stream<string, AppProcessError | PlatformError> |
| 243 | }), |
| 244 | ) |
| 245 | const mapped = built.pipe( |
| 246 | Stream.catch((cause): Stream.Stream<string, AppProcessError> => Stream.fail(wrapError(description, cause))), |
| 247 | ) |
| 248 | if (!options?.signal) return mapped |
| 249 | const signal = options.signal |
| 250 | return mapped.pipe( |
| 251 | Stream.interruptWhen(waitForAbort(signal).pipe(Effect.mapError((cause) => wrapError(description, cause)))), |
| 252 | ) |
| 253 | } |
| 254 | |
| 255 | return Service.of({ ...spawner, run, runStream }) |
| 256 | }), |
nothing calls this directly
no test coverage detected