npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tinyexec

v0.3.1

Published

A minimal library for executing processes in Node

Downloads

17,874,478

Readme

tinyexec 📟

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.

Installing

$ npm i -S tinyexec

Usage

A process can be spawned and awaited like so:

import {x} from 'tinyexec';

const result = await x('ls', ['-l']);

// result.stdout - the stdout as a string
// result.stderr - the stderr as a string
// result.exitCode - the process exit code as a number

You may also iterate over the lines of output via an async loop:

import {x} from 'tinyexec';

const proc = x('ls', ['-l']);

for await (const line of proc) {
  // line will be from stderr/stdout in the order you'd see it in a term
}

Options

Options can be passed to have finer control over spawning of the process:

await x('ls', [], {
  timeout: 1000
});

The options object can have the following properties:

  • signal - an AbortSignal to allow aborting of the execution
  • timeout - time in milliseconds at which the process will be forceably killed
  • persist - if true, the process will continue after the host exits
  • stdin - another Result can be used as the input to this process
  • nodeOptions - any valid options to node's underlying spawn function
  • throwOnError - if true, non-zero exit codes will throw an error

Piping to another process

You can pipe a process to another via the pipe method:

const proc1 = x('ls', ['-l']);
const proc2 = proc1.pipe('grep', ['.js']);
const result = await proc2;

console.log(result.stdout);

pipe takes the same options as a regular execution. For example, you can pass a timeout to the pipe call:

proc1.pipe('grep', ['.js'], {
  timeout: 2000
});

Killing a process

You can kill the process via the kill method:

const proc = x('ls');

proc.kill();

// or with a signal
proc.kill('SIGHUP');

Node modules/binaries

By default, node's available binaries from node_modules will be accessible in your command.

For example, in a repo which has eslint installed:

await x('eslint', ['.']);

In this example, eslint will come from the locally installed node_modules.

Using an abort signal

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.

const aborter = new AbortController();
const proc = x('node', ['./foo.mjs'], {
  signal: aborter.signal
});

// elsewhere...
aborter.abort();

await proc;

proc.aborted; // true
proc.killed; // true

API

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:

x('ls', ['-l'])
  .pipe('grep', ['js']);

The parameters are as follows:

  • command - the command to execute (without any arguments)
  • args - an array of arguments
  • options - options object

process

The underlying node ChildProcess. For example:

const proc = x('ls');

proc.process; // ChildProcess;

kill([signal])

Kills the current process with the specified signal. By default, this will use the SIGTERM signal.

For example:

const proc = x('ls');

proc.kill();

pid

The current process ID. For example:

const proc = x('ls');

proc.pid; // number

aborted

Whether the process has been aborted or not (via the signal originally passed in the options object).

For example:

const proc = x('ls');

proc.aborted; // bool

killed

Whether the process has been killed or not (e.g. via kill() or an abort signal).

For example:

const proc = x('ls');

proc.killed; // bool

exitCode

The exit code received when the process completed execution.

For example:

const proc = x('ls');

proc.exitCode; // number (e.g. 1)

Comparison with other libraries

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).