The @effect/platform/Command module provides a way to create and run commands with the specified process name and an optional list of arguments.
Creating Commands
The Command.make function generates a command object, which includes details such as the process name, arguments, and environment.
Example (Defining a Command for Directory Listing)
This command object does not execute until run by an executor.
Running Commands
You need a CommandExecutor to run the command, which can capture output in various formats such as strings, lines, or streams.
Example (Running a Command and Printing Output)
Output Formats
You can choose different methods to handle command output:
Method
Description
string
Runs the command returning the output as a string (with the specified encoding)
lines
Runs the command returning the output as an array of lines (with the specified encoding)
stream
Runs the command returning the output as a stream of Uint8Array chunks
streamLines
Runs the command returning the output as a stream of lines (with the specified encoding)
exitCode
If you only need the exit code of a command, use Command.exitCode.
Example (Getting the Exit Code)
Custom Environment Variables
You can customize environment variables in a command by using Command.env. This is useful when you need specific variables for the command’s execution.
Example (Setting Environment Variables)
In this example, the command runs in a shell to ensure environment variables are correctly processed.
Feeding Input to a Command
You can send input directly to a command’s standard input using the Command.feed function.
Example (Sending Input to a Command’s Standard Input)
Fetching Process Details
You can access details about a running process, such as exitCode, stdout, and stderr.
Example (Accessing Exit Code and Streams from a Running Process)
Streaming stdout to process.stdout
To stream a command’s stdout directly to process.stdout, you can use the following approach:
Example (Streaming Command Output Directly to Standard Output)