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
This commit is contained in:
emizzle 2018-04-20 17:39:45 +10:00
parent 9a79df68d9
commit c05915b0e9
2 changed files with 59 additions and 37 deletions

View File

@ -151,7 +151,7 @@ class Embark {
});
}
build(options, continueProcessing) {
build(options) {
let engine = new Engine({
env: options.env,
version: this.version,
@ -194,9 +194,7 @@ class Embark {
engine.logger.info("finished building".underline);
}
// needed due to child processes
if(err || !continueProcessing){
process.exit();
}
process.exit();
});
}
@ -273,6 +271,20 @@ class Embark {
// upddate our options with loaded plugins
options.plugins = this.plugins;
let engine = new Engine({
env: options.env,
version: this.version,
embarkConfig: 'embark.json',
interceptLogs: false,
logFile: options.logFile,
logLevel: options.logLevel,
events: options.events,
logger: options.logger,
config: options.config,
plugins: options.plugins
});
engine.init();
let cmdPlugin;
let self = this;
async.waterfall([
@ -292,17 +304,36 @@ class Embark {
callback();
}
},
function buildAndDeployContracts(callback){
function startServices(callback) {
let pluginList = engine.plugins.listPlugins();
if (pluginList.length > 0) {
engine.logger.info("loaded plugins: " + pluginList.join(", "));
}
engine.startService("libraryManager");
engine.startService("web3");
engine.startService("pipeline");
engine.startService("codeGenerator");
engine.startService("deployment");
engine.startService("ipfs");
callback();
},
function deploy(callback) {
// 2. upload to storage (outputDone event triggered after webpack finished)
self.events.on('outputDone', function () {
cmdPlugin.uploadCmds[0].cb()
cmdPlugin.uploadCmds[0].cb({web3: engine.web3})
.then((success) => {
callback(null, success);
})
.catch(callback);
});
// 1. build the contracts and dapp webpack
self.build(options, true);
engine.deployManager.deployContracts(function (err) {
engine.logger.info("finished building".underline);
if(err){
callback(err);
}
});
}
], function (err, _result) {
if (err) {

View File

@ -1,6 +1,5 @@
require('colors');
let async = require('async');
let shelljs = require('shelljs');
class Swarm {
constructor(options) {
@ -8,43 +7,35 @@ class Swarm {
this.buildDir = options.buildDir || 'dist/';
}
deploy() {
deploy(deployOptions) {
return new Promise((resolve, reject) => {
console.log("deploying to swarm!");
let self = this;
let web3 = (deployOptions || {}).web3;
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";
}
callback(null, swarm_bin);
function findWeb3(callback){
if(!web3){
callback('web3 must be passed in to the swarm deploy() method');
}else callback();
},
function runCommand(swarm_bin, callback) {
let cmd = `"${swarm_bin}" --defaultpath ${self.buildDir} index.html --recursive up ${self.buildDir}`;
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);
console.trace(cmd);
shelljs.exec(cmd, {silent:true}, function(code, stdout, stderr){ // {silent:true}: don't echo cmd output so it can be controlled via logLevel
console.log(stdout.green);
callback(stderr, {code: code, output: stdout});
});
},
function getHashFromOutput(result, callback) {
if (result.code !== 0) {
callback("couldn't upload, is the swarm daemon running?");
}
else{
let rows = result.output.split("\n");
let dir_hash = rows.reverse()[1];
callback(null, dir_hash);
}
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 http://localhost:8500/bzz:/" + dir_hash + "/").green);
console.log((`=== DApp available at ${self.options.storageConfig.getUrl}${dir_hash}/`).green);
callback();
}