From 15b8d9b33eb96a2695d2d5b8555565779eed68d1 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 7 Jan 2017 16:52:29 -0500 Subject: [PATCH] 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 b314b714..4e8fc629 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 55d689f3..5a2d6343 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 6333c2d6..38b2bfed 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 00000000..a7f6cc75 --- /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; + +