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

node-command-line

v2.0.2

Published

Simple command line interface to execute command from node environment with promise and avoid shell escaping

Downloads

3,308

Readme

node-command-line

Simple node.js commandline or terminal interface to execute cli commands from node environment with/without promise

using node-command-line you can run commands synchronously/asynchronously and get the output as a promise.

npm info :

node-command-line npm version total npm downloads for node-command-line monthly npm downloads for node-command-line npm licence for node-command-line

GitHub Info:

node-command-line GitHub Release GitHub license node-command-line license open issues for node-command-line on GitHub

Install the package

$ npm install --save node-command-line

Method

| method | argument | functionality | |--------|----------|---------------| | run | command, true/false | run command synchronously/asynchronously based on using await and pass second parameter true or false to print from library or not. Default vaue is true.|

**Note:

I've added a basic sanitization step that removes characters commonly associated with shell command injection attacks. This helps prevent unwanted characters from being executed within the shell command.

Examples

Inject the dependencies

var cmd     = require('node-command-line')

Ex1: Execute the single command without wait

function runSingleCommandWithoutWait() {
  cmd.run('node --version');
  console.log('Executed your command :)');
}

In this example run the command node --version that will show the node version and also print Executed your command :). node version may shown after print Executed your command :) because of second command do not wait for executing the first command.

I've added a basic sanitization step that removes characters commonly associated with shell command injection attacks. This helps prevent unwanted characters from being executed within the shell command.

Output in console like:

 Executed your command :)
 
 v16.15.1
 

Ex2: Execute command and pass second parameter true/false to print from library or not

function runSingleCommandWithoutWait() {
  cmd.run('node --version', false); // Will not print output from library
  console.log('Executed your command :)');
}

In this example run the command node --version that will not sow the node version but print Executed your command :). node version won't show after print Executed your command :) because the second parameter passed false.

Output in console like:

 Executed your command :)
 

Ex3: Execute the single command with wait (using promise)

async function runSingleCommandWithWait() {
  await cmd.run('node --version');
  console.log('Executed your command :)');
}

In this example run the command node --version that will show the node version and also print Executed your command :). node version will show before print Executed your command :) because of second command will execute after executing the first command.

Output in console like:

 v16.15.1
 
 Executed your command :)
 

Ex4: Execute the multiple command without wait

async function runMultipleCommandWithoutWait() {
  var commands = ["node --version", "npm --version"];
  for (var i = 0; i < commands.length; i++) {
    cmd.run(commands[i]);
  }
  console.log('Executed your command :)');
}

In this example run the command node --version and npm --version that will show the node version and npm version also print Executed your command :). node and npm version may shown after print Executed your command :) because of `console.log`` do not wait for executing the first two command.

Output in console like:

 Executed your command :)
 
 v16.15.1
 
 9.8.1
 

Ex5: Execute the multiple command without wait

async function runMultipleCommandWithWait() {
  var commands = ["node --version", "npm --version"];
  for (var i = 0; i < commands.length; i++) {
    await cmd.run(commands[i]);
  }
  console.log('Executed your command :)');
}

In this example run the command node --version and npm --version that will show the node version and npm version also print Executed your command :). node and npm version will show before print Executed your command :) because of `console.log`` will be waiting for executing the first two command.

Output in console like:

 
 v16.15.1
 
 9.8.1
 
 Executed your command :)
 

Ex6: Execute the single command with async wait and get response

async function runSingleCommandWithWaitAndGetResponse() {
  let response = await cmd.run('node --version');
  if(response.success) {
    console.log('Response received===', response.message);
    // do something
    // if success get stdout info in message. like response.message
  } else {
    // do something
    // if not success get error message and stdErr info as error and stdErr. 
    //like response.error and response.stdErr
  }
 console.log('Executed your command :)');
}

Output in console like:

 
 Executed your command :)