move contracts generation from pipeline to code generator module

This commit is contained in:
Iuri Matias 2018-05-16 10:00:56 -04:00
parent 7271b00cf4
commit 6544c2a999
2 changed files with 26 additions and 19 deletions

View File

@ -100,6 +100,12 @@ class CodeGenerator {
self.buildWeb3JS(cb);
});
this.events.setCommandHandler('code-generator:contract', (contractName, cb) => {
self.events.request('contracts:contract', contractName, (contractJSON) => {
self.buildContractJS(contractName, contractJSON.toString(), cb);
});
});
}
generateContext() {
@ -323,6 +329,22 @@ class CodeGenerator {
});
}
buildContractJS(contractName, contractJSON, cb) {
let contractCode = "";
contractCode += "import web3 from 'Embark/web3';\n";
contractCode += "import EmbarkJS from 'Embark/EmbarkJS';\n";
contractCode += "let " + contractName + "JSONConfig = " + contractJSON + ";\n";
contractCode += "let " + contractName + " = new EmbarkJS.Contract(" + contractName + "JSONConfig);\n";
contractCode += "\n__embarkContext.execWhenReady(function() {\n";
contractCode += "\n" + contractName + ".setProvider(web3.currentProvider);\n";
contractCode += "\n" + contractName + ".options.from = web3.eth.defaultAccount;\n";
contractCode += "\n});\n";
contractCode += "export default " + contractName + ";\n";
cb(null, contractCode);
}
buildWeb3JS(cb) {
const self = this;
let code = "";

View File

@ -176,7 +176,7 @@ class Pipeline {
);
}
buildContracts(contractsJSON, callback) {
buildContracts(contractsJSON, JSON, callback) {
fs.mkdirp(fs.dappPath(this.buildDir, 'contracts'), (err) => {
if (err) {
return callback(err);
@ -189,25 +189,10 @@ class Pipeline {
}
buildContractJS(contractName, callback) {
fs.readFile(fs.dappPath(this.buildDir, 'contracts', contractName + '.json'), (err, contractJSON) => {
if (err) {
return callback(err);
}
contractJSON = contractJSON.toString();
const self = this;
let contractCode = "";
contractCode += "import web3 from 'Embark/web3';\n";
contractCode += "import EmbarkJS from 'Embark/EmbarkJS';\n";
contractCode += "let " + contractName + "JSONConfig = " + contractJSON + ";\n";
contractCode += "let " + contractName + " = new EmbarkJS.Contract(" + contractName + "JSONConfig);\n";
contractCode += "\n__embarkContext.execWhenReady(function() {\n";
contractCode += "\n" + contractName + ".setProvider(web3.currentProvider);\n";
contractCode += "\n" + contractName + ".options.from = web3.eth.defaultAccount;\n";
contractCode += "\n});\n";
contractCode += "export default " + contractName + ";\n";
callback(null, contractCode);
self.events.request('code-generator:contract', contractName, (code) => {
callback(null, code);
});
}