re-add register upload cmd api; refactor storage module initialization

This commit is contained in:
Iuri Matias 2018-07-08 20:40:06 +03:00
parent c3fc614cd8
commit df97a6ac00
5 changed files with 57 additions and 55 deletions

View File

@ -253,17 +253,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) {

View File

@ -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');

View File

@ -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;

View File

@ -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"*/
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})});
}
embark.events.request("storage:upload:" + platform, cb);
handleUploadCommand() {
const self = this;
this.embark.events.setCommandHandler('storage:upload', (cb) => {
let platform = self.storageConfig.upload.provider;
let uploadCmds = self.embark.getPluginsProperty('uploadCmds', 'uploadCmds');
for (let uploadCmd of uploadCmds) {
if (uploadCmd.cmd === platform) {
return uploadCmd.cb.call(uploadCmd.cb, 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;

View File

@ -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;