odaseva-cli
Version:
Odaseva CLI
79 lines (64 loc) • 2.02 kB
JavaScript
/*
* Odaseva_cli Node.js wrapper
* For now, it only:
* - ensures Odaseva CLI executable is reachable and runs
* - runs the CLI with the provided parameters
*/
const fs = require('fs');
const shell = require('shelljs');
const path = require('path');
var util = require('util')
const EXE_NAME = 'odaseva';
function debug(s) {
if (process.env.ODASEVA_DEBUG) {
console.log('[odaseva-cli] ' + s);
}
}
function debug_obj(s, obj) {
if (process.env.ODASEVA_DEBUG) {
return debug(s + ': '+ util.inspect(obj, {showHidden: false, depth: null}))
}
}
/*
*/
var runFromCmdLine = function () {
var args = process.argv.slice(2);
var cmdString = args.map( function(arg){
return "\"" + arg.replace(/'/g, "'\\''") + "\"";
}).join(' ');
debug_obj('runFromCmdLine', process.argv);
debug('extracted command string: '+ cmdString);
return run(cmdString);
}
/*
*/
var run = function (cliArgs, bSilent = false) {
debug("cliArgs[" + cliArgs + ']');
var exePath = path.join( __dirname, '..', 'bin');
var fullPath = path.join(exePath, EXE_NAME);
debug("-> fullPath:" + fullPath);
// should check file is executable...
if (!fs.existsSync(fullPath)) {
var err = fullPath + ' is not a valid file. ' + EXE_NAME + ' cannot be found.';
debug(err)
return new Error(err)
}
var cmdLine = fullPath + ' ' + cliArgs;
debug('cmdLine['+cmdLine+']');
debug('silent['+bSilent+'] process.cwd()['+process.cwd()+']');
if (process.env.ODASEVA_NOEXEC)
{
debug('Command not executed (ODASEVA_NOEXEC)')
return 'Command not executed (ODASEVA_NOEXEC)';
}
debug('>>> Executing oda_cli...')
var cmdRes = shell.exec(cmdLine, { silent: bSilent, cwd: process.cwd() });
return cmdRes;
}
module.exports.run = run;
module.exports.runFromCmdLine = runFromCmdLine;
// Run if called directly from command line
if (require.main == module) {
debug('Called from command line');
runFromCmdLine();
}