From 3d36aaaeb37a5cb4f2c0d7a48e4ebbc81bf87719 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 8 Dec 2017 10:11:18 -0500 Subject: [PATCH] support both old system and imports in the pipeline --- lib/core/config.js | 151 ++++++++++++++++++++----------- test_app/app/js/index.js | 10 --- test_app/app/js/new_index.js | 166 +++++++++++++++++++++++++++++++++++ test_app/app/new_index.html | 107 ++++++++++++++++++++++ test_app/embark.json | 4 +- 5 files changed, 377 insertions(+), 61 deletions(-) create mode 100644 test_app/app/js/new_index.js create mode 100644 test_app/app/new_index.html diff --git a/lib/core/config.js b/lib/core/config.js index a71a72ee..cb1003ab 100644 --- a/lib/core/config.js +++ b/lib/core/config.js @@ -227,7 +227,11 @@ Config.prototype.loadEmbarkConfigFile = function() { Config.prototype.loadPipelineConfigFile = function() { var assets = this.embarkConfig.app; for(var targetFile in assets) { - this.assetFiles[targetFile] = this.loadFiles(assets[targetFile]); + if (typeof(assets[targetFile]) === 'string' || (Array.isArray(assets[targetFile]) && (assets[targetFile].length === 1))) { + this.assetFiles[targetFile] = this.loadFile(assets[targetFile]); + } else { + this.assetFiles[targetFile] = this.loadFiles(assets[targetFile]); + } } }; @@ -245,6 +249,101 @@ Config.prototype.loadChainTrackerFile = function() { this.chainTracker = chainTracker; }; +Config.prototype.loadFile = function(files) { + var self = this; + var originalFiles = utils.filesMatchingPattern(files); + var readFiles = []; + + // get plugins + var filesFromPlugins = []; + + var filePlugins = self.plugins.getPluginsFor('pipelineFiles'); + + if (filePlugins.length > 0) { + filePlugins.forEach(function(plugin) { + try { + var fileObjects = plugin.runFilePipeline(); + for (var i=0; i < fileObjects.length; i++) { + var fileObject = fileObjects[i]; + filesFromPlugins.push(fileObject); + } + } + catch(err) { + self.logger.error(err.message); + } + }); + } + + filesFromPlugins.filter(function(file) { + if (utils.fileMatchesPattern(files, file.intendedPath)) { + readFiles.push(file); + } + }); + + // get user files + originalFiles.filter(function(file) { + return file.indexOf('.') >= 0; + }).filter(function(file) { + if (file === 'embark.js') { + return; + } else if (file.indexOf("web3-") === 0) { + return; + } else if (file.indexOf("web3") === 0) { + return; + //} else if (file === 'abi.js') { + // readFiles.push({filename: file, content: "", path: file}); + } else { + + if (file.indexOf('.js') >= 0) { + //if (file.indexOf('.js') >= 0) { + readFiles.push(new File({filename: file, type: "custom", path: file, resolver: function(callback) { + console.log("---"); + console.log(fs.dappPath()); + console.log(file); + console.log("---"); + webpack({ + entry: utils.joinPath(fs.dappPath(), file), + output: { + //libraryTarget: 'umd', + path: utils.joinPath(fs.dappPath(), '.embark'), + //filename: 'my-first-webpack.bundle.js' + filename: file + }, + externals: function(context, request, callback) { + if (request == "MyLib") { + console.log("MyLib Detected!"); + return callback(null, "{foo: 'hello'}"); + } + callback(); + }, + module: { + rules: [ + { + test: /\.css$/, + use: [ + { loader: "style-loader" }, + { loader: "css-loader" } + ] + } + ] + } + }).run((err, stats) => { + console.log(err, stats); + //return callback(fs.readFileSync('./.embark/my-first-webpack.bundle.js').toString()); + return callback(fs.readFileSync('./.embark/' + file).toString()); + }); + }})); + } else { + readFiles.push(new File({filename: file, type: "dapp_file", path: file})); + } + + } + + }); + + return readFiles; +}; + Config.prototype.loadFiles = function(files) { var self = this; var originalFiles = utils.filesMatchingPattern(files); @@ -335,55 +434,7 @@ Config.prototype.loadFiles = function(files) { //} else if (file === 'abi.js') { // readFiles.push({filename: file, content: "", path: file}); } else { - - if (file.indexOf('index.js') >= 0) { - //if (file.indexOf('.js') >= 0) { - readFiles.push(new File({filename: file, type: "custom", path: file, resolver: function(callback) { - console.log("---"); - console.log(fs.dappPath()); - console.log(file); - console.log("---"); - webpack({ - entry: utils.joinPath(fs.dappPath(), file), - output: { - //libraryTarget: 'umd', - path: utils.joinPath(fs.dappPath(), '.embark'), - //filename: 'my-first-webpack.bundle.js' - filename: file - }, - externals: function(context, request, callback) { - if (request == "MyLib") { - console.log("MyLib Detected!"); - return callback(null, "{foo: 'hello'}"); - } - callback(); - }, - module: { - rules: [ - { - test: /\.css$/, - use: [ - { loader: "style-loader" }, - { loader: "css-loader" } - ] - } - ] - } - }).run((err, stats) => { - console.log(err, stats); - //return callback(fs.readFileSync('./.embark/my-first-webpack.bundle.js').toString()); - return callback(fs.readFileSync('./.embark/' + file).toString()); - }); - }})); - } else { - readFiles.push(new File({filename: file, type: "dapp_file", path: file})); - } - - //if (file.indexOf('.') >= 0) { - // readFiles.push(new File({filename: file, type: "dapp_file", path: file})); - //} else if (file[0] === '$') { - // //readFiles.push(new File({filename: file, type: 'embark_internal', path: file})); - //} + readFiles.push(new File({filename: file, type: "dapp_file", path: file})); } }); diff --git a/test_app/app/js/index.js b/test_app/app/js/index.js index caa255cb..cbf16091 100644 --- a/test_app/app/js/index.js +++ b/test_app/app/js/index.js @@ -1,12 +1,5 @@ /*globals $, SimpleStorage, document*/ -import Test2 from 'MyLib'; - -import test3 from './foo'; -import './foo.css'; - -window.test_3 = test3; - var addToLog = function(id, txt) { $(id + " .logs").append("
" + txt); }; @@ -15,9 +8,6 @@ var addToLog = function(id, txt) { // Blockchain example // =========================== $(document).ready(function() { - console.log([1,2,3].map(v => v + 1)); - alert('hello'); - window.test_2 = Test2; $("#blockchain button.set").click(function() { var value = parseInt($("#blockchain input.text").val(), 10); diff --git a/test_app/app/js/new_index.js b/test_app/app/js/new_index.js new file mode 100644 index 00000000..64fe318d --- /dev/null +++ b/test_app/app/js/new_index.js @@ -0,0 +1,166 @@ +/*globals $, SimpleStorage, document*/ + +import $ from './_vendor/jquery.min'; +//import EmbarkJS from 'Embark/EmbarkJS'; + +import Test2 from 'MyLib'; + +import test3 from './foo'; +import './foo.css'; + +window.test_3 = test3; + +var addToLog = function(id, txt) { + $(id + " .logs").append("
" + txt); +}; + +// =========================== +// Blockchain example +// =========================== +$(document).ready(function() { + console.log([1,2,3].map(v => v + 1)); + alert('hello'); + window.test_2 = Test2; + + $("#blockchain button.set").click(function() { + var value = parseInt($("#blockchain input.text").val(), 10); + + // If web3.js 1.0 is being used + if (EmbarkJS.isNewWeb3()) { + SimpleStorage.methods.set(value).send({from: web3.eth.defaultAccount}); + addToLog("#blockchain", "SimpleStorage.methods.set(value).send({from: web3.eth.defaultAccount})"); + } else { + SimpleStorage.set(value); + addToLog("#blockchain", "SimpleStorage.set(" + value + ")"); + } + + }); + + $("#blockchain button.get").click(function() { + // If web3.js 1.0 is being used + if (EmbarkJS.isNewWeb3()) { + SimpleStorage.methods.get().call(function(err, value) { + $("#blockchain .value").html(value); + }); + addToLog("#blockchain", "SimpleStorage.methods.get(console.log)"); + } else { + SimpleStorage.get().then(function(value) { + $("#blockchain .value").html(value.toNumber()); + }); + addToLog("#blockchain", "SimpleStorage.get()"); + } + }); + +}); + +// =========================== +// 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 +// =========================== +$(document).ready(function() { + + $("#communication .error").hide(); + $("#communication .errorVersion").hide(); + if (EmbarkJS.Messages.providerName === 'whisper') { + EmbarkJS.Messages.getWhisperVersion(function(err, version) { + if (err) { + $("#communication .error").show(); + $("#communication-controls").hide(); + $("#status-communication").addClass('status-offline'); + } else { + EmbarkJS.Messages.setProvider('whisper'); + $("#status-communication").addClass('status-online'); + } + }); + } + + $("#communication button.listenToChannel").click(function() { + var channel = $("#communication .listen input.channel").val(); + $("#communication #subscribeList").append("
subscribed to " + channel + " now try sending a message"); + EmbarkJS.Messages.listenTo({topic: [channel]}).then(function(message) { + $("#communication #messagesList").append("
channel: " + channel + " message: " + message); + }); + addToLog("#communication", "EmbarkJS.Messages.listenTo({topic: ['" + channel + "']}).then(function(message) {})"); + }); + + $("#communication button.sendMessage").click(function() { + var channel = $("#communication .send input.channel").val(); + var message = $("#communication .send input.message").val(); + EmbarkJS.Messages.sendMessage({topic: channel, data: message}); + addToLog("#communication", "EmbarkJS.Messages.sendMessage({topic: '" + channel + "', data: '" + message + "'})"); + }); + +}); diff --git a/test_app/app/new_index.html b/test_app/app/new_index.html new file mode 100644 index 00000000..1bf3a925 --- /dev/null +++ b/test_app/app/new_index.html @@ -0,0 +1,107 @@ + + + Embark - SimpleStorage Demo + + + + +

Embark - Usage Example

+ + + +
+
+

1. Set the value in the blockchain

+
+ + +

Once you set the value, the transaction will need to be mined and then the value will be updated on the blockchain.

+
+ +

2. Get the current value

+
+
+ current value is +
+ +

Click the button to get the current value. The initial value is 100.

+
+ +

3. Contract Calls

+

Javascript calls being made:

+
+
+
+
+ +
+ +

Save text to IPFS

+
+ + +

generated Hash:

+
+ +

Load text from IPFS given an hash

+
+ + +

result:

+
+ +

upload file to ipfs

+
+ + +

generated hash:

+
+ +

Get file or image from ipfs

+
+ + +

file available at:

+

+
+ +

Javascript calls being made:

+
+
EmbarkJS.Storage.setProvider('ipfs',{server: 'localhost', port: '5001'}) +
+
+
+
+ + +
+

Listen To channel

+
+ + +
+

messages received:

+

+
+ +

Send Message

+
+ + + +
+ +

Javascript calls being made:

+
+
EmbarkJS.Messages.setProvider('whisper') +
+
+
+
+ + + diff --git a/test_app/embark.json b/test_app/embark.json index 2f08464d..57b46ad2 100644 --- a/test_app/embark.json +++ b/test_app/embark.json @@ -4,12 +4,14 @@ "js/webpack_test.js": "app/js/index.js", "css/app.css": ["app/css/**"], "images/": ["app/images/**"], - "js/app.js": ["embark.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"], + "js/app.js": ["embark.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"], + "js/new_app.js": ["app/js/new_index.js"], "js/embark.js": ["embark.js"], "js/abi.js": "abi.js", "js/test.js": ["embark.js", "app/js/_vendor/jquery.min.js", "app/js/_vendor/async.min.js", "app/js/test.js"], "js/mytoken.js": ["$MyToken", "app/js/token_test.js"], "index.html": "app/index.html", + "new_index.html": "app/new_index.html", "test.html": "app/test.html", "test2.html": "app/test2.html", "js/myweb3.js": "web3-0.18.js",