diff --git a/js/embark.js b/js/embark.js index 914590ad3..06c05462d 100644 --- a/js/embark.js +++ b/js/embark.js @@ -213,6 +213,8 @@ EmbarkJS.Storage.getUrl = function(hash) { }; EmbarkJS.Storage.setProvider = function(provider, options) { + console.log("setting provider"); + console.log(arguments); var self = this; var promise = new Promise(function(resolve, reject) { if (provider.toLowerCase() === EmbarkJS.Storage.Providers.IPFS) { @@ -229,8 +231,10 @@ EmbarkJS.Storage.setProvider = function(provider, options) { self.ipfsConnection = IpfsApi(options.server, options.port); self._getUrl = options.getUrl || "http://localhost:8080/ipfs/"; } + window.ipfsConnection = self.ipfsConnection; resolve(self); } catch (err) { + console.log(err); self.ipfsConnection = null; reject(new Error('Failed to connect to IPFS')); } @@ -673,4 +677,6 @@ EmbarkJS.Utils = { } }; -module.exports = EmbarkJS; +//module.exports = EmbarkJS; + +export default EmbarkJS; diff --git a/lib/contracts/code_generator.js b/lib/contracts/code_generator.js index 64c38c0ee..e680382bb 100644 --- a/lib/contracts/code_generator.js +++ b/lib/contracts/code_generator.js @@ -1,3 +1,6 @@ +let fs = require('../core/fs.js'); +var utils = require('../utils/utils.js'); + require('ejs'); const Templates = { utils: require('./code_templates/utils.js.ejs'), @@ -263,6 +266,28 @@ class CodeGenerator { return contracts; } + + buildEmbarkJS() { + let embarkjsCode = fs.readFileSync(fs.embarkPath('js/embark.js')).toString(); + let code = ""; + + 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 'Embark/libs/IpfsApi';\n"; + code += "\nimport IpfsApi from 'ipfs-api';\n"; + } + + code += "\n" + embarkjsCode + "\n"; + + code += this.generateCommunicationInitialization(true); + code += this.generateStorageInitialization(true); + + let filePath = utils.joinPath(fs.dappPath(), ".embark", 'embark.js'); + fs.mkdirpSync(utils.joinPath(fs.dappPath(), ".embark")); + fs.writeFileSync(filePath, code); + } } module.exports = CodeGenerator; diff --git a/lib/core/engine.js b/lib/core/engine.js index a07de2334..7449c4ab0 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -114,6 +114,7 @@ class Engine { events: self.events }); codeGenerator.listenToCommands(); + codeGenerator.buildEmbarkJS(); self.events.emit('code-generator-ready'); }; diff --git a/lib/pipeline/pipeline.js b/lib/pipeline/pipeline.js index d2c9a080d..fd97a0f65 100644 --- a/lib/pipeline/pipeline.js +++ b/lib/pipeline/pipeline.js @@ -23,7 +23,6 @@ class Pipeline { this.buildContracts(contractsJSON); - self.buildWeb3JS(function() { // limit:1 due to issues when downloading required files such as web3.js @@ -36,8 +35,10 @@ class Pipeline { if (file.filename.indexOf('.js') >= 0) { let importsList = {}; - importsList["Embark/EmbarkJS"] = fs.embarkPath("js/embark.js"); + //importsList["Embark/EmbarkJS"] = fs.embarkPath("js/embark.js"); + importsList["Embark/EmbarkJS"] = utils.joinPath(fs.dappPath(), ".embark", 'embark.js'); importsList["Embark/web3"] = utils.joinPath(fs.dappPath(), ".embark", 'web3_instance.js'); + importsList["Embark/libs/IpfsApi"] = fs.embarkPath('js/ipfs.js'); async.waterfall([ function findImports(next) { @@ -93,6 +94,7 @@ class Pipeline { externals: function(context, request, callback) { if (request === "Embark/contracts/all") { return callback(null, fs.readFileSync(utils.joinPath(fs.dappPath(), '.embark', 'embark.js'))); + // should be .toString() ? } callback(); }, diff --git a/package.json b/package.json index f5f4258ac..6fbe8b0d0 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ "follow-redirects": "^1.2.4", "fs-extra": "^2.0.0", "globule": "^1.1.0", - "ipfs-api": "^14.0.3", + "ipfs-api": "6.0.2", "merge": "^1.2.0", "mocha": "^2.2.5", "orbit-db": "^0.17.3", diff --git a/test_app/app/js/new_index.js b/test_app/app/js/new_index.js index 9203ca537..cb1b7ba4f 100644 --- a/test_app/app/js/new_index.js +++ b/test_app/app/js/new_index.js @@ -2,27 +2,13 @@ import $ from './_vendor/jquery.min'; //import web3 from 'Embark/web3'; -//console.log("new_index"); -//console.log(web3); -//window.web3 = web3; -//console.log("finished importing web3"); import EmbarkJS from 'Embark/EmbarkJS'; +window.EmbarkJS = EmbarkJS; import SimpleStorage from 'Embark/contracts/SimpleStorage'; -//import web3 from 'Embark/web3'; - -console.log("SimpleStorage") -console.log(SimpleStorage); - -//console.log(EmbarkJS); -window.SimpleStorage = SimpleStorage; import test3 from './foo'; import './foo.css'; -console.log(test3); - -////window.test_3 = test3; -//// var addToLog = function(id, txt) { $(id + " .logs").append("
" + txt); }; @@ -32,7 +18,7 @@ var addToLog = function(id, txt) { // =========================== $(document).ready(function() { console.log([1,2,3].map(v => v + 1)); - alert('hello'); + //alert('hello'); $("#blockchain button.set").click(function() { var value = parseInt($("#blockchain input.text").val(), 10); @@ -65,80 +51,80 @@ $(document).ready(function() { }); -////// =========================== -////// Storage (IPFS) example -////// =========================== -////$(document).ready(function() { -//// // automatic set if config/storage.json has "enabled": true and "provider": "ipfs" -//// //EmbarkJS.Storage.setProvider('ipfs',{server: 'localhost', port: '5001'}); -//// -//// $("#storage .error").hide(); -//// EmbarkJS.Storage.ipfsConnection.ping() -//// .then(function(){ -//// $("#status-storage").addClass('status-online'); -//// $("#storage-controls").show(); -//// }) -//// .catch(function(err) { -//// if(err){ -//// console.log("IPFS Connection Error => " + err.message); -//// $("#storage .error").show(); -//// $("#status-storage").addClass('status-offline'); -//// $("#storage-controls").hide(); -//// } -//// }); -//// -//// $("#storage button.setIpfsText").click(function() { -//// var value = $("#storage input.ipfsText").val(); -//// EmbarkJS.Storage.saveText(value).then(function(hash) { -//// $("span.textHash").html(hash); -//// $("input.textHash").val(hash); -//// addToLog("#storage", "EmbarkJS.Storage.saveText('" + value + "').then(function(hash) { })"); -//// }) -//// .catch(function(err) { -//// if(err){ -//// console.log("IPFS saveText Error => " + err.message); -//// } -//// }); -//// }); -//// -//// $("#storage button.loadIpfsHash").click(function() { -//// var value = $("#storage input.textHash").val(); -//// EmbarkJS.Storage.get(value).then(function(content) { -//// $("span.ipfsText").html(content); -//// addToLog("#storage", "EmbarkJS.Storage.get('" + value + "').then(function(content) { })"); -//// }) -//// .catch(function(err) { -//// if(err){ -//// console.log("IPFS get Error => " + err.message); -//// } -//// }); -//// }); -//// -//// $("#storage button.uploadFile").click(function() { -//// var input = $("#storage input[type=file]"); -//// EmbarkJS.Storage.uploadFile(input).then(function(hash) { -//// $("span.fileIpfsHash").html(hash); -//// $("input.fileIpfsHash").val(hash); -//// addToLog("#storage", "EmbarkJS.Storage.uploadFile($('input[type=file]')).then(function(hash) { })"); -//// }) -//// .catch(function(err) { -//// if(err){ -//// console.log("IPFS uploadFile Error => " + err.message); -//// } -//// }); -//// }); -//// -//// $("#storage button.loadIpfsFile").click(function() { -//// var hash = $("#storage input.fileIpfsHash").val(); -//// var url = EmbarkJS.Storage.getUrl(hash); -//// var link = '' + url + ''; -//// $("span.ipfsFileUrl").html(link); -//// $(".ipfsImage").attr('src', url); -//// addToLog("#storage", "EmbarkJS.Storage.getUrl('" + hash + "')"); -//// }); -//// -////}); -//// +// =========================== +// Storage (IPFS) example +// =========================== +$(document).ready(function() { + // automatic set if config/storage.json has "enabled": true and "provider": "ipfs" + //EmbarkJS.Storage.setProvider('ipfs',{server: 'localhost', port: '5001'}); + + $("#storage .error").hide(); + EmbarkJS.Storage.ipfsConnection.ping() + .then(function(){ + $("#status-storage").addClass('status-online'); + $("#storage-controls").show(); + }) + .catch(function(err) { + if(err){ + console.log("IPFS Connection Error => " + err.message); + $("#storage .error").show(); + $("#status-storage").addClass('status-offline'); + $("#storage-controls").hide(); + } + }); + + $("#storage button.setIpfsText").click(function() { + var value = $("#storage input.ipfsText").val(); + EmbarkJS.Storage.saveText(value).then(function(hash) { + $("span.textHash").html(hash); + $("input.textHash").val(hash); + addToLog("#storage", "EmbarkJS.Storage.saveText('" + value + "').then(function(hash) { })"); + }) + .catch(function(err) { + if(err){ + console.log("IPFS saveText Error => " + err.message); + } + }); + }); + + $("#storage button.loadIpfsHash").click(function() { + var value = $("#storage input.textHash").val(); + EmbarkJS.Storage.get(value).then(function(content) { + $("span.ipfsText").html(content); + addToLog("#storage", "EmbarkJS.Storage.get('" + value + "').then(function(content) { })"); + }) + .catch(function(err) { + if(err){ + console.log("IPFS get Error => " + err.message); + } + }); + }); + + $("#storage button.uploadFile").click(function() { + var input = $("#storage input[type=file]"); + EmbarkJS.Storage.uploadFile(input).then(function(hash) { + $("span.fileIpfsHash").html(hash); + $("input.fileIpfsHash").val(hash); + addToLog("#storage", "EmbarkJS.Storage.uploadFile($('input[type=file]')).then(function(hash) { })"); + }) + .catch(function(err) { + if(err){ + console.log("IPFS uploadFile Error => " + err.message); + } + }); + }); + + $("#storage button.loadIpfsFile").click(function() { + var hash = $("#storage input.fileIpfsHash").val(); + var url = EmbarkJS.Storage.getUrl(hash); + var link = '' + url + ''; + $("span.ipfsFileUrl").html(link); + $(".ipfsImage").attr('src', url); + addToLog("#storage", "EmbarkJS.Storage.getUrl('" + hash + "')"); + }); + +}); + ////// =========================== ////// Communication (Whisper) example ////// =========================== diff --git a/test_app/app/new_index.html b/test_app/app/new_index.html index 3fc0714eb..8d672cef1 100644 --- a/test_app/app/new_index.html +++ b/test_app/app/new_index.html @@ -2,7 +2,8 @@ Embark - SimpleStorage Demo - + + diff --git a/test_app/embark.json b/test_app/embark.json index 6812e8990..bc7382414 100644 --- a/test_app/embark.json +++ b/test_app/embark.json @@ -2,6 +2,8 @@ "contracts": ["app/contracts/**"], "app": { "js/webpack_test.js": "app/js/index.js", + "js/jquery.js": "app/js/_vendor/jquery.min.js", + "js/bootstrap.js": "app/js/_vendor/bootstrap.min.js", "css/app.css": ["app/css/**"], "images/": ["app/images/**"], "js/app.js": ["app/js/_vendor/jquery.min.js", "app/js/_vendor/bootstrap.min.js", "app/js/**", "!app/js/test.js", "!app/js/foo.js", "!app/js/foo.css", "!app/js/new_index.js"],