emizzle c05915b0e9 swarm deploy refactored to use web3.bzz instead of command line
* `Embark.upload()` refactored to build own `Engine` and services so `web3` could be passed to `Swarm` module
* `Swarm.deploy()` modified to use `web3.bzz.upload()`
* needs detection of running swarm node
2018-04-20 17:39:45 +10:00

55 lines
1.7 KiB
JavaScript

require('colors');
let async = require('async');
class Swarm {
constructor(options) {
this.options = options;
this.buildDir = options.buildDir || 'dist/';
}
deploy(deployOptions) {
return new Promise((resolve, reject) => {
console.log("deploying to swarm!");
let self = this;
let web3 = (deployOptions || {}).web3;
async.waterfall([
function findWeb3(callback){
if(!web3){
callback('web3 must be passed in to the swarm deploy() method');
}else callback();
},
function setProvider(callback){
web3.bzz.setProvider(`http://${self.options.storageConfig.host}:${self.options.storageConfig.port}`);
callback();
},
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);
},
function printUrls(dir_hash, callback) {
console.log((`=== DApp available at ${self.options.storageConfig.getUrl}${dir_hash}/`).green);
callback();
}
], function (err, _result) {
if (err) {
console.log("error uploading to swarm".red);
console.log(err);
reject(err);
}
else resolve('successfully uploaded to swarm');
});
});
}
}
module.exports = Swarm;