move ipfs embarkjs code to module

This commit is contained in:
Iuri Matias 2017-12-28 12:16:50 -05:00
parent 4976408c11
commit e833ebd019
4 changed files with 34 additions and 9 deletions

View File

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

View File

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

View File

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

View File

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