mirror of https://github.com/embarklabs/embark.git
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:
commit
ceb48ebb21
|
@ -101,8 +101,17 @@
|
||||||
"downloading {{packageName}} {{version}}....": "downloading {{packageName}} {{version}}....",
|
"downloading {{packageName}} {{version}}....": "downloading {{packageName}} {{version}}....",
|
||||||
"Swarm node is offline...": "Swarm node is offline...",
|
"Swarm node is offline...": "Swarm node is offline...",
|
||||||
"Swarm node detected...": "Swarm node detected...",
|
"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)",
|
"Ethereum node (version unknown)": "Ethereum node (version unknown)",
|
||||||
"No Blockchain node found": "No Blockchain node found",
|
"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?",
|
"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"
|
||||||
}
|
}
|
|
@ -36,6 +36,7 @@ class Pipeline {
|
||||||
function createImportList(next) {
|
function createImportList(next) {
|
||||||
importsList["Embark/EmbarkJS"] = fs.dappPath(".embark", 'embark.js');
|
importsList["Embark/EmbarkJS"] = fs.dappPath(".embark", 'embark.js');
|
||||||
importsList["Embark/web3"] = fs.dappPath(".embark", 'web3_instance.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) {
|
self.plugins.getPluginsProperty('imports', 'imports').forEach(function (importObject) {
|
||||||
let [importName, importLocation] = importObject;
|
let [importName, importLocation] = importObject;
|
||||||
|
@ -44,15 +45,34 @@ class Pipeline {
|
||||||
|
|
||||||
next();
|
next();
|
||||||
},
|
},
|
||||||
function writeContracts(next) {
|
function writeContracts(next) {
|
||||||
self.events.request('contracts:list', (contracts) => {
|
self.events.request('contracts:list', (contracts) => {
|
||||||
async.each(contracts, (contract, eachCb) => {
|
// ensure the .embark/contracts directory exists (create if not exists)
|
||||||
self.events.request('code-generator:contract', contract.className, (contractCode) => {
|
fs.mkdirp(fs.dappPath(".embark/contracts", ''), (err) => {
|
||||||
let filePath = fs.dappPath(".embark", contract.className + '.js');
|
if(err) return next(err);
|
||||||
importsList["Embark/contracts/" + contract.className] = filePath;
|
|
||||||
fs.writeFile(filePath, contractCode, 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) => {
|
||||||
|
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) {
|
function assetFileWrite(next) {
|
||||||
|
|
Loading…
Reference in New Issue