| 26 | |
| 27 | /** @internal */ |
| 28 | export const makeExecutor = (start: _CommandExecutor.CommandExecutor["start"]): _CommandExecutor.CommandExecutor => { |
| 29 | const stream: _CommandExecutor.CommandExecutor["stream"] = (command) => |
| 30 | Stream.unwrapScoped(Effect.map(start(command), (process) => process.stdout)) |
| 31 | const streamLines: _CommandExecutor.CommandExecutor["streamLines"] = (command, encoding) => { |
| 32 | const decoder = new TextDecoder(encoding) |
| 33 | return Stream.splitLines( |
| 34 | Stream.mapChunks(stream(command), Chunk.map((bytes) => decoder.decode(bytes))) |
| 35 | ) |
| 36 | } |
| 37 | return { |
| 38 | [TypeId]: TypeId, |
| 39 | start, |
| 40 | exitCode: (command) => Effect.scoped(Effect.flatMap(start(command), (process) => process.exitCode)), |
| 41 | stream, |
| 42 | string: (command, encoding = "utf-8") => { |
| 43 | const decoder = new TextDecoder(encoding) |
| 44 | return pipe( |
| 45 | start(command), |
| 46 | Effect.flatMap((process) => Stream.run(process.stdout, collectUint8Array)), |
| 47 | Effect.map((bytes) => decoder.decode(bytes)), |
| 48 | Effect.scoped |
| 49 | ) |
| 50 | }, |
| 51 | lines: (command, encoding = "utf-8") => { |
| 52 | return pipe( |
| 53 | streamLines(command, encoding), |
| 54 | Stream.runCollect, |
| 55 | Effect.map(Chunk.toArray) |
| 56 | ) |
| 57 | }, |
| 58 | streamLines |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | const collectUint8Array: Sink.Sink<Uint8Array, Uint8Array> = Sink.foldLeftChunks( |
| 63 | new Uint8Array(), |