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

Commit

Permalink
integration into ethereumNode.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Luca Zeug committed Aug 8, 2016
1 parent dd131d7 commit 0e5dad8
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 67 deletions.
16 changes: 8 additions & 8 deletions modules/ethereumNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ class EthereumNode extends EventEmitter {
this._type = null;
this._network = null;

getNodePath();

this._socket = Sockets.get('node-ipc', Settings.rpcMode);

this.on('data', _.bind(this._logNodeData, this));
Expand Down Expand Up @@ -329,13 +327,15 @@ class EthereumNode extends EventEmitter {
this._network = network;
this._type = nodeType;

const binPath = getNodePath(nodeType);

log.debug(`Start node using ${binPath}`);

return new Q((resolve, reject) => {
this.__startProcess(nodeType, network, binPath)
.then(resolve, reject);
getNodePath.probe()
.then(() => {
const binPath = getNodePath.query(nodeType);
log.debug(`Start node using ${binPath}`);

this.__startProcess(nodeType, network, binPath)
.then(resolve, reject);
});
});
}

Expand Down
131 changes: 72 additions & 59 deletions modules/getNodePath.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ Gets the right Node path
@module getNodePath
*/

const path = require('path');
const fs = require('fs');
const exec = require('child_process').exec;
const path = require('path');
const Q = require('bluebird');
const exec = require('child_process').exec;
const cmpVer = require('semver-compare');
const binaryPath = path.resolve(__dirname + '/../nodes');
const log = require('./utils/logger').create('getNodePath');
Expand Down Expand Up @@ -73,71 +73,84 @@ function compareNodes() {
}


/**
* Get paths of all nodes, returns system or bundled path depending on the latest version
*
* @param {String} type the type of node (i.e. 'geth', 'eth')
*/
module.exports = function(type) {
if (resolvedPaths[type])
return resolvedPaths[type];

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'));
}
module.exports = {
/**
* Returns path of node
* linux and mac only: returns system or bundled path depending on the latest version
*
* @param {String} type the type of node (i.e. 'geth', 'eth')
*/
query: (type) => {
return resolvedPaths[type];
},
/**
* Evaluates node paths
* - Enumerates bundled nodes
* linux and mac only:
* - probes for system installation of nodes
* - compares the versions of bundled and system nodes (preferes latest version)
*/
probe: () => {
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;
}
});
}

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;
}));
});
} 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 = [];

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;
}));
}
getPathProms.push(getSystemPath(type)
.then((data) => {
paths[type][data.match(/(\/\w+)+/)[0]] = null;
}));
}
}).then(() => {
Q.all(getVersionProms).then(() => {
compareNodes();
log.debug('Available backends: %j', paths);

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);
})
.then(resolve, reject);
})
});
}
});
} else {
resolve();
}
});
}
}

0 comments on commit 0e5dad8

Please sign in to comment.