make compiler module async

This commit is contained in:
Iuri Matias 2017-02-17 07:14:44 -05:00
parent 27a3a9c3f2
commit c339732cd6
3 changed files with 43 additions and 22 deletions

View File

@ -1,11 +1,23 @@
/*jshint esversion: 6, loopfunc: true */ /*jshint esversion: 6, loopfunc: true */
var solc = require('solc'); var solc = require('solc');
var async = require('async');
function asyncEachObject(object, iterator, callback) {
async.each(
Object.keys(object || {}),
function(key, next){
iterator(key, object[key], next);
},
callback
);
}
async.eachObject = asyncEachObject;
var Compiler = function(options) { var Compiler = function(options) {
this.plugins = options.plugins; this.plugins = options.plugins;
}; };
Compiler.prototype.compile_contracts = function(contractFiles) { Compiler.prototype.compile_contracts = function(contractFiles, cb) {
var available_compilers = { var available_compilers = {
//".se": this.compile_serpent //".se": this.compile_serpent
@ -25,20 +37,25 @@ Compiler.prototype.compile_contracts = function(contractFiles) {
var compiledObject = {}; var compiledObject = {};
// TODO: warn about files it doesn't know how to compile async.eachObject(available_compilers,
for (var extension in available_compilers) { function(extension, compiler, callback) {
var compiler = available_compilers[extension]; // TODO: warn about files it doesn't know how to compile
var matchingFiles = contractFiles.filter(function(file) { var matchingFiles = contractFiles.filter(function(file) {
return (file.filename.match(/\.[0-9a-z]+$/)[0] === extension); return (file.filename.match(/\.[0-9a-z]+$/)[0] === extension);
}); });
Object.assign(compiledObject, compiler.call(compiler, matchingFiles || [])); compiler.call(compiler, matchingFiles || [], function(compileResult) {
} Object.assign(compiledObject, compileResult);
callback();
return compiledObject; });
},
function (err) {
cb(compiledObject);
}
);
}; };
Compiler.prototype.compile_solidity = function(contractFiles) { Compiler.prototype.compile_solidity = function(contractFiles, cb) {
var input = {}; var input = {};
for (var i = 0; i < contractFiles.length; i++){ for (var i = 0; i < contractFiles.length; i++){
@ -68,7 +85,7 @@ Compiler.prototype.compile_solidity = function(contractFiles) {
compiled_object[className].abiDefinition = JSON.parse(contract.interface); compiled_object[className].abiDefinition = JSON.parse(contract.interface);
} }
return compiled_object; cb(compiled_object);
}; };
module.exports = Compiler; module.exports = Compiler;

View File

@ -39,12 +39,15 @@ ContractsManager.prototype.build = function(done) {
async.waterfall([ async.waterfall([
function compileContracts(callback) { function compileContracts(callback) {
var compiler = new Compiler({plugins: self.plugins}); var compiler = new Compiler({plugins: self.plugins});
// TODO: check if try is still needed
try { try {
self.compiledContracts = compiler.compile_contracts(self.contractFiles); compiler.compile_contracts(self.contractFiles, function(compiledObject) {
self.compiledContracts = compiledObject;
callback();
});
} catch(err) { } catch(err) {
return callback(new Error(err.message)); return callback(new Error(err.message));
} }
return callback();
}, },
function prepareContractsFromConfig(callback) { function prepareContractsFromConfig(callback) {
var className, contract; var className, contract;

File diff suppressed because one or more lines are too long