@effect/platform/Command
docs
Command.make (command)
コマンドを構築
依存にCommandExecutorが発生する
CommandExecutor
BunContext.layerなどで解決できる
Command.make (command)で作ったコマンドをEffectとして実行する
e.g.
code:ts
import { Command } from "@effect/platform"
import { NodeContext, NodeRuntime } from "@effect/platform-node"
import { Effect } from "effect"
const command = Command.make("ls", "-al")
// Effect.Effect<void, PlatformError, CommandExecutor>
const program = Effect.gen(function* () {
// Runs the command returning the output as a string
const output = yield* Command.string(command)
console.log(output)
})
// Provide the necessary CommandExecutor
NodeRuntime.runMain(program.pipe(Effect.provide(NodeContext.layer)))
output formats
Command.string
stdout 全体を文字列で返す
encoding 指定あり
Command.lines
stdout を改行で分割した配列で返す
Command.stream
stdout を Stream<Uint8Array> として返す(巨大出力や逐次処理向き)
Command.streamLines
行単位の Stream(ログ追跡っぽい用途に向く)
Command.exitCode
https://effect.website/docs/platform/command/#exitcode
stdout を読まずに終了ステータスだけ欲しい時に使う
e.g. ls が成功すれば 0 が返る
Command.env
https://effect.website/docs/platform/command/#custom-environment-variables
環境変数をコマンドに付与できる
e.g. Command.env({ MY_CUSTOM_VAR: "..." })
Command.runInShell
シェル経由で実行されるため変数展開が効く
Command.feed
https://effect.website/docs/platform/command/#feeding-input-to-a-command
stdin にその文字列を流し込む
e.g.
code:ts
const command = Command.make("cat").pipe(Command.feed("Hello"))
Command.start
https://effect.website/docs/platform/command/#fetching-process-details
プロセスを起動して、実行中プロセスのハンドル(stdout/stderr/exitCode など)を返す
低レベル寄り API
Command.stdout("inherit")
https://effect.website/docs/platform/command/#streaming-stdout-to-processstdout
子プロセスの stdout を Node の process.stdout に直結する