Skip to content

Commit d1cb526

Browse files
committed
Merge branch 'swarm' into develop
2 parents 04bb61b + 26f2ad7 commit d1cb526

File tree

5 files changed

+118
-28
lines changed

5 files changed

+118
-28
lines changed

lib/cmd.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Cmd.prototype.process = function(args) {
1414
this.blockchain();
1515
this.simulator();
1616
this.test();
17-
this.ipfs();
17+
this.upload();
1818
this.otherCommands();
1919
program.parse(args);
2020
};
@@ -137,13 +137,17 @@ Cmd.prototype.test = function() {
137137
});
138138
};
139139

140-
Cmd.prototype.ipfs = function() {
140+
Cmd.prototype.upload = function() {
141141
var self = this;
142142
program
143-
.command('ipfs')
144-
.description('deploy to IPFS')
145-
.action(function() {
146-
self.Embark.ipfs();
143+
.command('upload [platform]')
144+
.description('upload your dapp to a decentralized storage. possible options: ipfs, swarm (e.g embark upload swarm)')
145+
.action(function(platform ,options) {
146+
// TODO: get env in cmd line as well
147+
self.Embark.initConfig('development', {
148+
embarkConfig: 'embark.json'
149+
});
150+
self.Embark.upload(platform);
147151
});
148152
};
149153

lib/index.js

Lines changed: 13 additions & 4 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

@@ -301,10 +302,18 @@ var Embark = {
301302
},
302303

303304
// TODO: should deploy if it hasn't already
304-
ipfs: function() {
305-
var ipfs = new IPFS({buildDir: 'dist/'});
306-
ipfs.deploy();
307-
}
305+
upload: function(platform) {
306+
if (platform === 'ipfs') {
307+
var ipfs = new IPFS({buildDir: 'dist/'});
308+
ipfs.deploy();
309+
} else if (platform === 'swarm') {
310+
var swarm = new Swarm({buildDir: 'dist/'});
311+
swarm.deploy();
312+
} else {
313+
console.log(("unknown platform: " + platform).red);
314+
console.log('try "embark upload ipfs" or "embark upload swarm"'.green);
315+
}
316+
},
308317

309318
};
310319

lib/ipfs.js

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,51 @@
11
var colors = require('colors');
2+
var async = require('async');
23

34
var IPFS = function(options) {
45
this.options = options;
56
this.buildDir = options.buildDir || 'dist/';
67
};
78

89
IPFS.prototype.deploy = function() {
9-
var ipfs_bin = exec('which ipfs').output.split("\n")[0];
10-
11-
if (ipfs_bin==='ipfs not found'){
12-
console.log('=== WARNING: IPFS not in an executable path. Guessing ~/go/bin/ipfs for path'.yellow);
13-
ipfs_bin = "~/go/bin/ipfs";
14-
}
15-
16-
var cmd = ipfs_bin + " add -r " + build_dir;
17-
console.log(("=== adding " + cmd + " to ipfs").green);
18-
19-
var result = exec(cmd);
20-
var rows = result.output.split("\n");
21-
var dir_row = rows[rows.length - 2];
22-
var dir_hash = dir_row.split(" ")[1];
23-
24-
console.log(("=== DApp available at http://localhost:8080/ipfs/" + dir_hash + "/").green);
25-
console.log(("=== DApp available at http://gateway.ipfs.io/ipfs/" + dir_hash + "/").green);
10+
var self = this;
11+
async.waterfall([
12+
function findBinary(callback) {
13+
var ipfs_bin = exec('which ipfs').output.split("\n")[0];
14+
15+
if (ipfs_bin==='ipfs not found'){
16+
console.log('=== WARNING: IPFS not in an executable path. Guessing ~/go/bin/ipfs for path'.yellow);
17+
ipfs_bin = "~/go/bin/ipfs";
18+
}
19+
20+
return callback(null, ipfs_bin);
21+
},
22+
function runCommand(ipfs_bin, callback) {
23+
var cmd = ipfs_bin + " add -r " + self.buildDir;
24+
console.log(("=== adding " + self.buildDir + " to ipfs").green);
25+
console.log(cmd.green);
26+
var result = exec(cmd);
27+
28+
return callback(null, result);
29+
},
30+
function getHashFromOutput(result, callback) {
31+
var rows = result.output.split("\n");
32+
var dir_row = rows[rows.length - 2];
33+
var dir_hash = dir_row.split(" ")[1];
34+
35+
return callback(null, dir_hash);
36+
},
37+
function printUrls(dir_hash, callback) {
38+
console.log(("=== DApp available at http://localhost:8080/ipfs/" + dir_hash + "/").green);
39+
console.log(("=== DApp available at http://gateway.ipfs.io/ipfs/" + dir_hash + "/").green);
40+
41+
return callback();
42+
}
43+
], function(err, result) {
44+
if (err) {
45+
console.log("error uploading to ipfs".red);
46+
console.log(err);
47+
}
48+
});
2649
};
2750

2851
module.exports = IPFS;

lib/swarm.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
var colors = require('colors');
2+
var async = require('async');
3+
4+
var Swarm = function(options) {
5+
this.options = options;
6+
this.buildDir = options.buildDir || 'dist/';
7+
};
8+
9+
Swarm.prototype.deploy = function() {
10+
var self = this;
11+
async.waterfall([
12+
function findBinary(callback) {
13+
var swarm_bin = exec('which swarm').output.split("\n")[0];
14+
15+
if (swarm_bin==='swarm not found' || swarm_bin === ''){
16+
console.log('=== WARNING: Swarm not in an executable path. Guessing ~/go/bin/swarm for path'.yellow);
17+
swarm_bin = "~/go/bin/swarm";
18+
}
19+
20+
return callback(null, swarm_bin);
21+
},
22+
function runCommand(swarm_bin, callback) {
23+
var cmd = swarm_bin + " --defaultpath " + self.buildDir + "index.html --recursive up " + self.buildDir;
24+
console.log(("=== adding " + self.buildDir + " to swarm").green);
25+
console.log(cmd.green);
26+
var result = exec(cmd);
27+
28+
return callback(null, result);
29+
},
30+
function getHashFromOutput(result, callback) {
31+
if (result.code !== 0) {
32+
return callback("couldn't upload, is the swarm daemon running?");
33+
}
34+
35+
var rows = result.output.split("\n");
36+
var dir_hash = rows.reverse()[1];
37+
38+
return callback(null, dir_hash);
39+
},
40+
function printUrls(dir_hash, callback) {
41+
console.log(("=== DApp available at http://localhost:8500/bzz:/" + dir_hash + "/").green);
42+
43+
return callback();
44+
}
45+
], function(err, result) {
46+
if (err) {
47+
console.log("error uploading to swarm".red);
48+
console.log(err);
49+
}
50+
});
51+
};
52+
53+
module.exports = Swarm;
54+

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"serve-static": "^1.11.1",
3333
"shelljs": "^0.5.0",
3434
"toposort": "^0.2.10",
35-
"web3": "^0.15.0",
35+
"web3": "^0.18.0",
3636
"wrench": "^1.5.8"
3737
},
3838
"author": "Iuri Matias <[email protected]>",

0 commit comments

Comments
 (0)