|
1 | 1 | var colors = require('colors'); |
| 2 | +var async = require('async'); |
2 | 3 |
|
3 | 4 | var IPFS = function(options) { |
4 | 5 | this.options = options; |
5 | 6 | this.buildDir = options.buildDir || 'dist/'; |
6 | 7 | }; |
7 | 8 |
|
8 | 9 | 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 | + }); |
26 | 49 | }; |
27 | 50 |
|
28 | 51 | module.exports = IPFS; |
|
0 commit comments