Skip to content
This repository has been archived by the owner on Sep 5, 2020. It is now read-only.

Commit

Permalink
use promises
Browse files Browse the repository at this point in the history
  • Loading branch information
Luca Zeug committed Aug 8, 2016
1 parent 23132a7 commit 9066989
Showing 1 changed file with 85 additions and 60 deletions.
145 changes: 85 additions & 60 deletions modules/getNodePath.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Gets the right Node path
const path = require('path');
const fs = require('fs');
const exec = require('child_process').exec;
const Q = require('bluebird');
const cmpVer = require('semver-compare');
const binaryPath = path.resolve(__dirname + '/../nodes');
const log = require('./utils/logger').create('getNodePath');
Expand All @@ -26,9 +27,9 @@ const paths = {}, // all versions (system + bundled): { type: { path: ve
* @param {String} type the type of node (i.e. 'geth', 'eth')
*/
function getSystemPath(type) {
var proc = exec('type ' + type, (e, stdout, stderr) => {
if (!e)
paths[type][stdout.match(/(\/\w+)+/)[0]] = null;
return new Q((resolve, reject) => {
var proc = exec('type ' + type);
proc.stdout.on('data', resolve);
});
}

Expand All @@ -37,26 +38,38 @@ function getSystemPath(type) {
* Get versions of node (system and bundled)
*
* @param {String} type the type of node (i.e. 'geth', 'eth')
* @param {String} path the path of the node instance
*/
function getVersion(type) {
setTimeout(() => {
for (let path in paths[type]) {
switch (type) {
case 'geth':
var command = path + ' version';
break;
case 'eth':
case 'parity':
var command = path + ' --version';
break;
function getVersion(type, path) {
return new Q((resolve, reject) => {
switch (type) {
case 'geth':
var command = path + ' version';
break;
case 'eth':
case 'parity':
var command = path + ' --version';
break;
}
var proc = exec(command);
proc.stdout.on('data', resolve);
});
}


/**
* Compare versions of system and bundled nodes
*/
function compareNodes() {
return new Q((resolve, reject) => {
for (let type in paths) {
var path = Object.keys(paths[type])[0];
if (Object.keys(paths[type]).length > 1) {
path = (cmpVer(Object.keys(paths[type])[0], Object.keys(paths[type])[1])) ? Object.keys(paths[type])[0] : Object.keys(paths[type])[1]
}
var proc = exec(command, (e, stdout, stderr) => {
if (!e) {
paths[type][path] = stdout.match(/[\d.]+/)[0];
}
});
resolvedPaths[type] = path;
}
}, 200);
});
}


Expand All @@ -66,53 +79,65 @@ function getVersion(type) {
* @param {String} type the type of node (i.e. 'geth', 'eth')
*/
module.exports = function(type) {
if (resolvedPaths[type]) {
if (resolvedPaths[type])
return resolvedPaths[type];
}

if(Settings.inProductionMode) {
var binPath = binaryPath.replace('nodes','node').replace('app.asar/','').replace('app.asar\\','');

if(process.platform === 'darwin') {
binPath = path.resolve(binPath.replace('/node', '/../Frameworks/node'));
}

return new Q((resolve, reject) => {
if(Settings.inProductionMode) {
var binPath = binaryPath.replace('nodes','node').replace('app.asar/','').replace('app.asar\\','');
if(process.platform === 'darwin') {
binPath = path.resolve(binPath.replace('/node', '/../Frameworks/node'));
}

fs.readdirSync(binPath).forEach((type) => {
var nodePath = binPath + '/' + type + '/' + type;
fs.readdirSync(binPath).forEach((type) => {
var nodePath = binPath + '/' + type + '/' + type;

if(process.platform === 'win32') {
nodePath = nodePath.replace(/\/+/,'\\');
nodePath += '.exe';
}
if(process.platform === 'win32') {
nodePath = nodePath.replace(/\/+/,'\\');
nodePath += '.exe';
}

paths[type] = {};
paths[type][nodePath] = null;
});
} else {
fs.readdirSync('nodes/').forEach((type) => {
if (fs.statSync('nodes/' + type).isDirectory()) {
var nodePath = path.resolve('nodes/' + type + '/' + process.platform +'-'+ process.arch + '/' + type);
paths[type] = {};
paths[type][nodePath] = null;
});
} else {
fs.readdirSync('nodes/').forEach((type) => {
if (fs.statSync('nodes/' + type).isDirectory()) {
var nodePath = path.resolve('nodes/' + type + '/' + process.platform +'-'+ process.arch + '/' + type);
paths[type] = {};
paths[type][nodePath] = null;
}
});
}

if (process.platform === 'linux' || process.platform === 'darwin') {
var getPathProms = [],
getVersionProms = [];

for (let type in paths) {
getPathProms.push(getSystemPath(type)
.then((data) => {
paths[type][data.match(/(\/\w+)+/)[0]] = null;
}));
}
});
}

if (process.platform === 'linux' || process.platform === 'darwin') {
for (var type in paths)
getVersion(type, getSystemPath(type));
}

setTimeout(() => {
for (type in paths) {
var path = Object.keys(paths[type])[0];

if (Object.keys(paths[type]).length > 1)
path = (cmpVer(Object.keys(paths[type])[0], Object.keys(paths[type])[1])) ? Object.keys(paths[type])[0] : Object.keys(paths[type])[1]

resolvedPaths[type] = path;
Q.all(getPathProms).then(() => {
for (let type in paths) {
for (let path in paths[type]) {
getVersionProms.push(getVersion(type, path)
.then((data)=>{
var version = data.match(/[\d.]+/)[0];
paths[type][path] = version;
}));
}
}
}).then(() => {
Q.all(getVersionProms).then(() => {
compareNodes();
log.debug('Available backends: %j', paths);
})
});
}

log.debug('Available backends: %j', paths);
}, 3000);
});
}

0 comments on commit 9066989

Please sign in to comment.