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

condexec

v0.0.1

Published

Conditionally shell out an exec

Downloads

12

Readme

condexec

Conditionally shell out an exec

Installation

npm install condexec

Usage

basic

var condexec = require('condexec');

var opts = {
  cwd: '/',
  onlyif: 'test -f /etc/passwd',
  notif: 'test -d /this/doesnt/exist',
  exec: 'echo "hello $USER you are in $PWD"'
};

condexec(opts, function(err) {
  console.log('done');
}

yields

hello dave you are in /
done

any and all output from the commands executed will be piped directly to the proper streams (stdout/stderr).

shell parsing

When the exec, onlyif, or notif attributes are given as strings, they are parsed by the shell before they are ran; they are executed as ['/bin/sh', '-c', '{string}']. Alternatively, you can pass an array of arguments instead of a string, which will allow you to bypass shell-parsing/word-splitting logic.

var file = '/etc/passwd';
var msg = 'this message has double"quotes and single\'quotes';
condexec({
  exec: ['echo', msg],
  onlyif: ['test', '-f', file]
});

yields

this message has double"quotes and single'quotes

Notice that you didn't need to worry about escaping the quotes before you passed them to exec.

error handling

When the callback is fired, it will contain what specifically produced an error.

var opts = {
  onlyif: 'test -d /etc/passwd',
  notif: 'test -d /aoehsutoehnaueohs/fake/fake/fake',
  exec: 'echo "hello $USER you are in $PWD"'
};

condexec(opts, function(err) {
  console.error(err.message);
});

yields

[notif] command `test -f /etc/passwd` returned with code 0

It tells you what command failed, in what clause, and with what code. Notice that, because the command failed, the exec line was never called.

debug

Pass in a debug attribute to see exactly what is happening

var opts = {
  debug: true,
  cwd: '/',
  onlyif: 'test -f /etc/passwd',
  notif: 'test -d /aoehsutoehnaueohs/fake/fake/fake',
  exec: 'echo "hello $USER you are in $PWD"'
};

condexec(opts, function(err) {
  console.log('success!');
});

yields

[onlyif] about to execute `test -f /etc/passwd`, expects 0
[onlyif] command `test -f /etc/passwd` returned with code 0
[notif] about to execute `test -d /aoehsutoehnaueohs/fake/fake/fake`, expects != 0
[notif] command `test -d /aoehsutoehnaueohs/fake/fake/fake` returned with code 1
[exec] about to execute `echo "hello $USER you are in $PWD"`, expects 0
hello dave you are in /
[exec] command `echo "hello $USER you are in $PWD"` returned with code 0
success!

Function

function condexec(options, callback(err))

options

  • exec: a string or array of commands to execute given the 2 optional conditions are satisfied
  • notif: an optional condition to meet before the exec command is run, expects this to return non-0
  • onlyif: an optional condition to meet before the exec command is run, expects this to return 0
  • debug: turn on debug output

This object is then passed into child_process.spawn, so pass in any options that you would like, such as cwd, env, etc.

callback

The callback runs when the conditional exec has finished, either successfully or with an error. The err object will be null if it was successful

License

MIT