2018-05-28 12:59:18 +00:00
|
|
|
|
|
|
|
let utils = require('../../utils/utils.js');
|
|
|
|
let fs = require('../../core/fs.js');
|
|
|
|
let _ = require('underscore');
|
|
|
|
|
|
|
|
class Storage {
|
|
|
|
constructor(embark, options){
|
|
|
|
this._storageConfig = options.storageConfig;
|
|
|
|
|
|
|
|
let storageProviderCls = require(`../${this._storageConfig.upload.provider}/index.js`);
|
2018-05-28 12:59:18 +00:00
|
|
|
this._uploadProvider = new storageProviderCls(embark, this._storageConfig); /*eslint no-new: "off"*/
|
2018-05-28 12:59:18 +00:00
|
|
|
|
2018-05-28 12:59:18 +00:00
|
|
|
if(typeof this._uploadProvider.initProvider == 'function') this._uploadProvider.initProvider();
|
|
|
|
if(typeof this._uploadProvider.commandlineDeploy == 'function') this._uploadProvider.commandlineDeploy();
|
|
|
|
if(typeof this._uploadProvider.setServiceCheck == 'function') this._uploadProvider.setServiceCheck();
|
|
|
|
if(typeof this._uploadProvider.addObjectToConsole == 'function') this._uploadProvider.addObjectToConsole();
|
2018-05-28 12:59:18 +00:00
|
|
|
|
2018-05-28 12:59:18 +00:00
|
|
|
// loop through all available providers and add "register provider" code in EmbarkJS
|
|
|
|
// which allows the provider to be set in the DApp
|
2018-05-28 12:59:18 +00:00
|
|
|
this._storageConfig.available_providers.forEach(providerStr => {
|
2018-05-28 12:59:18 +00:00
|
|
|
let storageProviderCls = require(`../${providerStr}/index.js`);
|
|
|
|
this._storageProvider = new storageProviderCls(this.storageConfig); /*eslint no-new: "off"*/
|
|
|
|
if(typeof this._storageProvider.addProviderToEmbarkJS == 'function') this._storageProvider.addProviderToEmbarkJS();
|
2018-05-28 12:59:18 +00:00
|
|
|
});
|
|
|
|
|
2018-05-28 12:59:18 +00:00
|
|
|
// add the code to call setProviders in embarkjs
|
2018-05-28 12:59:18 +00:00
|
|
|
this.addSetProviders();
|
|
|
|
}
|
|
|
|
|
|
|
|
addSetProviders() {
|
|
|
|
// TODO: make this a shouldAdd condition
|
|
|
|
if (this._storageConfig === {} || !this._storageConfig.dappConnection || !this._storageConfig.dappConnection.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// filter list of dapp connections based on available_providers set in config
|
|
|
|
let hasSwarm = _.contains(this._storageConfig.available_providers, 'swarm'); // don't need to eval this in every loop iteration
|
|
|
|
let connectionsToSet = _.filter(this._storageConfig.dappConnection, (conn) => {
|
|
|
|
return _.contains(this._storageConfig.available_providers, conn.provider) || (conn === '$BZZ' && hasSwarm);
|
|
|
|
});
|
|
|
|
|
2018-05-28 12:59:18 +00:00
|
|
|
let code = "";
|
|
|
|
code += "\n" + fs.readFileSync(utils.joinPath(__dirname, 'embarkjs.js')).toString();
|
|
|
|
code += `\n__embarkStorage.setProviders(${JSON.stringify(connectionsToSet)}));`;
|
2018-05-28 12:59:18 +00:00
|
|
|
|
2018-05-28 12:59:18 +00:00
|
|
|
this.embark.addCodeToEmbarkJS(code);
|
2018-05-28 12:59:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Storage;
|