How to call shell command to call an executable js file with passing arguments from another js (need to test cli interface)?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
4 views
Eats file go.js the transmission of 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);


There is a file spec.js for tests, it caused the file go.js but something is wrong.
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) })


Could you tell me the solution of the problem call file
by | 4 views

1 Answer

0 like 0 dislike
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) });
by
110,608 questions
257,186 answers
0 comments
27,889 users