Run/Install ruby gem from io.js with child_process

300 Views Asked by At

AIM: Create a desktop app(GUI) with Electron(Atom Shell), that runs a gem's commands from io.js.

1. The problem is that I want to know which is the best way to handle the commands call to the gem inside the system.

2. If the gem is not installed inside the system the desktop app is worthless. Which is the best way to handle this dependency?

EXAMPLE CODE THAT I'VE SORTED OUT

var spawn = require("child_process").spawn;
var jk = spawn('jekyll', ['serve']);

jk.stdout.on('data', function(data){
  console.log(data.toString('utf8')); // WRITE IN THE CONSOLE GEM OUTPUT
});

Is that code a good way to execute the commands?.

My ideal scenario is "As a user I want to press a button that compile the site" -> On click then run the build command of the gem that will be handled by that code possibly.

1

There are 1 best solutions below

2
On

Here's a contrived example using "ShellJS" and jekyll as the dependency:

var shell = require('shelljs');

if (shell.which('gem')) {
  if (shell.exec("gem install jekyll").code === 0) {
      shell.exec("jekyll new my-site");
      //Do more stuff
      shell.exec("jekyll build");
  }
} else {
  echo('Sorry, this program requires gem. Install from Source');
  exit(1);
}

The idea is to use which to check if gem is installed and then install the dependency. You can then use exec to run commands with that gem.