/ lib / modules / storage / index.js
index.js
 1  
 2  class Storage {
 3    constructor(embark, options){
 4      this.embark = embark;
 5      this.storageConfig = embark.config.storageConfig;
 6      this.plugins = options.plugins;
 7  
 8      if (!this.storageConfig.enabled) return;
 9  
10      this.handleUploadCommand();
11      this.addSetProviders();
12    }
13  
14    handleUploadCommand() {
15      const self = this;
16      this.embark.events.setCommandHandler('storage:upload', (cb) => {
17        let platform = self.storageConfig.upload.provider;
18  
19        let uploadCmds = self.plugins.getPluginsProperty('uploadCmds', 'uploadCmds');
20        for (let uploadCmd of uploadCmds) {
21          if (uploadCmd.cmd === platform) {
22            return uploadCmd.cb.call(uploadCmd.cb, cb);
23          }
24        }
25  
26        cb({message: __('platform "{{platform}}" is specified as the upload provider, however no plugins have registered an upload command for "{{platform}}".', {platform: platform})});
27      });
28    }
29  
30    addSetProviders() {
31      let code = `\nEmbarkJS.Storage.setProviders(${JSON.stringify(this.storageConfig.dappConnection || [])});`;
32  
33      let shouldInit = (storageConfig) => {
34        return storageConfig.enabled;
35      };
36  
37      this.embark.addProviderInit('storage', code, shouldInit);
38      this.embark.addConsoleProviderInit('storage', code, shouldInit);
39    }
40  
41  }
42  
43  module.exports = Storage;