embark-area-51/lib/upload/swarm.js

56 lines
1.6 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-01-07 21:52:29 +00:00
2017-03-29 17:50:05 +00:00
let Swarm = function(options) {
2017-01-07 21:52:29 +00:00
this.options = options;
this.buildDir = options.buildDir || 'dist/';
};
Swarm.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 swarm_bin = shelljs.exec('which swarm').output.split("\n")[0];
2017-01-08 18:19:27 +00:00
if (swarm_bin==='swarm not found' || swarm_bin === ''){
console.log('=== WARNING: Swarm not in an executable path. Guessing ~/go/bin/swarm for path'.yellow);
swarm_bin = "~/go/bin/swarm";
}
return callback(null, swarm_bin);
},
function runCommand(swarm_bin, callback) {
2017-03-29 17:50:05 +00:00
let cmd = swarm_bin + " --defaultpath " + self.buildDir + "index.html --recursive up " + self.buildDir;
2017-01-08 18:19:27 +00:00
console.log(("=== adding " + self.buildDir + " to swarm").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) {
if (result.code !== 0) {
return callback("couldn't upload, is the swarm daemon running?");
}
2017-03-29 17:50:05 +00:00
let rows = result.output.split("\n");
let dir_hash = rows.reverse()[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:8500/bzz:/" + dir_hash + "/").green);
return callback();
}
], function(err, result) {
if (err) {
console.log("error uploading to swarm".red);
console.log(err);
}
});
2017-01-07 21:52:29 +00:00
};
module.exports = Swarm;