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

cmd-promise

v1.2.0

Published

Node command line interface with a simple Promise based API.

Downloads

8

Readme

CMD Promise

Node command line interface with a simple Promise based API.

JavaScript Style Guide Version Downloads

Inspired by node-cmd.

Features

  • Simple Promise based API.
  • Single or multiple commands in one call.
  • Passes the exec() node options through.
  • Returns an object containing both stdout and stderr.
  • Optionally return the child process instead of the output.
  • Zero dependencies.

Requirments

Uses native node promises (including Promise.all with generic iterables) so requires at least node version 4.0.0. See http://node.green/.

Install

npm install cmd-promise

Examples

Single command

const cmd = require('cmd-promise')

cmd(`node -v`).then(out => {
  console.log('out =', out)
}).catch(err => {
  console.log('err =', err)
})

// out = { stdout: 'v4.2.2\r\n', stderr: '' }

Multiple commands

const cmd = require('cmd-promise')

const commands = `
  node -v
  npm -v
`

cmd(commands).then(out => {
  console.log('out =', out)
}).catch(err => {
  console.log('err =', err)
})

// out = [ { stdout: 'v4.2.2\r\n', stderr: '' }, { stdout: '4.4.1\n', stderr: '' } ]
// out[0].stdout = v4.2.2

More involved example

const semver = require('semver') // https://github.com/npm/node-semver
const cmd = require('cmd-promise')

const commands = `
  npm view npm version
  npm -v
`

cmd(commands).then(out => {
  return {
    npm: out[0].stdout.replace(/\n/g, ''),
    me: out[1].stdout.replace(/\n/g, '')
  }
}).then(versions => {
  if (semver.lt(versions.me, versions.npm)) {
    console.log(`My npm version is out of date (npm install npm@latest -g).`)
  } else {
    console.log(`My npm version is up to date! :-)`)
  }
}).catch(err => {
  console.log('err =', err)
})

Return the child process instead

const cmd = require('../cmd-promise')

const options = { returnProcess: true }

cmd(`node -v`, options).then(childProcess => {
  console.log('pid =', childProcess.pid)
  childProcess.stdout.on('data', stdout => {
    console.log('stdout =', stdout)
  })
  childProcess.stderr.on('data', stderr => {
    console.log('stderr =', stderr)
  })
}).catch(err => {
  console.log('err =', err)
})

Pass exec() options

Pass child_process.exec() options as defined in the node docs.

const cmd = require('../cmd-promise')

const execOptions = { timeout: 1000 }

cmd(`node -v`, {}, execOptions).then(out => {
  console.log('out =', out)
}).catch(err => {
  console.log('err =', err)
})

API

cmd(commands [,options] [,execOptions]) -> Promise

  • commands (string) Single or multiple line string of commands to execute.
  • options (object)
    • returnProcess (boolean) Return the child process instead of waiting on and returning the outcome. Default is false.
  • execOptions (object) Options as passed to the exec() method of the child_process node module.

Returns a Promise.

For single commands the promises return value is an object containing stdout and stderr properties. If options.returnProcess is set to true the return value is the child process instead.

const cmd = require('cmd-promise')

cmd(`node -v`).then(out => {
  console.log('out.stdout =', out.stdout) // v4.2.2
  console.log('out.stderr =', out.stderr)
})

For multiple line command calls the promises return value is an array of object's containing stdout and stderr properties. If options.returnProcess is set to true the return value is an array of child processes instead.

const cmd = require('cmd-promise')

const commands = `
  node -v
  npm -v
`

cmd(commands).then(out => {
  console.log('out[0] =', out[0]) // result from 'node -v'
  console.log('out[1] =', out[1]) // result from 'npm -v'
})