How to run two different node apps (forceios and forcedroid) requiring variable arguments with one command

56 Views Asked by At

So, there are two completely different packages with their own package.json files and command sets. forcedroid and forceios. Each of these take some arguments... and they are in the same order. The arguments are then taken into the application and manipulated and/or used.

They are installed globally.

The commands to run each separately are as follows:

forceios create argv1 argv2 argv3 forcedroid create argv1 argv2 argv3

Ideally, I want to create one command to rule them all.

forceboth create argv1 argv2 argv3

I would like this command to execute each of the above.

1

There are 1 best solutions below

1
On BEST ANSWER

Inspired from your question, I tried to write basic global node_module forceboth that can run both commands using only one forceboth.

You can clone the repo and run npm install -g to install the package globally.

Or, save the following code and run node cmd.js create argv1 argv2 argv3

cmd.js

var args = [];

process.argv.forEach(function (val, index, array) {
  args.push(val);
});

var foreiOS = new run_cmd(
  'forceios',args,
  function (me, buffer) { me.stdout += buffer.toString() },
  function () { console.log(foreiOS.stdout) }
);

var foreDriod = new run_cmd(
  'forcedroid',args,
  function (me, buffer) { me.stdout += buffer.toString() },
  function () { console.log(foreDriod.stdout) }
);    

function run_cmd(cmd, args, cb, end) {
   var spawn = require('child_process').spawn;
   var child = spawn(cmd, args);
   var me = this;
   child.stdout.on('data', function (buffer) { cb(me, buffer) });
   child.stdout.on('end', end);
}

There can be issue with the code. Please update it, if you have any concern.