nodeproc

Simpler handling of spawning processes

Usage no npm install needed!

<script type="module">
  import nodeproc from 'https://cdn.skypack.dev/nodeproc';
</script>

README

nodeproc

Simpler way to handle spawning processes in NodeJs

Build Status Dependency Status

var Nodeproc = require('nodeproc');

var processes = new Nodeproc();

// Most basic use
processes.spawn('npm ls').then(function(results) {
  console.log('Finished');
  console.log('Process ID: ', results.procId);
  console.log('Exit code: ', results.exitCode);
});

// Slightly more complicated
processes.spawn({
  command: 'npm',
  args: ['ls'],
  cwd: '/home/joe' // Specify where the command should be run from
}).then(function() {
  console.log('Finished');
});

// Control stdout and/or stderr from the process.
// These can be anything that supports the write(data, encoding) method.
// These default to process.stdout and process.stderr respectively
var captureStdout = {
  write: function(data, encoding) {
    console.log('Got data: ', data.toString(encoding));
  }
};
processes.spawn({
  command: 'npm',
  args: 'ls' // Can either be an array or a space-delimited string
  stdout: captureStdout
}).then(funnction() {
  console.log('Finished');
});

// Catch errors using a Promise catch function
processes.spawn({
  command: 'npm',
  args: ['doesnotexit'],
  ignoreExitCode: true
}).catch(function(err) {
  console.log('Got error! ', err);
  console.log('Process ID: ', err.procId);
  console.log('Exit code: ', err.exitCode);
  console.log('Stderr: ', err.stderr);
});

// Can ignore error exit status codes, and always resolve the process
processes.spawn({
  command: 'npm',
  args: ['doesnotexit'],
  ignoreExitStatusCode: true
}).then(function() {
  console.log('Finished');
});

// Can kill processes prematurely
processes.spawn('npm ls').kill();

// Set invalidateOnError=true when creating the Nodeproc instance, and when one process errors,
//  it will automatically kill all other running processes, and will prevent others from being created.
var processes2 = new Nodeproc({invalidateOnError = true});

See the tests for more examples