diff --git a/lib/contracts/code_generator.js b/lib/contracts/code_generator.js index 801a07ac0..383513b16 100644 --- a/lib/contracts/code_generator.js +++ b/lib/contracts/code_generator.js @@ -210,9 +210,17 @@ class CodeGenerator { result += Templates.define_when_env_loaded(); - if (self.storageConfig.provider === 'ipfs' && self.storageConfig.enabled === true) { - let block = "\nEmbarkJS.Storage.setProvider('" + self.storageConfig.provider + "', {server: '" + self.storageConfig.host + "', port: '" + self.storageConfig.port + "', getUrl: '" + self.storageConfig.getUrl + "'});"; - result += Templates.exec_when_env_loaded({block: block}); + let pluginsWithCode = this.plugins.getPluginsFor('initCode'); + if (pluginsWithCode.length > 0) { + for (let plugin of pluginsWithCode) { + let initCodes = plugin.embarkjs_init_code.storage; + for (let initCode of initCodes) { + let [block, shouldInit] = initCode; + if (shouldInit.call(plugin, self.storageConfig)) { + result += Templates.exec_when_env_loaded({block: block}); + } + } + } } return result; diff --git a/lib/core/plugin.js b/lib/core/plugin.js index 3f5bdda62..9fe984f92 100644 --- a/lib/core/plugin.js +++ b/lib/core/plugin.js @@ -22,6 +22,7 @@ var Plugin = function(options) { this.pluginTypes = []; this.uploadCmds = []; this.embarkjs_code = []; + this.embarkjs_init_code = {}; this.logger = options.logger; this.events = options.events; this.config = options.config; @@ -148,6 +149,12 @@ Plugin.prototype.addCodeToEmbarkJS = function(code) { this.pluginTypes.push('embarkjsCode'); }; +Plugin.prototype.addProviderInit = function(providerType, code, initCondition) { + this.embarkjs_init_code[providerType] = this.embarkjs_init_code[providerType] || []; + this.embarkjs_init_code[providerType].push([code, initCondition]); + this.pluginTypes.push('initCode'); +}; + Plugin.prototype.runCommands = function(cmd, options) { return this.console.map(function(cb) { return cb.call(this, cmd, options); diff --git a/lib/modules/ipfs/index.js b/lib/modules/ipfs/index.js index acb3347d8..0b9e146a1 100644 --- a/lib/modules/ipfs/index.js +++ b/lib/modules/ipfs/index.js @@ -17,6 +17,7 @@ class IPFS { this.commandlineDeploy(); this.setServiceCheck(); this.addIPFSToEmbarkJS(); + this.addSetProvider(); } commandlineDeploy() { @@ -98,6 +99,21 @@ class IPFS { this.embark.addCodeToEmbarkJS(code); } + + addSetProvider() { + let config = JSON.stringify({ + server: this.storageConfig.host, + port: this.storageConfig.port, + getUrl: this.storageConfig.getUrl + }); + let code = "\nEmbarkJS.Storage.setProvider('ipfs'," + config + ");"; + + let shouldInit = (storageConfig) => { + return (storageConfig.provider === 'ipfs' && storageConfig.enabled === true); + }; + + this.embark.addProviderInit('storage', code, shouldInit); + } } module.exports = IPFS;