1/**
2 * Shared Node.js implementation of the Effect `Stdio` service.
3 *
4 * `NodeStdio` provides {@link Stdio.Stdio} from the current Node process. The
5 * exported {@link layer} reads command-line arguments from `process.argv`,
6 * consumes input from `process.stdin`, and writes normal and error output to
7 * `process.stdout` and `process.stderr`. Standard input remains open, and
8 * standard output and error output are not ended unless requested.
9 *
10 * @since 4.0.0
11 */
12import * as Effect from "effect/Effect"
13import * as Layer from "effect/Layer"
14import { systemError } from "effect/PlatformError"
15import * as Stdio from "effect/Stdio"
16import { fromWritable } from "./NodeSink.ts"
17import { fromReadable } from "./NodeStream.ts"
18
19/**
20 * Provides `Stdio` from `process.argv`, `process.stdin`, `process.stdout`,
21 * and `process.stderr`; stdin remains open and stdout/stderr are not ended by
22 * default.
23 *
24 * @category layers
25 * @since 4.0.0
26 */
27export const layer: Layer.Layer<Stdio.Stdio> = Layer.succeed(
28 Stdio.Stdio,
29 Stdio.make({
30 args: Effect.sync(() => process.argv.slice(2)),
31 stdinIsTerminal: Effect.sync(() => process.stdin.isTTY === true),
32 stdoutIsTerminal: Effect.sync(() => process.stdout.isTTY === true),
33 stdout: (options) =>
34 fromWritable({
35 evaluate: () => process.stdout,
36 onError: (cause) =>
37 systemError({
38 module: "Stdio",
39 method: "stdout",
40 _tag: "Unknown",
41 cause
42 }),
43 endOnDone: options?.endOnDone ?? false
44 }),
45 stderr: (options) =>
46 fromWritable({
47 evaluate: () => process.stderr,
48 onError: (cause) =>
49 systemError({
50 module: "Stdio",
51 method: "stderr",
52 _tag: "Unknown",
53 cause
54 }),
55 endOnDone: options?.endOnDone ?? false
56 }),
57 stdin: fromReadable({
58 evaluate: () => process.stdin,
59 onError: (cause) =>
60 systemError({
61 module: "Stdio",
62 method: "stdin",
63 _tag: "Unknown",
64 cause
65 }),
66 closeOnDone: false
67 })
68 })
69)