runs npm commands via npm's CLI or JS API, at your choice
to get all capabilities, and choose the API implementation at runtime:
npm i npm-runner
to only use the CLI API:
npm i npm-runner --no-optional
initializes an npm-runner implementation and returns a runner function to call with npm commands.
globalOptions
{Options}
(optional) global options that
will be applied by default on every invocation of the runner.
Returns: {Function}
a run()
function that is bound to
the passed options.
invokes an npm command. available only after initialization.
command
{String}
an npm command to run, e.g. install -D
.
localOptions
{Options}
(optional) local options that
will be applied to the current invocation only, and override the global
options passed on init()
.
callback
{Function}
a callback that will be called when
the npm command execution is finished. it receives two arguments: err
and output
. err
is any raised error, and output
is the command
output, broken down to an array of output lines.
Type: Object<*>
instructions to apply globally (when passed to init()
), or for
a specific npm invocation (when passed to run()
).
Type: String
Default value: 'cli'
Mandatory: no
how commands will be executed: 'javascript'
means using the npm
node
module javascript API (requires npm
as a local npm dependency), while
'cli'
will rely on your global npm installation and send commands to
the terminal.
Type: Boolean
Default value: false
Mandatory: no
whether to pipe the npm command output to stdout (in any case, the
output will be sent to the run()
callback).
Type: String|Path
Default value: ''
Mandatory: no
the directory to execute the npm command from (at the moment it's only used by the CLI API).
Type: Array<String>
Default value: []
Mandatory: no
don't fail the execution when any of these terms are found in error messages of the npm command output.
Type: Array<String>
Default value: []
Mandatory: no
npm flags that will be appended to every command. e.g. put 'parseable'
here if you intend to dissect the output of every command, or 'json'
if you always want to use the command output as an object.
const npm = require('npm-runner').init({
tee: true
});
npm('update', done);
const npm = require('npm-runner').init();
npm('link npm-runner', {
cwd: path.resolve(__dirname)
}, done);
const npm = require('npm-runner').init({
api: 'javascript'
});
npm('update', done);
npm('update', {
api: 'cli'
}, done);
here, the done
callback will be called with no errors, even if some
extraneous
errors slipped to the stderr.
const npm = require('npm-runner').init({
ignoreErrors: [
'extraneous'
]
});
npm('list', done);
const npm = require('npm-runner').init();
const packageName = 'npm-runner';
npm(`list ${packageName} --parseable --long --silent`, (err, parseableOutput) => {
let output = parseableOutput.shift();
if (output) {
let path = output.split(':').shift();
console.log(`${packageName}'s path is "${path}"`);
}
});