-
-
Notifications
You must be signed in to change notification settings - Fork 0
spawn
Eugene Lazutkin edited this page Sep 30, 2024
·
2 revisions
Spawn a process with advanced ways to configure and control it.
import {spawn} from 'dollar-shell';
The TypeScript declaration:
type SpawnStreamState = 'pipe' | 'ignore' | 'inherit' | 'piped' | null;
interface SpawnOptions {
cwd?: string;
env?: {[key: string]: string | undefined};
stdin?: SpawnStreamState;
stdout?: SpawnStreamState;
stderr?: SpawnStreamState;
}
interface Subprocess<R = string> {
readonly command: string[];
readonly options: SpawnOptions | undefined;
readonly exited: Promise<number>;
readonly finished: boolean;
readonly killed: boolean;
readonly exitCode: number | null;
readonly signalCode: string | null;
readonly stdin: WritableStream<R> | null;
readonly stdout: ReadableStream<R> | null;
readonly stderr: ReadableStream<R> | null;
readonly asDuplex: DuplexPair<R>;
kill(): void;
}
declare function spawn(command: string[], options?: SpawnOptions): Subprocess;
DuplexPair
is defined in $
.
Arguments:
-
command
— an array of strings. The first element is the command to run. The rest are its arguments. -
options
— an optional object with options to configure the process:-
cwd
— the optional current working directory as a string. Defaults toprocess.cwd()
. -
env
— the optional environment variables as an object (key-value pairs). Defaults toprocess.env
. -
stdin
— the optional source stream state. Defaults tonull
. -
stdout
— the optional destination stream state. Defaults tonull
. -
stderr
— the optional destination stream state. Defaults tonull
.
-
stdin
, stdout
and stderr
can be a string (one of 'inherit'
, 'ignore'
, 'pipe'
or 'piped'
)
or null
. The latter is equivalent to 'ignore'
. 'piped'
is an alias of 'pipe'
:
-
'inherit'
— inherit streams from the parent process. For output steams (stdout
andstderr
), it means that they will be piped to the same target, e.g., the console. -
'ignore'
— the stream is ignored. -
'pipe'
— the stream is available for reading or writing.
Returns a sub-process object with the following properties:
-
command
— the command that was run as an array of strings. -
options
— the options that were passed tospawn()
. -
exited
— a promise that resolves to the exit code of the process. It is used to wait for the process to exit. -
finished
— a boolean. It istrue
when the process has finished andfalse
otherwise. -
killed
— a boolean. It istrue
when the process has been killed andfalse
otherwise. -
exitCode
— the exit code of the process as a number. It isnull
if the process hasn't exited yet. -
signalCode
— the signal code of the process as a string. It isnull
if the process hasn't exited yet. -
stdin
— the source stream of the process ifoptions.stdin
was'pipe'
. It isnull
otherwise. -
stdout
— the destination stream of the process ifoptions.stdout
was'pipe'
. It isnull
otherwise. -
stderr
— the destination stream of the process ifoptions.stderr
was'pipe'
. It isnull
otherwise. -
asDuplex
— returns a duplex stream that can be used to pipe the input and output. It is defined as{readable: this.stdout, writable: this.stdin}
. -
kill()
— kills the process.killed
will betrue
as soon as the process has been killed. It can be used to pipe the input and output. Seespawn()
'sstdin
andstdout
above for more details.
Important: all streams are exposed as web streams.
import {spawn} from 'dollar-shell';
const sp = spawn(['sleep', '5'])
await new Promise(resolve => setTimeout(resolve, 1000));
sp.kill();
await sp.exited;
sp.finished === true;
sp.killed === true;