Skip to content

Commit c05915b

Browse files
committed
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
1 parent 9a79df6 commit c05915b

File tree

2 files changed

+59
-37
lines changed

2 files changed

+59
-37
lines changed

lib/index.js

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ class Embark {
151151
});
152152
}
153153

154-
build(options, continueProcessing) {
154+
build(options) {
155155
let engine = new Engine({
156156
env: options.env,
157157
version: this.version,
@@ -194,9 +194,7 @@ class Embark {
194194
engine.logger.info("finished building".underline);
195195
}
196196
// needed due to child processes
197-
if(err || !continueProcessing){
198-
process.exit();
199-
}
197+
process.exit();
200198
});
201199
}
202200

@@ -273,6 +271,20 @@ class Embark {
273271
// upddate our options with loaded plugins
274272
options.plugins = this.plugins;
275273

274+
let engine = new Engine({
275+
env: options.env,
276+
version: this.version,
277+
embarkConfig: 'embark.json',
278+
interceptLogs: false,
279+
logFile: options.logFile,
280+
logLevel: options.logLevel,
281+
events: options.events,
282+
logger: options.logger,
283+
config: options.config,
284+
plugins: options.plugins
285+
});
286+
engine.init();
287+
276288
let cmdPlugin;
277289
let self = this;
278290
async.waterfall([
@@ -292,17 +304,36 @@ class Embark {
292304
callback();
293305
}
294306
},
295-
function buildAndDeployContracts(callback){
307+
function startServices(callback) {
308+
let pluginList = engine.plugins.listPlugins();
309+
if (pluginList.length > 0) {
310+
engine.logger.info("loaded plugins: " + pluginList.join(", "));
311+
}
312+
313+
engine.startService("libraryManager");
314+
engine.startService("web3");
315+
engine.startService("pipeline");
316+
engine.startService("codeGenerator");
317+
engine.startService("deployment");
318+
engine.startService("ipfs");
319+
callback();
320+
},
321+
function deploy(callback) {
296322
// 2. upload to storage (outputDone event triggered after webpack finished)
297323
self.events.on('outputDone', function () {
298-
cmdPlugin.uploadCmds[0].cb()
324+
cmdPlugin.uploadCmds[0].cb({web3: engine.web3})
299325
.then((success) => {
300326
callback(null, success);
301327
})
302328
.catch(callback);
303329
});
304330
// 1. build the contracts and dapp webpack
305-
self.build(options, true);
331+
engine.deployManager.deployContracts(function (err) {
332+
engine.logger.info("finished building".underline);
333+
if(err){
334+
callback(err);
335+
}
336+
});
306337
}
307338
], function (err, _result) {
308339
if (err) {

lib/modules/swarm/upload.js

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,41 @@
11
require('colors');
22
let async = require('async');
3-
let shelljs = require('shelljs');
43

54
class Swarm {
65
constructor(options) {
76
this.options = options;
87
this.buildDir = options.buildDir || 'dist/';
98
}
109

11-
deploy() {
10+
deploy(deployOptions) {
1211
return new Promise((resolve, reject) => {
1312
console.log("deploying to swarm!");
1413
let self = this;
14+
let web3 = (deployOptions || {}).web3;
1515
async.waterfall([
16-
function findBinary(callback) {
17-
let swarm_bin = shelljs.which('swarm');
18-
19-
if (swarm_bin === 'swarm not found' || !swarm_bin) {
20-
console.log('=== WARNING: Swarm not in an executable path. Guessing ~/go/bin/swarm for path'.yellow);
21-
swarm_bin = "~/go/bin/swarm";
22-
}
23-
24-
callback(null, swarm_bin);
16+
function findWeb3(callback){
17+
if(!web3){
18+
callback('web3 must be passed in to the swarm deploy() method');
19+
}else callback();
2520
},
26-
function runCommand(swarm_bin, callback) {
27-
let cmd = `"${swarm_bin}" --defaultpath ${self.buildDir} index.html --recursive up ${self.buildDir}`;
28-
console.log(("=== adding " + self.buildDir + " to swarm").green);
29-
console.trace(cmd);
30-
shelljs.exec(cmd, {silent:true}, function(code, stdout, stderr){ // {silent:true}: don't echo cmd output so it can be controlled via logLevel
31-
console.log(stdout.green);
32-
callback(stderr, {code: code, output: stdout});
33-
});
21+
function setProvider(callback){
22+
web3.bzz.setProvider(`http://${self.options.storageConfig.host}:${self.options.storageConfig.port}`);
23+
callback();
3424
},
35-
function getHashFromOutput(result, callback) {
36-
if (result.code !== 0) {
37-
callback("couldn't upload, is the swarm daemon running?");
38-
}
39-
else{
40-
let rows = result.output.split("\n");
41-
let dir_hash = rows.reverse()[1];
42-
43-
callback(null, dir_hash);
44-
}
25+
function runCommand(callback) {
26+
console.log(("=== adding " + self.buildDir + " to swarm").green);
27+
web3.bzz.upload({
28+
path: self.buildDir, // path to data / file / directory
29+
kind: "directory", // could also be "file" or "data"
30+
defaultFile: "index.html" // optional, and only for kind === "directory"
31+
})
32+
.then((success) => {
33+
callback(null, success);
34+
})
35+
.catch(callback);
4536
},
4637
function printUrls(dir_hash, callback) {
47-
console.log(("=== DApp available at http://localhost:8500/bzz:/" + dir_hash + "/").green);
38+
console.log((`=== DApp available at ${self.options.storageConfig.getUrl}${dir_hash}/`).green);
4839

4940
callback();
5041
}

0 commit comments

Comments
 (0)