O
O
oleg_templ2018-05-23 01:20:53
Software testing
oleg_templ, 2018-05-23 01:20:53

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);

There is a spec.js file for tests, we call the go.js file for execution in it, but something obviously does not go according to plan.
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) })

Tell me the solution to the problem of calling a file

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladlen Hellsite, 2018-05-23
@oleg_templ

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 question

Ask a Question

731 491 924 answers to any question