Skip to content

Commit 15b8d9b

Browse files
committed
supporting uploading dapp to swarm
1 parent e8ef3ed commit 15b8d9b

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

lib/cmd.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Cmd.prototype.process = function(args) {
1515
this.simulator();
1616
this.test();
1717
this.ipfs();
18+
this.swarm();
1819
this.otherCommands();
1920
program.parse(args);
2021
};
@@ -137,6 +138,7 @@ Cmd.prototype.test = function() {
137138
});
138139
};
139140

141+
// TODO: replace both of this with a deploy/upload command
140142
Cmd.prototype.ipfs = function() {
141143
var self = this;
142144
program
@@ -147,6 +149,16 @@ Cmd.prototype.ipfs = function() {
147149
});
148150
};
149151

152+
Cmd.prototype.swarm = function() {
153+
var self = this;
154+
program
155+
.command('swarm')
156+
.description('deploy to SWARM')
157+
.action(function() {
158+
self.Embark.swarm();
159+
});
160+
};
161+
150162
Cmd.prototype.otherCommands = function() {
151163
program
152164
.action(function(env){

lib/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var Monitor = require('./monitor.js');
2323
var ServicesMonitor = require('./services.js');
2424
var Console = require('./console.js');
2525
var IPFS = require('./ipfs.js');
26+
var Swarm = require('./swarm.js');
2627

2728
var Embark = {
2829

@@ -304,6 +305,12 @@ var Embark = {
304305
ipfs: function() {
305306
var ipfs = new IPFS({buildDir: 'dist/'});
306307
ipfs.deploy();
308+
},
309+
310+
// TODO: should deploy if it hasn't already
311+
swarm: function() {
312+
var swarm = new Swarm({buildDir: 'dist/'});
313+
swarm.deploy();
307314
}
308315

309316
};

lib/ipfs.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ IPFS.prototype.deploy = function() {
1313
ipfs_bin = "~/go/bin/ipfs";
1414
}
1515

16-
var cmd = ipfs_bin + " add -r " + build_dir;
17-
console.log(("=== adding " + cmd + " to ipfs").green);
16+
var cmd = ipfs_bin + " add -r " + this.buildDir;
17+
console.log(("=== adding " + this.buildDir + " to ipfs").green);
18+
console.log(cmd.green);
1819

1920
var result = exec(cmd);
2021
var rows = result.output.split("\n");

lib/swarm.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var colors = require('colors');
2+
3+
var Swarm = function(options) {
4+
this.options = options;
5+
this.buildDir = options.buildDir || 'dist/';
6+
};
7+
8+
Swarm.prototype.deploy = function() {
9+
var swarm_bin = exec('which swarm').output.split("\n")[0];
10+
11+
if (swarm_bin==='swarm not found' || swarm_bin === ''){
12+
console.log('=== WARNING: Swarm not in an executable path. Guessing ~/go/bin/swarm for path'.yellow);
13+
swarm_bin = "~/go/bin/swarm";
14+
}
15+
16+
var cmd = swarm_bin + " --defaultpath " + this.buildDir + "index.html --recursive up " + this.buildDir;
17+
console.log(("=== adding " + this.buildDir + " to swarm").green);
18+
console.log(cmd.green);
19+
20+
var result = exec(cmd);
21+
var rows = result.output.split("\n");
22+
var dir_hash = rows.reverse()[1];
23+
24+
console.log(("=== DApp available at http://localhost:8500/bzz:/" + dir_hash + "/").green);
25+
};
26+
27+
module.exports = Swarm;
28+
29+

0 commit comments

Comments
 (0)