npm-run-script

emulate the `npm run-script` using stdio

Usage no npm install needed!

<script type="module">
  import npmRunScript from 'https://cdn.skypack.dev/npm-run-script';
</script>

README

Npm Run Script

Installation

npm install npm-run-script --save

Motivation

child_process doesn't know path of the local-installed npm commands.

npm install eslint
node -e "require('child_process').exec('eslint').stderr.pipe(process.stderr)"
# /bin/sh: eslint: command not found

by using execa, this problem can be solved. but, can't inherit the stdout.

npm install execa
node -e "require('execa').shell('eslint').then((result)=>console.log(result))"
# { stdout: 'eslint [options] file.js [file.js] [ ...

npm-run-script inherits the stdio, and run the command and returns the child_process.

node -e "require('npm-run-script')('eslint').once('exit',(code)=>console.log('exit in',code))"
# eslint [options] file.js [file.js] [dir]
#
# Basic configuration:
# ...
# exit in 0

API

npmRunScript(command, spawnOptions={stdio:'inherit'}) -> childProcess

run the command as shell comand in childProcess.

import npmRunScript from 'npm-run-script';

const command = 'eslint && exit 59';
const child = npmRunScript(command, { stdio: 'ignore' });// quiet...
child.once('error', (error) => {
  console.trace(error);
  process.exit(1);
});
child.once('exit', (exitCode) => {
  console.trace('exit in', exitCode);
  process.exit(exitCode);
});

Development

Requirement global

  • NodeJS v5.10.0
  • Npm v3.8.3
git clone https://github.com/59naga/npm-run-script
cd npm-run-script
npm install

npm test

License

MIT