li4trg
Last Updated: February 25, 2016
·
517
· phaus
902a4faaa4de6f6aebd6fd7a9fbab46a

Creating "gem install" commands from a given "gem list" output

#!/usr/bin/node

var sys = require('sys')
var exec = require('child_process').exec;
var child;

function getGemInfo(stdout){
    var lines = stdout.split("\n");
    var line, version, name, d, gem;
    for(var i in lines){
        line = lines[i];
        if(line){
            gem = {};
            d = line.indexOf("(");
            gem.name = line.substring(0,d).replace(" ","").replace("\n","");
            gem.versions = getVersions(line.substring(d+1).replace(")",""))
            console.log(createGemCommand(gem));
        }
    }
}

function getVersions(part){
    var versions = part.split(",");
    for(var i in versions){
        versions[i] = versions[i].replace(" ","").replace("\n","");
    }
    return versions;
}

function createGemCommand(gem){
    var base = "gem install";
    var out = "";
    for(var i in  gem.versions){
        out += base+" "+gem.name+" -v="+gem.versions[i]+"\n";
    }
    return out;
}

child = exec("gem list", function (error, stdout, stderr) {
  if (error !== null) {
    console.log('exec error: ' + error);
  } else {
     getGemInfo(stdout);
  }
});
Say Thanks
Respond