-
-
Notifications
You must be signed in to change notification settings - Fork 0
Eugene Lazutkin edited this page Feb 20, 2026
·
9 revisions
$ is a set of helper functions to run OS-specific commands and/or use them in stream pipelines.
This function is similar to $$ but it uses different default spawn options related to streams and
different (simpler) return values.
import $ from 'dollar-shell';
// or:
// import {$} from 'dollar-shell';The TypeScript declaration:
interface DollarResult {
code: number | null;
signal: string | null;
killed: boolean;
}
interface DuplexPair<R = any> {
readable: ReadableStream<R>;
writable: WritableStream<R>;
}
interface DollarImpl<R = any> extends Dollar<Promise<DollarResult>> {
from: Dollar<ReadableStream<R>>;
to: Dollar<WritableStream<R>>;
through: Dollar<DuplexPair<R>>;
io: Dollar<DuplexPair<R>>;
}
declare const $: DollarImpl;Dollar is defined in $$.
The API is:
-
$— all streams are ignored. It returns a promise that resolves to an object with the following properties:-
code— the exit code of the process. Seespawn()'sexitCodeabove for more details. -
signal— the signal code of the process. Seespawn()'ssignalCodeabove for more details. -
killed— a boolean. It istruewhen the process has been killed andfalseotherwise. Seespawn()'skilledabove for more details.
-
-
$.from— setsstdouttopipeand returnsstdoutof the process. It can be used to process the output. Seespawn()'sstdoutabove for more details. -
$.to— setsstdintopipeand returnsstdinof the process. It can be used to pipe the input. Seespawn()'sstdinabove for more details. -
$.ioAKA$.through— setsstdinandstdouttopipeand returnsstdinandstdoutof the process as a{readable, writable}pair. It can be used to create a pipeline where an external process can be used as a transform step.
Just like $$ all $ functions support two signatures: they can be used as tag functions spawning
processes with default spawn options, or they can take an options object and produce a tag function.
When $ function is used to update defaults it still copies its own properties to the new object unchanged:
const $custom = $({stdout: 'inherit', stderr: 'inherit'});
const result = await $custom`ls -l .`;
typeof $custom.from === 'function';
typeof $custom.to === 'function';
typeof $custom.through === 'function';
typeof $custom.io === 'function';my-util --foo --bar ${'b a z'}A pipeline from ls -l to grep LICENSE to wc to tee output.txt:
import $ from 'dollar-shell';
import chain from 'stream-chain';
chain([
$.from`ls -l .`,
$.io`grep LICENSE`,
$.io`wc`,
$.to({stdout: 'inherit'})`tee output.txt`
]);A combined pipeline:
import $ from 'dollar-shell';
import chain from 'stream-chain';
import lines from 'stream-json/utils/lines.js';
chain([
$.from`ls -l .`,
new TextDecoderStream(),
lines(),
line => console.log(line)
]);Just run a command with default spawn options:
import $ from 'dollar-shell';
const result = await $`my-util --foo --bar ${'b a z'}`;
if (result.code || result.signal) {
console.error(
'Process exited with code',
result.code,
'or signal',
result.signal
);
}
if (result.killed) {
console.error('Process was killed');
}