Skip to content

Commit acf1fa4

Browse files
authored
Merge pull request #372 from embark-framework/bug_fix/upload-to-swarm
swarm deploy refactored to use web3.bzz instead of command line
2 parents 51cfafa + 4c39a3f commit acf1fa4

File tree

5 files changed

+77
-62
lines changed

5 files changed

+77
-62
lines changed

lib/cmd.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,7 @@ class Cmd {
181181
.option('--loglevel [loglevel]', 'level of logging to display ["error", "warn", "info", "debug", "trace"]', /^(error|warn|info|debug|trace)$/i, 'debug')
182182
.description('Upload your dapp to a decentralized storage (e.g embark upload ipfs).')
183183
.action(function (platform, env, _options) {
184-
let environment = env || 'development';
185-
embark.initConfig(environment, {
186-
embarkConfig: 'embark.json', interceptLogs: false
187-
});
188-
_options.env = environment;
184+
_options.env = env || 'development';
189185
_options.logFile = _options.logfile; // fix casing
190186
_options.logLevel = _options.loglevel; // fix casing
191187
embark.upload(platform, _options);

lib/core/engine.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ class Engine {
6868
"webServer": this.webServerService,
6969
"ipfs": this.ipfsService,
7070
"web3": this.web3Service,
71-
"libraryManager": this.libraryManagerService
71+
"libraryManager": this.libraryManagerService,
72+
"swarm": this.swarmService
7273
};
7374

7475
let service = services[serviceName];
@@ -195,6 +196,14 @@ class Engine {
195196
});
196197
}
197198

199+
swarmService(_options) {
200+
this.registerModule('swarm', {
201+
addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor),
202+
storageConfig: this.config.storageConfig,
203+
web3: _options.web3
204+
});
205+
}
206+
198207
web3Service(options) {
199208
let self = this;
200209
this.web3 = options.web3;

lib/index.js

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,9 @@ class Embark {
157157
});
158158
}
159159

160-
build(options, continueProcessing) {
160+
build(options) {
161161
this.context = options.context || [constants.contexts.build];
162+
162163
let engine = new Engine({
163164
env: options.env,
164165
version: this.version,
@@ -202,9 +203,7 @@ class Embark {
202203
engine.logger.info("finished building".underline);
203204
}
204205
// needed due to child processes
205-
if(err || !continueProcessing){
206-
process.exit();
207-
}
206+
process.exit();
208207
});
209208
}
210209

@@ -271,59 +270,81 @@ class Embark {
271270
}
272271

273272
upload(platform, options) {
274-
this.context = options.context || [constants.contexts.upload, constants.contexts.build];
275273

276-
// populate options that were instantiated with initConfig to pass around
277-
options.buildDir = 'dist/';
278-
options.storageConfig = this.config.storageConfig;
279-
options.events = this.events;
280-
options.logger = this.logger;
281-
options.config = this.config;
282-
283-
// load plugins
284-
this.plugins.loadInternalPlugin('ipfs', options);
285-
this.plugins.loadInternalPlugin('swarm', options);
274+
this.context = options.context || [constants.contexts.upload, constants.contexts.build];
286275

287-
// upddate our options with loaded plugins
288-
options.plugins = this.plugins;
276+
let engine = new Engine({
277+
env: options.env,
278+
version: this.version,
279+
embarkConfig: 'embark.json',
280+
interceptLogs: false,
281+
logFile: options.logFile,
282+
logLevel: options.logLevel,
283+
events: options.events,
284+
logger: options.logger,
285+
config: options.config,
286+
plugins: options.plugins
287+
});
288+
engine.init();
289289

290290
let cmdPlugin;
291-
let self = this;
292291
async.waterfall([
292+
293+
function startServices(callback) {
294+
295+
engine.startService("libraryManager");
296+
engine.startService("web3");
297+
engine.startService("pipeline");
298+
engine.startService("codeGenerator");
299+
engine.startService("deployment");
300+
engine.startService("ipfs");
301+
engine.startService("swarm", {buildDir:'dist/',web3: engine.web3});
302+
callback();
303+
},
293304
function setupStoragePlugin(callback){
305+
let pluginList = engine.plugins.listPlugins();
306+
if (pluginList.length > 0) {
307+
engine.logger.info("loaded plugins: " + pluginList.join(", "));
308+
}
309+
294310
// check use has input existing storage plugin
295-
let cmdPlugins = self.plugins.getPluginsFor('uploadCmds');
311+
let cmdPlugins = engine.plugins.getPluginsFor('uploadCmds');
296312

297313
if (cmdPlugins.length > 0) {
298314
cmdPlugin = cmdPlugins.find((pluginCmd) => {
299315
return pluginCmd.name == platform;
300316
});
301317
}
302318
if (!cmdPlugin) {
303-
self.logger.info('try "embark upload ipfs" or "embark upload swarm"'.green);
319+
engine.logger.info('try "embark upload ipfs" or "embark upload swarm"'.green);
304320
callback({message: 'unknown platform: ' + platform});
305321
} else {
306322
callback();
307323
}
308324
},
309-
function buildAndDeployContracts(callback){
325+
function deploy(callback) {
310326
// 2. upload to storage (outputDone event triggered after webpack finished)
311-
self.events.on('outputDone', function () {
327+
engine.events.on('outputDone', function () {
312328
cmdPlugin.uploadCmds[0].cb()
313329
.then((success) => {
314330
callback(null, success);
315331
})
316332
.catch(callback);
317333
});
318334
// 1. build the contracts and dapp webpack
319-
self.build(options, true);
335+
engine.deployManager.deployContracts(function (err) {
336+
engine.logger.info("finished deploying".underline);
337+
if(err){
338+
callback(err);
339+
}
340+
});
320341
}
321342
], function (err, _result) {
322343
if (err) {
323-
self.logger.error(err.message);
324-
self.logger.debug(err.stack);
344+
engine.logger.error(err.message);
345+
engine.logger.debug(err.stack);
325346
} else {
326-
self.logger.info("finished building dapp and deploying to " + platform.underline);
347+
engine.logger.info(`finished building DApp and deploying to ${platform}`.underline);
327348
}
328349

329350
// needed due to child processes

lib/modules/swarm/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ class Swarm {
77

88
this.upload_swarm = new UploadSwarm({
99
buildDir: options.buildDir || 'dist/',
10-
storageConfig: options.storageConfig
10+
storageConfig: options.storageConfig,
11+
web3: options.web3
1112
});
1213

1314
embark.registerUploadCommand('swarm', this.upload_swarm.deploy.bind(this.upload_swarm));

lib/modules/swarm/upload.js

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,38 @@
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/';
8+
this.web3 = options.web3;
9+
this.storageConfig = options.storageConfig;
910
}
1011

1112
deploy() {
1213
return new Promise((resolve, reject) => {
1314
console.log("deploying to swarm!");
1415
let self = this;
16+
let web3 = this.web3;
1517
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);
18+
function setProvider(callback){
19+
web3.bzz.setProvider(`http://${self.storageConfig.host}:${self.storageConfig.port}`);
20+
callback();
2521
},
26-
function runCommand(swarm_bin, callback) {
27-
let cmd = `"${swarm_bin}" --defaultpath ${self.buildDir} index.html --recursive up ${self.buildDir}`;
22+
function runCommand(callback) {
2823
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-
});
34-
},
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-
}
24+
web3.bzz.upload({
25+
path: self.buildDir, // path to data / file / directory
26+
kind: "directory", // could also be "file" or "data"
27+
defaultFile: "index.html" // optional, and only for kind === "directory"
28+
})
29+
.then((success) => {
30+
callback(null, success);
31+
})
32+
.catch(callback);
4533
},
4634
function printUrls(dir_hash, callback) {
47-
console.log(("=== DApp available at http://localhost:8500/bzz:/" + dir_hash + "/").green);
35+
console.log((`=== DApp available at ${self.storageConfig.getUrl}${dir_hash}/`).green);
4836

4937
callback();
5038
}

0 commit comments

Comments
 (0)