From 9592f3e69b95f96280811338417bbee295061a4b Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sun, 8 Jul 2018 20:40:06 +0300 Subject: [PATCH] re-add register upload cmd api; refactor storage module initialization --- lib/core/engine.js | 14 +++----------- lib/core/plugin.js | 6 ++++++ lib/modules/ipfs/index.js | 25 +++++++++++++----------- lib/modules/storage/index.js | 30 ++++++++++++++--------------- lib/modules/swarm/index.js | 37 +++++++++++++++++++----------------- 5 files changed, 57 insertions(+), 55 deletions(-) diff --git a/lib/core/engine.js b/lib/core/engine.js index 6e9faf33f..b8fe4c5c2 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -254,17 +254,9 @@ class Engine { } storageService(_options) { - this.registerModule('storage', { - storageConfig: this.config.storageConfig, - webServerConfig: this.config.webServerConfig, - blockchainConfig: this.config.blockchainConfig, - host: _options.host, - port: _options.port, - servicesMonitor: this.servicesMonitor, - events: this.events, - logger: this.logger, - context: this.context - }); + this.registerModule('storage'); + this.registerModule('ipfs'); + this.registerModule('swarm'); } web3Service(options) { diff --git a/lib/core/plugin.js b/lib/core/plugin.js index fb01e355b..d622dbfdb 100644 --- a/lib/core/plugin.js +++ b/lib/core/plugin.js @@ -22,6 +22,7 @@ var Plugin = function(options) { this.compilers = []; this.serviceChecks = []; this.pluginTypes = []; + this.uploadCmds = []; this.imports = []; this.embarkjs_code = []; this.embarkjs_init_code = {}; @@ -173,6 +174,11 @@ Plugin.prototype.registerCompiler = function(extension, cb) { this.addPluginType('compilers'); }; +Plugin.prototype.registerUploadCommand = function(cmd, cb) { + this.uploadCmds.push({cmd: cmd, cb: cb}); + this.addPluginType('uploadCmds'); +}; + Plugin.prototype.addCodeToEmbarkJS = function(code) { this.embarkjs_code.push(code); this.addPluginType('embarkjsCode'); diff --git a/lib/modules/ipfs/index.js b/lib/modules/ipfs/index.js index 14ccb449b..76a8f51cb 100644 --- a/lib/modules/ipfs/index.js +++ b/lib/modules/ipfs/index.js @@ -18,10 +18,10 @@ class IPFS { this.webServerConfig = embark.config.webServerConfig; this.blockchainConfig = embark.config.blockchainConfig; - this.commandlineDeploy(); this.setServiceCheck(); this.addProviderToEmbarkJS(); this.addObjectToConsole(); + this.registerUploadCommand(); this._checkService((err) => { if (!err) { @@ -32,16 +32,6 @@ class IPFS { }); } - commandlineDeploy() { - let upload_ipfs = new UploadIPFS({ - buildDir: this.buildDir || 'dist/', - storageConfig: this.storageConfig.upload, - configIpfsBin: this.storageConfig.ipfs_bin || "ipfs" - }); - - this.events.setCommandHandler('storage:upload:ipfs', upload_ipfs.deploy.bind(upload_ipfs)); - } - setServiceCheck() { let self = this; @@ -154,6 +144,19 @@ class IPFS { }); } + registerUploadCommand() { + const self = this; + this.embark.registerUploadCommand('ipfs', (cb) => { + let upload_ipfs = new UploadIPFS({ + buildDir: self.buildDir || 'dist/', + storageConfig: self.storageConfig, + configIpfsBin: self.storageConfig.ipfs_bin || "ipfs" + }); + + upload_ipfs.deploy(cb); + }); + } + } module.exports = IPFS; diff --git a/lib/modules/storage/index.js b/lib/modules/storage/index.js index d4bf90699..5ac7712e2 100644 --- a/lib/modules/storage/index.js +++ b/lib/modules/storage/index.js @@ -1,35 +1,33 @@ -const _ = require('underscore'); - -const IpfsModule = require('../ipfs'); -const SwarmModule = require('../swarm'); class Storage { - constructor(embark, options){ + constructor(embark, _options){ this.embark = embark; this.storageConfig = embark.config.storageConfig; if (!this.storageConfig.enabled) return; + this.handleUploadCommand(); this.addSetProviders(); + } - new IpfsModule(embark, options); /*eslint no-new: "off"*/ - new SwarmModule(embark, options); /*eslint no-new: "off"*/ + handleUploadCommand() { + const self = this; + this.embark.events.setCommandHandler('storage:upload', (cb) => { + let platform = self.storageConfig.upload.provider; - embark.events.setCommandHandler('storage:upload', (cb) => { - let platform = options.storageConfig.upload.provider; - - if (['swarm', 'ipfs'].indexOf(platform) === -1) { - return cb({message: __('platform "{{platform}}" is specified as the upload provider, however no plugins have registered an upload command for "{{platform}}".', {platform: platform})}); + let uploadCmds = self.embark.getPluginsProperty('uploadCmds', 'uploadCmds'); + for (let uploadCmd of uploadCmds) { + if (uploadCmd.cmd === platform) { + return uploadCmd.cb.call(uploadCmd.cb, cb); + } } - embark.events.request("storage:upload:" + platform, cb); + cb({message: __('platform "{{platform}}" is specified as the upload provider, however no plugins have registered an upload command for "{{platform}}".', {platform: platform})}); }); } addSetProviders() { - const self = this; - - let code = `\nEmbarkJS.Storage.setProviders(${JSON.stringify(this.storageConfig.dappConnection)});`; + let code = `\nEmbarkJS.Storage.setProviders(${JSON.stringify(this.storageConfig.dappConnection || [])});`; let shouldInit = (storageConfig) => { return storageConfig.enabled; diff --git a/lib/modules/swarm/index.js b/lib/modules/swarm/index.js index 90057d92b..1de8c0234 100644 --- a/lib/modules/swarm/index.js +++ b/lib/modules/swarm/index.js @@ -10,25 +10,25 @@ class Swarm { constructor(embark, options) { this.logger = embark.logger; this.events = embark.events; - this.buildDir = options.buildDir; + this.buildDir = embark.config.buildDir; this.storageConfig = embark.config.storageConfig; - this.host = options.host || this.storageConfig.host; - this.port = options.port || this.storageConfig.port; + this.host = this.storageConfig.host; + this.port = this.storageConfig.port; this.embark = embark; this.webServerConfig = embark.config.webServerConfig; this.blockchainConfig = embark.config.blockchainConfig; - this.providerUrl = utils.buildUrl(options.protocol || options.storageConfig.upload.protocol, options.host || options.storageConfig.upload.host, options.port || options.storageConfig.upload.port); + this.providerUrl = utils.buildUrl(this.storageConfig.upload.protocol, this.storageConfig.upload.host, this.storageConfig.upload.port); - this.getUrl = options.storageConfig.upload.getUrl || this.providerUrl + '/bzz:/'; + this.getUrl = this.storageConfig.upload.getUrl || this.providerUrl + '/bzz:/'; this.bzz = new Web3Bzz(this.providerUrl); - this.commandlineDeploy(); this.setServiceCheck(); this.addProviderToEmbarkJS(); this.startProcess(() => {}); + this.registerUploadCommand(); this._checkService((err) => { if (!err) { @@ -39,17 +39,6 @@ class Swarm { }); } - commandlineDeploy() { - this.upload_swarm = new UploadSwarm({ - buildDir: this.buildDir || 'dist/', - storageConfig: this.storageConfig, - getUrl: this.getUrl, - bzz: this.bzz - }); - - this.events.setCommandHandler('storage:upload:swarm', this.upload_swarm.deploy.bind(this.upload_swarm)); - } - setServiceCheck() { let self = this; @@ -124,6 +113,20 @@ class Swarm { }); } + registerUploadCommand(cb) { + const self = this; + this.embark.registerUploadCommand('ipfs', () => { + let upload_swarm = new UploadSwarm({ + buildDir: self.buildDir || 'dist/', + storageConfig: self.storageConfig, + getUrl: self.getUrl, + bzz: self.bzz + }); + + upload_swarm.deploy(cb); + }); + } + } module.exports = Swarm;