Answer the question
In order to leave comments, you need to log in
How to call a shell command to call an executable js file passing arguments from another js (needed to test the cli interface)?
Eats go.js file, passing arguments catches commander.js
const program = require('commander');
program
.version('0.0.1', '-v, --version')
.option('-l, --lol ', console.log('Hello my friend!'))
.parse(process.argv);
function run_cmd(cmd, args, callBack ) {
const spawn = require('child_process').spawn;
const child = spawn(cmd, args);
const resp = "";
child.stdout.on('data', function (buffer) { resp += buffer.toString() });
child.stdout.on('end', function() { callBack (resp) });
};
run_cmd( "./go.js", ["-l"], function(text) { console.log (text) })
Answer the question
In order to leave comments, you need to log in
const { spawn } = require('child_process');
function run_cmd(cmd, args, callBack) {
const child = spawn(cmd, args);
let resp = "";
child.stdout.on('data', function (buffer) { resp += buffer.toString() });
child.stdout.on('end', function() { callBack (resp) });
child.stdout.on('error', callBack);
};
run_cmd("./go.js", ["-l"], function(err, text) {
if (err) return console.error(err);
console.log(text)
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question