Skip to content

Commit cc30ff3

Browse files
committed
Implemented PR review changes
* Removed config init from `cmd.js` for upload. * refactored `upload()` to use engine services instead of loading and using plugins directly. * now passing web3 directly to the `Swarm` constructor
1 parent c05915b commit cc30ff3

File tree

5 files changed

+46
-53
lines changed

5 files changed

+46
-53
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: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ class Engine {
6767
"webServer": this.webServerService,
6868
"ipfs": this.ipfsService,
6969
"web3": this.web3Service,
70-
"libraryManager": this.libraryManagerService
70+
"libraryManager": this.libraryManagerService,
71+
"swarm": this.swarmService
7172
};
7273

7374
let service = services[serviceName];
@@ -194,6 +195,16 @@ class Engine {
194195
});
195196
}
196197

198+
swarmService(_options) {
199+
this.registerModule('swarm', {
200+
addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor),
201+
storageConfig: this.config.storageConfig,
202+
host: _options.host,
203+
port: _options.port,
204+
web3: _options.web3
205+
});
206+
}
207+
197208
web3Service(options) {
198209
let self = this;
199210
this.web3 = options.web3;

lib/index.js

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -256,20 +256,6 @@ class Embark {
256256
}
257257

258258
upload(platform, options) {
259-
260-
// populate options that were instantiated with initConfig to pass around
261-
options.buildDir = 'dist/';
262-
options.storageConfig = this.config.storageConfig;
263-
options.events = this.events;
264-
options.logger = this.logger;
265-
options.config = this.config;
266-
267-
// load plugins
268-
this.plugins.loadInternalPlugin('ipfs', options);
269-
this.plugins.loadInternalPlugin('swarm', options);
270-
271-
// upddate our options with loaded plugins
272-
options.plugins = this.plugins;
273259

274260
let engine = new Engine({
275261
env: options.env,
@@ -286,61 +272,63 @@ class Embark {
286272
engine.init();
287273

288274
let cmdPlugin;
289-
let self = this;
290275
async.waterfall([
276+
277+
function startServices(callback) {
278+
279+
engine.startService("libraryManager");
280+
engine.startService("web3");
281+
engine.startService("pipeline");
282+
engine.startService("codeGenerator");
283+
engine.startService("deployment");
284+
engine.startService("ipfs");
285+
engine.startService("swarm", {buildDir:'dist/',web3: engine.web3});
286+
callback();
287+
},
291288
function setupStoragePlugin(callback){
289+
let pluginList = engine.plugins.listPlugins();
290+
if (pluginList.length > 0) {
291+
engine.logger.info("loaded plugins: " + pluginList.join(", "));
292+
}
293+
292294
// check use has input existing storage plugin
293-
let cmdPlugins = self.plugins.getPluginsFor('uploadCmds');
295+
let cmdPlugins = engine.plugins.getPluginsFor('uploadCmds');
294296

295297
if (cmdPlugins.length > 0) {
296298
cmdPlugin = cmdPlugins.find((pluginCmd) => {
297299
return pluginCmd.name == platform;
298300
});
299301
}
300302
if (!cmdPlugin) {
301-
self.logger.info('try "embark upload ipfs" or "embark upload swarm"'.green);
303+
engine.logger.info('try "embark upload ipfs" or "embark upload swarm"'.green);
302304
callback({message: 'unknown platform: ' + platform});
303305
} else {
304306
callback();
305307
}
306308
},
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-
},
321309
function deploy(callback) {
322310
// 2. upload to storage (outputDone event triggered after webpack finished)
323-
self.events.on('outputDone', function () {
324-
cmdPlugin.uploadCmds[0].cb({web3: engine.web3})
311+
engine.events.on('outputDone', function () {
312+
cmdPlugin.uploadCmds[0].cb()
325313
.then((success) => {
326314
callback(null, success);
327315
})
328316
.catch(callback);
329317
});
330318
// 1. build the contracts and dapp webpack
331319
engine.deployManager.deployContracts(function (err) {
332-
engine.logger.info("finished building".underline);
320+
engine.logger.info("finished deploying".underline);
333321
if(err){
334322
callback(err);
335323
}
336324
});
337325
}
338326
], function (err, _result) {
339327
if (err) {
340-
self.logger.error(err.message);
341-
self.logger.debug(err.stack);
328+
engine.logger.error(err.message);
329+
engine.logger.debug(err.stack);
342330
} else {
343-
self.logger.info("finished building dapp and deploying to " + platform.underline);
331+
engine.logger.info(`finished building DApp and deploying to ${platform}`.underline);
344332
}
345333

346334
// 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: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,18 @@ class Swarm {
55
constructor(options) {
66
this.options = options;
77
this.buildDir = options.buildDir || 'dist/';
8+
this.web3 = options.web3;
9+
this.storageConfig = options.storageConfig;
810
}
911

10-
deploy(deployOptions) {
12+
deploy() {
1113
return new Promise((resolve, reject) => {
1214
console.log("deploying to swarm!");
1315
let self = this;
14-
let web3 = (deployOptions || {}).web3;
16+
let web3 = this.web3;
1517
async.waterfall([
16-
function findWeb3(callback){
17-
if(!web3){
18-
callback('web3 must be passed in to the swarm deploy() method');
19-
}else callback();
20-
},
2118
function setProvider(callback){
22-
web3.bzz.setProvider(`http://${self.options.storageConfig.host}:${self.options.storageConfig.port}`);
19+
web3.bzz.setProvider(`http://${self.storageConfig.host}:${self.storageConfig.port}`);
2320
callback();
2421
},
2522
function runCommand(callback) {
@@ -35,7 +32,7 @@ class Swarm {
3532
.catch(callback);
3633
},
3734
function printUrls(dir_hash, callback) {
38-
console.log((`=== DApp available at ${self.options.storageConfig.getUrl}${dir_hash}/`).green);
35+
console.log((`=== DApp available at ${self.storageConfig.getUrl}${dir_hash}/`).green);
3936

4037
callback();
4138
}

0 commit comments

Comments
 (0)