Merge pull request #445 from embark-framework/features/improve-imports

Improve dapp imports to allow alternate syntax for importing contracts
This commit is contained in:
Iuri Matias 2018-05-22 12:06:22 -04:00 committed by GitHub
commit ceb48ebb21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 8 deletions

View File

@ -101,8 +101,17 @@
"downloading {{packageName}} {{version}}....": "downloading {{packageName}} {{version}}....",
"Swarm node is offline...": "Swarm node is offline...",
"Swarm node detected...": "Swarm node detected...",
"Cannot find file %s Please ensure you are running this command inside the Dapp folder": "Cannot find file %s Please ensure you are running this command inside the Dapp folder",
"finished building": "finished building",
"compiling Vyper contracts": "compiling Vyper contracts",
"Vyper exited with error code ": "Vyper exited with error code ",
"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",
"Ethereum node (version unknown)": "Ethereum node (version unknown)",
"No Blockchain node found": "No Blockchain node found",
"Couldn't connect to an Ethereum node are you sure it's on?": "Couldn't connect to an Ethereum node are you sure it's on?",
"make sure you have an Ethereum node or simulator running. e.g '%s'": "make sure you have an Ethereum node or simulator running. e.g '%s'"
"make sure you have an Ethereum node or simulator running. e.g '%s'": "make sure you have an Ethereum node or simulator running. e.g '%s'",
"finished building DApp and deploying to": "finished building DApp and deploying to"
}

View File

@ -36,6 +36,7 @@ class Pipeline {
function createImportList(next) {
importsList["Embark/EmbarkJS"] = fs.dappPath(".embark", 'embark.js');
importsList["Embark/web3"] = fs.dappPath(".embark", 'web3_instance.js');
importsList["Embark/contracts"] = fs.dappPath(".embark/contracts", '');
self.plugins.getPluginsProperty('imports', 'imports').forEach(function (importObject) {
let [importName, importLocation] = importObject;
@ -44,15 +45,34 @@ class Pipeline {
next();
},
function writeContracts(next) {
function writeContracts(next) {
self.events.request('contracts:list', (contracts) => {
async.each(contracts, (contract, eachCb) => {
self.events.request('code-generator:contract', contract.className, (contractCode) => {
let filePath = fs.dappPath(".embark", contract.className + '.js');
importsList["Embark/contracts/" + contract.className] = filePath;
fs.writeFile(filePath, contractCode, eachCb);
// ensure the .embark/contracts directory exists (create if not exists)
fs.mkdirp(fs.dappPath(".embark/contracts", ''), (err) => {
if(err) return next(err);
// 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) => {
let filePath = fs.dappPath(".embark/contracts", contract.className + '.js');
importsList["Embark/contracts/" + contract.className] = filePath;
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(){
importsHelperFile.write('\n}'); // close the module.exports = {}
importsHelperFile.close(next); // close the write stream
});
}, next);
});
});
},
function assetFileWrite(next) {