Skip to content

extra-child-process 1.1.1

Install from the command line:
Learn more about npm packages
$ npm install @nodef/extra-child-process@1.1.1
Install via package.json:
"@nodef/extra-child-process": "1.1.1"

About this version

Useful additions to inbuilt child_process module.
πŸ“¦ Node.js, πŸ“œ Files, πŸ“° Docs.

This package provides async versions of functions (in additon to the existing sync and callback-based functions), which are not included with the inbuilt child_process module. They are named as *Async can be used with Promise-based asynchronous programming using the await keyword. spawnAsync is a special case here, which returns a PromiseWithChild which is essentially a Promise with a .child property to allow ChildProcess to be directly accessed similar to spawn. In addition, callback-based functions, such as exec, also behave as async functions when a callback is not provided. Functions for locating path of executable(s) such as which and whichAll are also included. Design was based on local ideas and literature survey.

Stability: Experimental.


const cp = require('extra-child-process');


// 1. List files in current directory.
async function example1() {
  var {stdout, stderr} = await cp.exec('ls -a');
  var {stdout, stderr} = await cp.execFile('ls', ['-a']);
  cp.exec('ls -a', (err, stdout, stderr) => 0);
  cp.execFile('ls', ['-a'], (err, stdout, stderr) => 0);
  // β†’ .
  // β†’ ..
  // β†’ .build
  // β†’ .git
  // β†’ .github
  // β†’ ...
}
example1();


// 2. List files in 'src' directory.
async function example2() {
  var {stdout, stderr} = await cp.exec('ls -a', {cwd: 'src'});
  var {stdout, stderr} = await cp.execFile('ls', ['-a'], {cwd: 'src'});
  cp.exec('ls -a', {cwd: 'src'}, (err, stdout, stderr) => 0);
  cp.execFile('ls', ['-a'], {cwd: 'src'}, (err, stdout, stderr) => 0);
  // β†’ .
  // β†’ ..
  // β†’ index.ts
}
example2();


// 3. Locate path of node executable.
async function example3() {
  var paths = process.env.PATH.split(';');
  var exec  = await cp.which('node');
  var exec  = await cp.which('node', {paths});
  // β†’ 'D:\\Program Files\\nodejs\\node.exe'
}
example3();


// 4. Locate path of n*e executables.
async function example4() {
  var paths = process.env.PATH.split(';');
  var execs = await cp.whichAll(/^n.*?e$/);
  var execs = await cp.whichAll(/^n.*?e$/, {paths});
  // β†’ [
  // β†’   'D:\\Program Files\\Git\\usr\\bin\\nice.exe',
  // β†’   'C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v11.2\\bin\\nvprune.exe',
  // β†’   'D:\\Program Files\\nodejs\\node.exe'
  // β†’ ]
}
example4();


Index

Property Description
exec Execute a command within a shell, buffering any output.
execFile Execute an executable without a shell by default, buffering any output.
fork This method is a special case of spawn used specifically to spawn new Node.js processes.
spawn This method spawns a new process using the given command and args.
spawnAsync Spawn new process using given command and arguments.
which Locate path of executable for given command.
whichAll Locate paths of all matching executables for given command.


References



Details


Assets

  • extra-child-process-1.1.1-npm.tgz

Download activity

  • Total downloads 0
  • Last 30 days 0
  • Last week 0
  • Today 0