From e8ef3ed8b7ef07a03d4ea7b8107b31dbbe4bdc66 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 7 Jan 2017 13:32:32 -0500 Subject: [PATCH 1/4] update to web3js 0.18.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 03b868b02..ed6a34895 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "serve-static": "^1.11.1", "shelljs": "^0.5.0", "toposort": "^0.2.10", - "web3": "^0.15.0", + "web3": "^0.18.0", "wrench": "^1.5.8" }, "author": "Iuri Matias ", From 15b8d9b33eb96a2695d2d5b8555565779eed68d1 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 7 Jan 2017 16:52:29 -0500 Subject: [PATCH 2/4] supporting uploading dapp to swarm --- lib/cmd.js | 12 ++++++++++++ lib/index.js | 7 +++++++ lib/ipfs.js | 5 +++-- lib/swarm.js | 29 +++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 lib/swarm.js diff --git a/lib/cmd.js b/lib/cmd.js index b314b714c..4e8fc629c 100644 --- a/lib/cmd.js +++ b/lib/cmd.js @@ -15,6 +15,7 @@ Cmd.prototype.process = function(args) { this.simulator(); this.test(); this.ipfs(); + this.swarm(); this.otherCommands(); program.parse(args); }; @@ -137,6 +138,7 @@ Cmd.prototype.test = function() { }); }; +// TODO: replace both of this with a deploy/upload command Cmd.prototype.ipfs = function() { var self = this; program @@ -147,6 +149,16 @@ Cmd.prototype.ipfs = function() { }); }; +Cmd.prototype.swarm = function() { + var self = this; + program + .command('swarm') + .description('deploy to SWARM') + .action(function() { + self.Embark.swarm(); + }); +}; + Cmd.prototype.otherCommands = function() { program .action(function(env){ diff --git a/lib/index.js b/lib/index.js index 55d689f33..5a2d63431 100644 --- a/lib/index.js +++ b/lib/index.js @@ -23,6 +23,7 @@ var Monitor = require('./monitor.js'); var ServicesMonitor = require('./services.js'); var Console = require('./console.js'); var IPFS = require('./ipfs.js'); +var Swarm = require('./swarm.js'); var Embark = { @@ -304,6 +305,12 @@ var Embark = { ipfs: function() { var ipfs = new IPFS({buildDir: 'dist/'}); ipfs.deploy(); + }, + + // TODO: should deploy if it hasn't already + swarm: function() { + var swarm = new Swarm({buildDir: 'dist/'}); + swarm.deploy(); } }; diff --git a/lib/ipfs.js b/lib/ipfs.js index 6333c2d6c..38b2bfed2 100644 --- a/lib/ipfs.js +++ b/lib/ipfs.js @@ -13,8 +13,9 @@ IPFS.prototype.deploy = function() { ipfs_bin = "~/go/bin/ipfs"; } - var cmd = ipfs_bin + " add -r " + build_dir; - console.log(("=== adding " + cmd + " to ipfs").green); + var cmd = ipfs_bin + " add -r " + this.buildDir; + console.log(("=== adding " + this.buildDir + " to ipfs").green); + console.log(cmd.green); var result = exec(cmd); var rows = result.output.split("\n"); diff --git a/lib/swarm.js b/lib/swarm.js new file mode 100644 index 000000000..a7f6cc752 --- /dev/null +++ b/lib/swarm.js @@ -0,0 +1,29 @@ +var colors = require('colors'); + +var Swarm = function(options) { + this.options = options; + this.buildDir = options.buildDir || 'dist/'; +}; + +Swarm.prototype.deploy = function() { + var swarm_bin = exec('which swarm').output.split("\n")[0]; + + if (swarm_bin==='swarm not found' || swarm_bin === ''){ + console.log('=== WARNING: Swarm not in an executable path. Guessing ~/go/bin/swarm for path'.yellow); + swarm_bin = "~/go/bin/swarm"; + } + + var cmd = swarm_bin + " --defaultpath " + this.buildDir + "index.html --recursive up " + this.buildDir; + console.log(("=== adding " + this.buildDir + " to swarm").green); + console.log(cmd.green); + + var result = exec(cmd); + var rows = result.output.split("\n"); + var dir_hash = rows.reverse()[1]; + + console.log(("=== DApp available at http://localhost:8500/bzz:/" + dir_hash + "/").green); +}; + +module.exports = Swarm; + + From c53d92baa5e9575b9a310084ab172d943010a9c1 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 7 Jan 2017 20:37:48 -0500 Subject: [PATCH 3/4] refactor deployments to upload command --- lib/cmd.js | 28 ++++++++++------------------ lib/index.js | 20 +++++++++++--------- 2 files changed, 21 insertions(+), 27 deletions(-) diff --git a/lib/cmd.js b/lib/cmd.js index 4e8fc629c..9df0fdd79 100644 --- a/lib/cmd.js +++ b/lib/cmd.js @@ -14,8 +14,7 @@ Cmd.prototype.process = function(args) { this.blockchain(); this.simulator(); this.test(); - this.ipfs(); - this.swarm(); + this.upload(); this.otherCommands(); program.parse(args); }; @@ -138,24 +137,17 @@ Cmd.prototype.test = function() { }); }; -// TODO: replace both of this with a deploy/upload command -Cmd.prototype.ipfs = function() { +Cmd.prototype.upload = function() { var self = this; program - .command('ipfs') - .description('deploy to IPFS') - .action(function() { - self.Embark.ipfs(); - }); -}; - -Cmd.prototype.swarm = function() { - var self = this; - program - .command('swarm') - .description('deploy to SWARM') - .action(function() { - self.Embark.swarm(); + .command('upload [platform]') + .description('upload your dapp to a decentralized storage. possible options: ipfs, swarm (e.g embark upload swarm)') + .action(function(platform ,options) { + // TODO: get env in cmd line as well + self.Embark.initConfig('development', { + embarkConfig: 'embark.json' + }); + self.Embark.upload(platform); }); }; diff --git a/lib/index.js b/lib/index.js index 5a2d63431..8c5c15c2b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -302,17 +302,19 @@ var Embark = { }, // TODO: should deploy if it hasn't already - ipfs: function() { - var ipfs = new IPFS({buildDir: 'dist/'}); - ipfs.deploy(); + upload: function(platform) { + if (platform === 'ipfs') { + var ipfs = new IPFS({buildDir: 'dist/'}); + ipfs.deploy(); + } else if (platform === 'swarm') { + var swarm = new Swarm({buildDir: 'dist/'}); + swarm.deploy(); + } else { + console.log(("unknown platform: " + platform).red); + console.log('try "embark upload ipfs" or "embark upload swarm"'.green); + } }, - // TODO: should deploy if it hasn't already - swarm: function() { - var swarm = new Swarm({buildDir: 'dist/'}); - swarm.deploy(); - } - }; module.exports = Embark; From 26f2ad7407a251e283c92325f15520e80528e4e3 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sun, 8 Jan 2017 13:19:27 -0500 Subject: [PATCH 4/4] refactor ipfs and swarm code --- lib/ipfs.js | 50 ++++++++++++++++++++++++++++++++++++-------------- lib/swarm.js | 51 ++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 74 insertions(+), 27 deletions(-) diff --git a/lib/ipfs.js b/lib/ipfs.js index 38b2bfed2..1c79c907e 100644 --- a/lib/ipfs.js +++ b/lib/ipfs.js @@ -1,4 +1,5 @@ var colors = require('colors'); +var async = require('async'); var IPFS = function(options) { this.options = options; @@ -6,24 +7,45 @@ var IPFS = function(options) { }; IPFS.prototype.deploy = function() { - var ipfs_bin = exec('which ipfs').output.split("\n")[0]; + var self = this; + async.waterfall([ + function findBinary(callback) { + var ipfs_bin = exec('which ipfs').output.split("\n")[0]; - if (ipfs_bin==='ipfs not found'){ - console.log('=== WARNING: IPFS not in an executable path. Guessing ~/go/bin/ipfs for path'.yellow); - ipfs_bin = "~/go/bin/ipfs"; - } + if (ipfs_bin==='ipfs not found'){ + console.log('=== WARNING: IPFS not in an executable path. Guessing ~/go/bin/ipfs for path'.yellow); + ipfs_bin = "~/go/bin/ipfs"; + } - var cmd = ipfs_bin + " add -r " + this.buildDir; - console.log(("=== adding " + this.buildDir + " to ipfs").green); - console.log(cmd.green); + return callback(null, ipfs_bin); + }, + function runCommand(ipfs_bin, callback) { + var cmd = ipfs_bin + " add -r " + self.buildDir; + console.log(("=== adding " + self.buildDir + " to ipfs").green); + console.log(cmd.green); + var result = exec(cmd); - var result = exec(cmd); - var rows = result.output.split("\n"); - var dir_row = rows[rows.length - 2]; - var dir_hash = dir_row.split(" ")[1]; + return callback(null, result); + }, + function getHashFromOutput(result, callback) { + var rows = result.output.split("\n"); + var dir_row = rows[rows.length - 2]; + var dir_hash = dir_row.split(" ")[1]; - console.log(("=== DApp available at http://localhost:8080/ipfs/" + dir_hash + "/").green); - console.log(("=== DApp available at http://gateway.ipfs.io/ipfs/" + dir_hash + "/").green); + return callback(null, dir_hash); + }, + function printUrls(dir_hash, callback) { + console.log(("=== DApp available at http://localhost:8080/ipfs/" + dir_hash + "/").green); + console.log(("=== DApp available at http://gateway.ipfs.io/ipfs/" + dir_hash + "/").green); + + return callback(); + } + ], function(err, result) { + if (err) { + console.log("error uploading to ipfs".red); + console.log(err); + } + }); }; module.exports = IPFS; diff --git a/lib/swarm.js b/lib/swarm.js index a7f6cc752..1ebb36bb1 100644 --- a/lib/swarm.js +++ b/lib/swarm.js @@ -1,4 +1,5 @@ var colors = require('colors'); +var async = require('async'); var Swarm = function(options) { this.options = options; @@ -6,24 +7,48 @@ var Swarm = function(options) { }; Swarm.prototype.deploy = function() { - var swarm_bin = exec('which swarm').output.split("\n")[0]; + var self = this; + async.waterfall([ + function findBinary(callback) { + var swarm_bin = exec('which swarm').output.split("\n")[0]; - if (swarm_bin==='swarm not found' || swarm_bin === ''){ - console.log('=== WARNING: Swarm not in an executable path. Guessing ~/go/bin/swarm for path'.yellow); - swarm_bin = "~/go/bin/swarm"; - } + if (swarm_bin==='swarm not found' || swarm_bin === ''){ + console.log('=== WARNING: Swarm not in an executable path. Guessing ~/go/bin/swarm for path'.yellow); + swarm_bin = "~/go/bin/swarm"; + } - var cmd = swarm_bin + " --defaultpath " + this.buildDir + "index.html --recursive up " + this.buildDir; - console.log(("=== adding " + this.buildDir + " to swarm").green); - console.log(cmd.green); + return callback(null, swarm_bin); + }, + function runCommand(swarm_bin, callback) { + var cmd = swarm_bin + " --defaultpath " + self.buildDir + "index.html --recursive up " + self.buildDir; + console.log(("=== adding " + self.buildDir + " to swarm").green); + console.log(cmd.green); + var result = exec(cmd); - var result = exec(cmd); - var rows = result.output.split("\n"); - var dir_hash = rows.reverse()[1]; + return callback(null, result); + }, + function getHashFromOutput(result, callback) { + if (result.code !== 0) { + return callback("couldn't upload, is the swarm daemon running?"); + } - console.log(("=== DApp available at http://localhost:8500/bzz:/" + dir_hash + "/").green); + var rows = result.output.split("\n"); + var dir_hash = rows.reverse()[1]; + + return callback(null, dir_hash); + }, + function printUrls(dir_hash, callback) { + console.log(("=== DApp available at http://localhost:8500/bzz:/" + dir_hash + "/").green); + + return callback(); + } + ], function(err, result) { + if (err) { + console.log("error uploading to swarm".red); + console.log(err); + } + }); }; module.exports = Swarm; -