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');
|
2017-01-07 21:52:29 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
class Swarm {
|
|
|
|
constructor(options) {
|
|
|
|
this.options = options;
|
|
|
|
this.buildDir = options.buildDir || 'dist/';
|
|
|
|
}
|
|
|
|
|
|
|
|
deploy() {
|
2018-04-13 05:06:13 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let self = this;
|
|
|
|
async.waterfall([
|
|
|
|
function findBinary(callback) {
|
|
|
|
let swarm_bin = shelljs.which('swarm');
|
|
|
|
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
|
2018-04-15 08:41:50 +00:00
|
|
|
callback(null, swarm_bin);
|
2018-04-13 05:06:13 +00:00
|
|
|
},
|
|
|
|
function runCommand(swarm_bin, callback) {
|
|
|
|
let cmd = `"${swarm_bin}" --defaultpath ${self.buildDir} index.html --recursive up ${self.buildDir}`;
|
|
|
|
console.log(("=== adding " + self.buildDir + " to swarm").green);
|
|
|
|
console.log(cmd.green);
|
2018-04-15 08:41:50 +00:00
|
|
|
shelljs.exec(cmd, function(code, stdout, stderr){
|
|
|
|
callback(stderr, {code: code, output: stdout});
|
|
|
|
});
|
2018-04-13 05:06:13 +00:00
|
|
|
},
|
|
|
|
function getHashFromOutput(result, callback) {
|
|
|
|
if (result.code !== 0) {
|
2018-04-15 08:41:50 +00:00
|
|
|
callback("couldn't upload, is the swarm daemon running?");
|
2018-04-13 05:06:13 +00:00
|
|
|
}
|
2018-04-15 08:41:50 +00:00
|
|
|
else{
|
|
|
|
let rows = result.output.split("\n");
|
|
|
|
let dir_hash = rows.reverse()[1];
|
2018-04-13 05:06:13 +00:00
|
|
|
|
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) {
|
|
|
|
console.log(("=== DApp available at http://localhost:8500/bzz:/" + dir_hash + "/").green);
|
|
|
|
|
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) {
|
|
|
|
console.log("error uploading to swarm".red);
|
|
|
|
console.log(err);
|
|
|
|
reject(err);
|
2017-03-30 11:12:39 +00:00
|
|
|
}
|
2018-04-13 05:06:13 +00:00
|
|
|
else resolve('successfully uploaded to swarm');
|
|
|
|
});
|
2017-03-30 11:12:39 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2017-01-07 21:52:29 +00:00
|
|
|
|
|
|
|
module.exports = Swarm;
|