2017-12-05 23:14:46 +00:00
|
|
|
require('colors');
|
2017-03-29 17:50:05 +00:00
|
|
|
let async = require('async');
|
|
|
|
let shelljs = require('shelljs');
|
2015-06-28 02:20:07 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
class IPFS {
|
|
|
|
|
|
|
|
constructor(options) {
|
|
|
|
this.options = options;
|
|
|
|
this.buildDir = options.buildDir || 'dist/';
|
|
|
|
this.storageConfig = options.storageConfig;
|
|
|
|
this.configIpfsBin = this.storageConfig.ipfs_bin || "ipfs";
|
|
|
|
}
|
|
|
|
|
|
|
|
deploy() {
|
2018-04-13 05:06:13 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2018-04-19 04:25:43 +00:00
|
|
|
console.log("deploying to ipfs!");
|
2018-04-13 05:06:13 +00:00
|
|
|
let self = this;
|
|
|
|
async.waterfall([
|
|
|
|
function findBinary(callback) {
|
|
|
|
let ipfs_bin = shelljs.which(self.configIpfsBin);
|
|
|
|
|
|
|
|
if (ipfs_bin === 'ipfs not found' || !ipfs_bin) {
|
2018-05-08 21:49:46 +00:00
|
|
|
console.log(('=== WARNING: ' + self.configIpfsBin + ' ' + __('not found or not in the path. Guessing %s for path', '~/go/bin/ipfs')).yellow);
|
2018-04-13 05:06:13 +00:00
|
|
|
ipfs_bin = "~/go/bin/ipfs";
|
|
|
|
}
|
|
|
|
|
2018-04-15 08:41:50 +00:00
|
|
|
callback(null, ipfs_bin);
|
2018-04-13 05:06:13 +00:00
|
|
|
},
|
|
|
|
function runCommand(ipfs_bin, callback) {
|
|
|
|
let cmd = `"${ipfs_bin}" add -r ${self.buildDir}`;
|
2018-05-08 21:49:46 +00:00
|
|
|
console.log(("=== " + __("adding %s to ipfs", self.buildDir)).green);
|
2018-04-30 05:56:43 +00:00
|
|
|
console.debug(cmd);
|
2018-04-19 04:25:43 +00:00
|
|
|
shelljs.exec(cmd, {silent:true}, function(code, stdout, stderr){ // {silent:true}: don't echo cmd output so it can be controlled via logLevel
|
|
|
|
console.log(stdout.green);
|
2018-04-15 08:41:50 +00:00
|
|
|
callback(stderr, stdout);
|
|
|
|
});
|
2018-04-13 05:06:13 +00:00
|
|
|
},
|
|
|
|
function getHashFromOutput(result, callback) {
|
2018-04-15 08:41:50 +00:00
|
|
|
let rows = result.split("\n");
|
2018-04-13 05:06:13 +00:00
|
|
|
let dir_row = rows[rows.length - 2];
|
|
|
|
let dir_hash = dir_row.split(" ")[1];
|
|
|
|
|
2018-04-15 08:41:50 +00:00
|
|
|
callback(null, dir_hash);
|
2018-04-13 05:06:13 +00:00
|
|
|
},
|
|
|
|
function printUrls(dir_hash, callback) {
|
2018-05-08 21:49:46 +00:00
|
|
|
console.log(("=== " + __("DApp available at") + " http://localhost:8080/ipfs/" + dir_hash + "/").green);
|
|
|
|
console.log(("=== " + __("DApp available at") + " http://gateway.ipfs.io/ipfs/" + dir_hash + "/").green);
|
2018-04-13 05:06:13 +00:00
|
|
|
|
2018-04-15 08:41:50 +00:00
|
|
|
callback();
|
2017-03-30 11:12:39 +00:00
|
|
|
}
|
2018-04-13 05:06:13 +00:00
|
|
|
], function (err, _result) {
|
|
|
|
if (err) {
|
2018-05-08 21:49:46 +00:00
|
|
|
console.log(__("error uploading to ipfs").red);
|
2018-04-13 05:06:13 +00:00
|
|
|
console.log(err);
|
|
|
|
reject(err);
|
|
|
|
}
|
2018-05-08 21:49:46 +00:00
|
|
|
else resolve(__('successfully uploaded to ipfs'));
|
2018-04-13 05:06:13 +00:00
|
|
|
});
|
2017-03-30 11:12:39 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2015-06-28 02:20:07 +00:00
|
|
|
|
2016-10-02 21:04:22 +00:00
|
|
|
module.exports = IPFS;
|