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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@node-cli-toolkit/exec-bash-command

v0.3.2

Published

This is a utility that should run any bash command or CLI. It supports sending inputs to the command, checks for exit codes, and allows to check the `stdout` and `stderr` easily with a promise interface.

Readme

@node-cli-toolkit/exec-bash-command

This is a utility that should run any bash command or CLI. It supports sending inputs to the command, checks for exit codes, and allows to check the stdout and stderr easily with a promise interface.

It also allows to run node scripts easily (that are meant to be run as CLIs)

Supported Features:

  • Run a bash command with inputs
  • Check for exit codes
  • Check stdout/stderr output
  • Run a inline node script
  • Run an inline node script with a different node command (ex: ts-node)
  • Different timeouts for inputs
  • Change the default timeoutBetweenInputs
  • Run in a different cwd
  • Debugger support (output the contents of the script that's running to console)

To see examples of each please see tests under __tests__ or below.

Usage

Run a bash command

import execBashCommand, {
  IExecBashCommandReturn
} from "@node-cli-toolkit/exec-bash-command";

const error = jest.fn();
const output = jest.fn();
const PROJECT_ROOT = `${__dirname}/..`;

const DEFAULT_EXEC_BASH_COMMAND_OPTS = {
  errorCB: error,
  outputCB: output,
  cwd: PROJECT_ROOT
};

const { code }: IExecBashCommandReturn = await execBashCommand({
  ...DEFAULT_EXEC_BASH_COMMAND_OPTS,
  bashCommand: `echo "hello"`
});
expect(error.mock.calls.length).toBe(0);
expect(code).toBe(0);
expect(output).toBeCalledWith(expect.stringContaining("hello"));

Run a bash command with inputs

import execBashCommand, {
  IExecBashCommandReturn
} from "@node-cli-toolkit/exec-bash-command";
import { SPACE, DOWN, ENTER } from "@node-cli-toolkit/send-input-to-cli";

const error = jest.fn();
const output = jest.fn();
const PROJECT_ROOT = `${__dirname}/..`;

const DEFAULT_EXEC_BASH_COMMAND_OPTS = {
  errorCB: error,
  outputCB: output,
  cwd: PROJECT_ROOT
};

const STD_CLI_INPUTS = [
  // Check "Option 1"
  SPACE,

  // Move to "Option 2"
  DOWN,

  // Move to "Option 3"
  DOWN,

  // Check "Option 3"
  SPACE,

  // Next Question
  ENTER,

  // Type answer to "What's your name"
  "Anatoliy Zaslavskiy",

  // Submit answer to question
  ENTER
];

const { code }: IExecBashCommandReturn = await execBashCommand({
  ...DEFAULT_EXEC_BASH_COMMAND_OPTS,
  bashCommand: `ts-node ./mockCLIs/standard.ts`,
  inputs: STD_CLI_INPUTS
});

expect(error.mock.calls.length).toBe(0);

expect(code).toBe(0);

expect(output).toBeCalledWith(
  expect.stringContaining("Which option do you want to choose?")
);

expect(output).toBeCalledWith(expect.stringContaining("◯ Option 1"));

expect(output).toBeCalledWith(expect.stringContaining("◯ Option 3"));

expect(output).toBeCalledWith(expect.stringContaining("Option 1 Chosen"));

expect(output).not.toBeCalledWith(expect.stringContaining("Option 2 Chosen"));

expect(output).toBeCalledWith(expect.stringContaining("Option 3 Chosen"));

expect(output).toBeCalledWith(
  expect.stringContaining("What's your full name?")
);

expect(output).toBeCalledWith(
  expect.stringContaining('Your name is "Anatoliy Zaslavskiy')
);

Run a bash command with different exit code

import execBashCommand, {
  IExecBashCommandReturn
} from "@node-cli-toolkit/exec-bash-command";

const error = jest.fn();
const output = jest.fn();
const PROJECT_ROOT = `${__dirname}/..`;

const DEFAULT_EXEC_BASH_COMMAND_OPTS = {
  errorCB: error,
  outputCB: output,
  cwd: PROJECT_ROOT
};

try {
  await execBashCommand({
    ...DEFAULT_EXEC_BASH_COMMAND_OPTS,
    bashCommand: `echo "hello" && >&2 echo "Something bad happened 1" && exit 1`
  });
} catch (e) {
  expect(output).toBeCalledWith(expect.stringContaining("hello"));

  expect(error).toBeCalledWith(
    expect.stringContaining("Something bad happened")
  );

  expect(e.code).toBe(1);
}

Run a bash command that outputs to stderr

import execBashCommand, {
  IExecBashCommandReturn
} from "@node-cli-toolkit/exec-bash-command";

const error = jest.fn();
const output = jest.fn();
const PROJECT_ROOT = `${__dirname}/..`;

const DEFAULT_EXEC_BASH_COMMAND_OPTS = {
  errorCB: error,
  outputCB: output,
  cwd: PROJECT_ROOT
};

const { code }: IExecBashCommandReturn = await execBashCommand({
  ...DEFAULT_EXEC_BASH_COMMAND_OPTS,
  bashCommand: `echo "hello" && >&2 echo "Something bad happened 2"`
});

expect(output).toBeCalledWith(expect.stringContaining("hello"));

expect(error).toBeCalledWith(expect.stringContaining("Something bad happened"));

expect(code).toBe(0);