Simple replacement for shelljs.exec
.
It provides just two methods:
exec
: run a command while stdin/stdout/stderr is inherited from the main process (can be used also for interactive processes); just exit code is returnedeval
: run a command and wait for the result from stdout (cannot be used for interactive processes)
Via npm:
$ npm install --save cmdjs
var cmd = require('cmdjs');
// interactive process
cmd.exec('npm init');
// check exit code
if (cmd.exec('ls package.json') === 0) {
console.log('file exists');
}
// evaluate command and return lines in an array
var files = cmd.eval('ls -l .', { type: 'lines' });
console.log(files.length);
All methods run synchronously.
Available options:
ignoreErrors
: don't throw an error when exit code is non-zero (optional; defaultfalse
)
Returns an exit code.
Available options:
ignoreErrors
: don't throw an error when exit code is non-zero (optional; defaultfalse
)type
: eitherstring
,lines
,json
ornumber
(optional; defaultstring
)
Returns standard output parsed according to provided type
: string
, array
, plain object
or number
respectively.