diff --git a/lib/contracts/code_generator.js b/lib/contracts/code_generator.js index 46f416291..d8577b5f0 100644 --- a/lib/contracts/code_generator.js +++ b/lib/contracts/code_generator.js @@ -285,13 +285,15 @@ class CodeGenerator { code += "\nimport Web3 from '" + utils.joinPath(fs.embarkPath("js/web3-1.0.min.js")) + "'\n"; code += "\nimport web3 from 'Embark/web3';\n"; - if (this.storageConfig !== {} && this.storageConfig.provider === 'ipfs' && this.storageConfig.enabled === true) { - code += "\nimport IpfsApi from 'ipfs-api';\n"; - } - code += "\n" + embarkjsCode + "\n"; - code += "\n" + fs.readFileSync(fs.embarkPath('js/embarkjs/ipfs.js')).toString(); + let pluginsWithCode = this.plugins.getPluginsFor('embarkjsCode'); + if (pluginsWithCode.length > 0) { + for (let plugin of pluginsWithCode) { + code += plugin.embarkjs_code.join('\n'); + } + } + code += "\n" + fs.readFileSync(fs.embarkPath('js/embarkjs/whisper.js')).toString(); //code += "\n" + fs.readFileSync(fs.embarkPath('js/embarkjs/orbit.js')).toString(); diff --git a/lib/core/plugin.js b/lib/core/plugin.js index 94e9a5099..3f5bdda62 100644 --- a/lib/core/plugin.js +++ b/lib/core/plugin.js @@ -21,6 +21,7 @@ var Plugin = function(options) { this.serviceChecks = []; this.pluginTypes = []; this.uploadCmds = []; + this.embarkjs_code = []; this.logger = options.logger; this.events = options.events; this.config = options.config; @@ -142,6 +143,11 @@ Plugin.prototype.registerUploadCommand = function(cmd, cb) { this.pluginTypes.push('uploadCmds'); }; +Plugin.prototype.addCodeToEmbarkJS = function(code) { + this.embarkjs_code.push(code); + this.pluginTypes.push('embarkjsCode'); +}; + Plugin.prototype.runCommands = function(cmd, options) { return this.console.map(function(cb) { return cb.call(this, cmd, options); diff --git a/js/embarkjs/ipfs.js b/lib/modules/ipfs/embarkjs.js similarity index 95% rename from js/embarkjs/ipfs.js rename to lib/modules/ipfs/embarkjs.js index e55850cfb..16e68c121 100644 --- a/js/embarkjs/ipfs.js +++ b/lib/modules/ipfs/embarkjs.js @@ -1,10 +1,10 @@ +import IpfsApi from 'ipfs-api'; + let __embarkIPFS = {}; __embarkIPFS.setProvider = function(options) { var self = this; var promise = new Promise(function(resolve, reject) { - self.currentStorage = EmbarkJS.Storage.IPFS; - try { if (options === undefined) { self.ipfsConnection = IpfsApi('localhost', '5001'); @@ -26,7 +26,7 @@ __embarkIPFS.setProvider = function(options) { __embarkIPFS.saveText = function(text) { const self = this; var promise = new Promise(function(resolve, reject) { - if (!this.ipfsConnection) { + if (!self.ipfsConnection) { var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()'); reject(connectionError); } @@ -43,6 +43,7 @@ __embarkIPFS.saveText = function(text) { }; __embarkIPFS.get = function(hash) { + const self = this; // TODO: detect type, then convert if needed //var ipfsHash = web3.toAscii(hash); var promise = new Promise(function(resolve, reject) { @@ -61,6 +62,7 @@ __embarkIPFS.get = function(hash) { }; __embarkIPFS.uploadFile = function(inputSelector) { + const self = this; var file = inputSelector[0].files[0]; if (file === undefined) { @@ -94,4 +96,3 @@ __embarkIPFS.getUrl = function(hash) { return (self._getUrl || "http://localhost:8080/ipfs/") + hash; }; -EmbarkJS.Storage.registerProvider('ipfs', __embarkIPFS); diff --git a/lib/modules/ipfs/index.js b/lib/modules/ipfs/index.js index 76bf90ea4..acb3347d8 100644 --- a/lib/modules/ipfs/index.js +++ b/lib/modules/ipfs/index.js @@ -1,5 +1,6 @@ let UploadIPFS = require('./upload.js'); let utils = require('../../utils/utils.js'); +let fs = require('../../core/fs.js'); class IPFS { @@ -15,6 +16,7 @@ class IPFS { this.commandlineDeploy(); this.setServiceCheck(); + this.addIPFSToEmbarkJS(); } commandlineDeploy() { @@ -82,6 +84,20 @@ class IPFS { }); } + addIPFSToEmbarkJS() { + if (this.storageConfig === {}) { + return; + } + if(this.storageConfig.provider !== 'ipfs' || this.storageConfig.enabled !== true) { + return; + } + + let code = ""; + code += "\n" + fs.readFileSync(utils.joinPath(__dirname, 'embarkjs.js')).toString(); + code += "\nEmbarkJS.Storage.registerProvider('ipfs', __embarkIPFS);"; + + this.embark.addCodeToEmbarkJS(code); + } } module.exports = IPFS;