embark/lib/upload/ipfs.js

57 lines
1.7 KiB
JavaScript
Raw Normal View History

2017-03-29 17:50:05 +00:00
let colors = require('colors');
let async = require('async');
let shelljs = require('shelljs');
2017-03-29 17:50:05 +00:00
let IPFS = function(options) {
2016-10-02 21:04:22 +00:00
this.options = options;
this.buildDir = options.buildDir || 'dist/';
this.plugins = options.plugins;
this.storageConfig = options.storageConfig;
this.configIpfsBin = this.storageConfig.ipfs_bin || "ipfs";
2016-10-02 21:04:22 +00:00
};
IPFS.prototype.deploy = function() {
2017-03-29 17:50:05 +00:00
let self = this;
2017-01-08 18:19:27 +00:00
async.waterfall([
function findBinary(callback) {
2017-03-29 17:50:05 +00:00
let ipfs_bin = shelljs.exec('which ' + self.configIpfsBin).output.split("\n")[0];
2017-01-08 18:19:27 +00:00
if (ipfs_bin === 'ipfs not found' || ipfs_bin === ''){
console.log(('=== WARNING: ' + self.configIpfsBin + ' not found or not in the path. Guessing ~/go/bin/ipfs for path').yellow);
2017-01-08 18:19:27 +00:00
ipfs_bin = "~/go/bin/ipfs";
}
return callback(null, ipfs_bin);
},
function runCommand(ipfs_bin, callback) {
2017-03-29 17:50:05 +00:00
let cmd = ipfs_bin + " add -r " + self.buildDir;
2017-01-08 18:19:27 +00:00
console.log(("=== adding " + self.buildDir + " to ipfs").green);
console.log(cmd.green);
2017-03-29 17:50:05 +00:00
let result = shelljs.exec(cmd);
2017-01-08 18:19:27 +00:00
return callback(null, result);
},
function getHashFromOutput(result, callback) {
2017-03-29 17:50:05 +00:00
let rows = result.output.split("\n");
let dir_row = rows[rows.length - 2];
let dir_hash = dir_row.split(" ")[1];
2017-01-08 18:19:27 +00:00
return callback(null, dir_hash);
},
function printUrls(dir_hash, callback) {
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);
return callback();
}
], function(err, result) {
if (err) {
console.log("error uploading to ipfs".red);
console.log(err);
}
});
2016-10-02 21:04:22 +00:00
};
2016-10-02 21:04:22 +00:00
module.exports = IPFS;
2016-10-02 20:58:52 +00:00