Gathering detailed insights and metrics for tinyexec
Gathering detailed insights and metrics for tinyexec
Gathering detailed insights and metrics for tinyexec
Gathering detailed insights and metrics for tinyexec
npm install tinyexec
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
132 Stars
25 Commits
3 Forks
2 Watching
2 Branches
10 Contributors
Updated on 09 Nov 2024
TypeScript (97.56%)
JavaScript (2.44%)
Cumulative downloads
Total Downloads
Last day
13.2%
1,001,413
Compared to previous day
Last week
11.2%
4,960,448
Compared to previous week
Last month
44%
18,908,824
Compared to previous month
Last year
0%
37,074,834
Compared to previous year
A minimal package for executing commands
This package was created to provide a minimal way of interacting with child processes without having to manually deal with streams, piping, etc.
1$ npm i -S tinyexec
A process can be spawned and awaited like so:
1import {x} from 'tinyexec'; 2 3const result = await x('ls', ['-l']); 4 5// result.stdout - the stdout as a string 6// result.stderr - the stderr as a string 7// result.exitCode - the process exit code as a number
You may also iterate over the lines of output via an async loop:
1import {x} from 'tinyexec'; 2 3const proc = x('ls', ['-l']); 4 5for await (const line of proc) { 6 // line will be from stderr/stdout in the order you'd see it in a term 7}
Options can be passed to have finer control over spawning of the process:
1await x('ls', [], { 2 timeout: 1000 3});
The options object can have the following properties:
signal
- an AbortSignal
to allow aborting of the executiontimeout
- time in milliseconds at which the process will be forceably killedpersist
- if true
, the process will continue after the host exitsstdin
- another Result
can be used as the input to this processnodeOptions
- any valid options to node's underlying spawn
functionthrowOnError
- if true, non-zero exit codes will throw an errorYou can pipe a process to another via the pipe
method:
1const proc1 = x('ls', ['-l']); 2const proc2 = proc1.pipe('grep', ['.js']); 3const result = await proc2; 4 5console.log(result.stdout);
pipe
takes the same options as a regular execution. For example, you can
pass a timeout to the pipe call:
1proc1.pipe('grep', ['.js'], { 2 timeout: 2000 3});
You can kill the process via the kill
method:
1const proc = x('ls'); 2 3proc.kill(); 4 5// or with a signal 6proc.kill('SIGHUP');
By default, node's available binaries from node_modules
will be accessible
in your command.
For example, in a repo which has eslint
installed:
1await x('eslint', ['.']);
In this example, eslint
will come from the locally installed node_modules
.
An abort signal can be passed to a process in order to abort it at a later
time. This will result in the process being killed and aborted
being set
to true
.
1const aborter = new AbortController(); 2const proc = x('node', ['./foo.mjs'], { 3 signal: aborter.signal 4}); 5 6// elsewhere... 7aborter.abort(); 8 9await proc; 10 11proc.aborted; // true 12proc.killed; // true
Calling x(command[, args])
returns an awaitable Result
which has the
following API methods and properties available:
pipe(command[, args[, options]])
Pipes the current command to another. For example:
1x('ls', ['-l']) 2 .pipe('grep', ['js']);
The parameters are as follows:
command
- the command to execute (without any arguments)args
- an array of argumentsoptions
- options objectprocess
The underlying node ChildProcess
. For example:
1const proc = x('ls'); 2 3proc.process; // ChildProcess;
kill([signal])
Kills the current process with the specified signal. By default, this will
use the SIGTERM
signal.
For example:
1const proc = x('ls'); 2 3proc.kill();
pid
The current process ID. For example:
1const proc = x('ls'); 2 3proc.pid; // number
aborted
Whether the process has been aborted or not (via the signal
originally
passed in the options object).
For example:
1const proc = x('ls'); 2 3proc.aborted; // bool
killed
Whether the process has been killed or not (e.g. via kill()
or an abort
signal).
For example:
1const proc = x('ls'); 2 3proc.killed; // bool
exitCode
The exit code received when the process completed execution.
For example:
1const proc = x('ls'); 2 3proc.exitCode; // number (e.g. 1)
tinyexec
aims to provide a lightweight layer on top of Node's own
child_process
API.
Some clear benefits compared to other libraries are that tinyexec
will be much lighter, have a much
smaller footprint and will have a less abstract interface (less "magic"). It
will also have equal security and cross-platform support to popular
alternatives.
There are various features other libraries include which we are unlikely to ever implement, as they would prevent us from providing a lightweight layer.
For example, if you'd like write scripts rather than individual commands, and
prefer to use templating, we'd definitely recommend
zx. zx is a much higher level library which
does some of the same work tinyexec
does but behind a template string
interface.
Similarly, libraries like execa
will provide helpers for various things
like passing files as input to processes. We opt not to support features like
this since many of them are easy to do yourself (using Node's own APIs).
No vulnerabilities found.
No security vulnerabilities found.