Skip to content

Commit 26f2ad7

Browse files
committed
refactor ipfs and swarm code
1 parent c53d92b commit 26f2ad7

File tree

2 files changed

+82
-35
lines changed

2 files changed

+82
-35
lines changed

lib/ipfs.js

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +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 " + this.buildDir;
17-
console.log(("=== adding " + this.buildDir + " to ipfs").green);
18-
console.log(cmd.green);
19-
20-
var result = exec(cmd);
21-
var rows = result.output.split("\n");
22-
var dir_row = rows[rows.length - 2];
23-
var dir_hash = dir_row.split(" ")[1];
24-
25-
console.log(("=== DApp available at http://localhost:8080/ipfs/" + dir_hash + "/").green);
26-
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+
});
2749
};
2850

2951
module.exports = IPFS;

lib/swarm.js

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

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

89
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);
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+
});
2551
};
2652

2753
module.exports = Swarm;
2854

29-

0 commit comments

Comments
 (0)