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 */
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) {
this.plugins = options.plugins;
};
Compiler.prototype.compile_contracts = function(contractFiles) {
Compiler.prototype.compile_contracts = function(contractFiles, cb) {
var available_compilers = {
//".se": this.compile_serpent
@ -25,20 +37,25 @@ Compiler.prototype.compile_contracts = function(contractFiles) {
var compiledObject = {};
async.eachObject(available_compilers,
function(extension, compiler, callback) {
// TODO: warn about files it doesn't know how to compile
for (var extension in available_compilers) {
var compiler = available_compilers[extension];
var matchingFiles = contractFiles.filter(function(file) {
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();
});
},
function (err) {
cb(compiledObject);
}
return compiledObject;
);
};
Compiler.prototype.compile_solidity = function(contractFiles) {
Compiler.prototype.compile_solidity = function(contractFiles, cb) {
var input = {};
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);
}
return compiled_object;
cb(compiled_object);
};
module.exports = Compiler;

View File

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

File diff suppressed because one or more lines are too long