2017-12-05 23:14:46 +00:00
|
|
|
require('colors');
|
2017-03-29 17:50:05 +00:00
|
|
|
let async = require('async');
|
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/';
|
|
|
|
}
|
|
|
|
|
2018-04-20 07:39:45 +00:00
|
|
|
deploy(deployOptions) {
|
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 swarm!");
|
2018-04-13 05:06:13 +00:00
|
|
|
let self = this;
|
2018-04-20 07:39:45 +00:00
|
|
|
let web3 = (deployOptions || {}).web3;
|
2018-04-13 05:06:13 +00:00
|
|
|
async.waterfall([
|
2018-04-20 07:39:45 +00:00
|
|
|
function findWeb3(callback){
|
|
|
|
if(!web3){
|
|
|
|
callback('web3 must be passed in to the swarm deploy() method');
|
|
|
|
}else callback();
|
2018-04-13 05:06:13 +00:00
|
|
|
},
|
2018-04-20 07:39:45 +00:00
|
|
|
function setProvider(callback){
|
|
|
|
web3.bzz.setProvider(`http://${self.options.storageConfig.host}:${self.options.storageConfig.port}`);
|
|
|
|
callback();
|
2018-04-13 05:06:13 +00:00
|
|
|
},
|
2018-04-20 07:39:45 +00:00
|
|
|
function runCommand(callback) {
|
|
|
|
console.log(("=== adding " + self.buildDir + " to swarm").green);
|
|
|
|
web3.bzz.upload({
|
|
|
|
path: self.buildDir, // path to data / file / directory
|
|
|
|
kind: "directory", // could also be "file" or "data"
|
|
|
|
defaultFile: "index.html" // optional, and only for kind === "directory"
|
|
|
|
})
|
|
|
|
.then((success) => {
|
|
|
|
callback(null, success);
|
|
|
|
})
|
|
|
|
.catch(callback);
|
2018-04-13 05:06:13 +00:00
|
|
|
},
|
|
|
|
function printUrls(dir_hash, callback) {
|
2018-04-20 07:39:45 +00:00
|
|
|
console.log((`=== DApp available at ${self.options.storageConfig.getUrl}${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) {
|
|
|
|
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;
|