From f0605bed2b21bcca17fa295d9422b15c805292de Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Fri, 8 Feb 2019 13:02:42 -0500 Subject: [PATCH] refactor(generation/contracts): generate contracts to artifact dir --- packages/embark/src/lib/constants.json | 3 ++- .../src/lib/modules/code_generator/index.js | 5 +++-- .../embark/src/lib/modules/pipeline/index.js | 20 ++++++++++--------- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/packages/embark/src/lib/constants.json b/packages/embark/src/lib/constants.json index 7e58d2c47..d54973cdc 100644 --- a/packages/embark/src/lib/constants.json +++ b/packages/embark/src/lib/constants.json @@ -89,6 +89,7 @@ "blockchain": "blockchain.json", "storage": "storage.json", "communication": "communication.json", - "embarkjs": "embarkjs.js" + "embarkjs": "embarkjs.js", + "contractsJs": "contracts" } } diff --git a/packages/embark/src/lib/modules/code_generator/index.js b/packages/embark/src/lib/modules/code_generator/index.js index 28e77ab39..fa972b5c1 100644 --- a/packages/embark/src/lib/modules/code_generator/index.js +++ b/packages/embark/src/lib/modules/code_generator/index.js @@ -189,7 +189,7 @@ class CodeGenerator { if (err) { this.logger.error(err.message || err); } - cb(err); + cb(err, filePath); }); } @@ -376,7 +376,8 @@ class CodeGenerator { contractCode += `let ${contractName} = new EmbarkJS.Blockchain.Contract(${contractName}JSONConfig);\n`; contractCode += "export default " + contractName + ";\n"; - cb(contractCode); + + this.generateArtifact(contractCode, contractName + '.js', constants.dappConfig.contractsJs, cb); } buildWeb3JS(cb) { diff --git a/packages/embark/src/lib/modules/pipeline/index.js b/packages/embark/src/lib/modules/pipeline/index.js index 2acc31072..d5d74ec9d 100644 --- a/packages/embark/src/lib/modules/pipeline/index.js +++ b/packages/embark/src/lib/modules/pipeline/index.js @@ -139,6 +139,7 @@ class Pipeline { let self = this; const importsList = {}; let placeholderPage; + const contractsDir = fs.dappPath(self.embarkConfig.generationDir, constants.dappConfig.contractsJs); if (!self.assetFiles || !Object.keys(self.assetFiles).length) { return self.buildContracts(callback); @@ -157,7 +158,7 @@ class Pipeline { function createImportList(next) { importsList["Embark/EmbarkJS"] = fs.dappPath(self.embarkConfig.generationDir, constants.dappConfig.embarkjs); importsList["Embark/web3"] = fs.dappPath(".embark", 'web3_instance.js'); - importsList["Embark/contracts"] = fs.dappPath(".embark/contracts", ''); + importsList["Embark/contracts"] = contractsDir; self.plugins.getPluginsProperty('imports', 'imports').forEach(importObject => { let [importName, importLocation] = importObject; @@ -167,26 +168,27 @@ class Pipeline { }, function writeContracts(next) { self.events.request('contracts:list', (_err, contracts) => { - // ensure the .embark/contracts directory exists (create if not exists) - fs.mkdirp(fs.dappPath(".embark/contracts", ''), err => { + fs.mkdirp(contractsDir, err => { if (err) return next(err); - // Create a file .embark/contracts/index.js that requires all contract files + // Create a file index.js that requires all contract files // Used to enable alternate import syntax: // e.g. import {Token} from 'Embark/contracts' // e.g. import * as Contracts from 'Embark/contracts' - let importsHelperFile = fs.createWriteStream(fs.dappPath(".embark/contracts", 'index.js')); + let importsHelperFile = fs.createWriteStream(utils.joinPath(contractsDir, 'index.js')); importsHelperFile.write('module.exports = {\n'); async.eachOf(contracts, (contract, idx, eachCb) => { - self.events.request('code-generator:contract', contract.className, contractCode => { - let filePath = fs.dappPath(".embark/contracts", contract.className + '.js'); - importsList["Embark/contracts/" + contract.className] = filePath; - fs.writeFile(filePath, contractCode, eachCb); + self.events.request('code-generator:contract', contract.className, (err, contractPath) => { + if (err) { + return eachCb(err); + } + importsList["Embark/contracts/" + contract.className] = contractPath; // add the contract to the exports list to support alternate import syntax importsHelperFile.write(`"${contract.className}": require('./${contract.className}').default`); if (idx < contracts.length - 1) importsHelperFile.write(',\n'); // add a comma if we have more contracts to add + eachCb(); }); }, () => { importsHelperFile.write('\n}'); // close the module.exports = {}