Update to make the imports 'shim' more readable as requested.

This commit is contained in:
emizzle 2018-05-22 12:11:45 +10:00
parent d4c04bbed7
commit 85919a4f6d
2 changed files with 20 additions and 12 deletions

View File

@ -114,5 +114,9 @@
"finished building": "finished building", "finished building": "finished building",
"compiling Vyper contracts": "compiling Vyper contracts", "compiling Vyper contracts": "compiling Vyper contracts",
"Vyper exited with error code ": "Vyper exited with error code ", "Vyper exited with error code ": "Vyper exited with error code ",
"attempted to deploy %s without specifying parameters": "attempted to deploy %s without specifying parameters" "attempted to deploy %s without specifying parameters": "attempted to deploy %s without specifying parameters",
"adding %s to ipfs": "adding %s to ipfs",
"DApp available at": "DApp available at",
"successfully uploaded to ipfs": "successfully uploaded to ipfs",
"finished building DApp and deploying to": "finished building DApp and deploying to"
} }

View File

@ -50,23 +50,27 @@ class Pipeline {
// ensure the .embark/contracts directory exists (create if not exists) // ensure the .embark/contracts directory exists (create if not exists)
fs.mkdirp(fs.dappPath(".embark/contracts", ''), (err) => { fs.mkdirp(fs.dappPath(".embark/contracts", ''), (err) => {
if(err) return next(err); if(err) return next(err);
async.each(contracts, (contract, eachCb) => {
// Create a file .embark/contracts/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'));
importsHelperFile.write('module.exports = {\n');
async.eachOf(contracts, (contract, idx, eachCb) => {
self.events.request('code-generator:contract', contract.className, (contractCode) => { self.events.request('code-generator:contract', contract.className, (contractCode) => {
let filePath = fs.dappPath(".embark/contracts", contract.className + '.js'); let filePath = fs.dappPath(".embark/contracts", contract.className + '.js');
importsList["Embark/contracts/" + contract.className] = filePath; importsList["Embark/contracts/" + contract.className] = filePath;
fs.writeFile(filePath, contractCode, eachCb); fs.writeFile(filePath, contractCode, eachCb);
// 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
}); });
}, function(){ }, function(){
// create a file .embark/contracts/index.js that requires all files in the .embark/contracts directory importsHelperFile.write('\n}'); // close the module.exports = {}
// except for itself. Used to enable alternate import syntax: importsHelperFile.close(next); // close the write stream
// e.g. import {Token} from 'Embark/contracts'
// e.g. import * as Contracts from 'Embark/contracts'
let importsHelper = `module.exports = (ctx => {
let keys = ctx.keys();
let values = keys.map(ctx);
return keys.reduce((o, k, i) => { o[k.replace('./', '').replace('.js', '')] = values[i].default; return o; }, {});
})(require.context('./', true, /^(?!.*index).*\.js$/));`;
fs.writeFile(fs.dappPath('.embark/contracts/index.js'), importsHelper, next);
}); });
}); });
}); });