diff --git a/packages/embark/src/lib/core/engine.js b/packages/embark/src/lib/core/engine.js index fe48546ee..3f532dd40 100644 --- a/packages/embark/src/lib/core/engine.js +++ b/packages/embark/src/lib/core/engine.js @@ -192,8 +192,10 @@ class Engine { this.registerModule('code_generator', {plugins: self.plugins, env: self.env}); const generateCode = function (modifiedAssets) { - self.events.request("code-generator:embarkjs:build", () => { - self.events.emit('code-generator-ready', modifiedAssets); + self.events.request("module:storage:initiated", () => { + self.events.request("code-generator:embarkjs:build", () => { + self.events.emit('code-generator-ready', modifiedAssets); + }); }); }; const cargo = async.cargo((tasks, callback) => { @@ -271,9 +273,27 @@ class Engine { } storageService(_options) { - this.registerModule('storage', {plugins: this.plugins}); - this.registerModule('ipfs'); - this.registerModule('swarm'); + async.parallel([ + (next) => { + if (!this.config.storageConfig.available_providers.includes("ipfs")) { + return next(); + } + this.registerModule('ipfs'); + this.events.on("ipfs:process:started", next); + }, + (next) => { + if (!this.config.storageConfig.available_providers.includes("swarm")) { + return next(); + } + this.registerModule('swarm'); + this.events.on("swarm:process:started", next); + } + ], (err) => { + if(err) { + console.error(__("Error starting storage process(es): %s", err)); + } + this.registerModule('storage', {plugins: this.plugins}); + }); } web3Service(options) { diff --git a/packages/embark/src/lib/modules/ipfs/index.js b/packages/embark/src/lib/modules/ipfs/index.js index 10e59f665..5da6d61ef 100644 --- a/packages/embark/src/lib/modules/ipfs/index.js +++ b/packages/embark/src/lib/modules/ipfs/index.js @@ -8,7 +8,6 @@ const constants = require('../../constants.json'); class IPFS { constructor(embark, options) { - const self = this; this.logger = embark.logger; this.events = embark.events; this.buildDir = options.buildDir; @@ -26,9 +25,10 @@ class IPFS { this.registerUploadCommand(); this.events.request("processes:register", "ipfs", (cb) => { - self.startProcess(() => { + this.startProcess(() => { this.addStorageProviderToEmbarkJS(); this.addObjectToConsole(); + this.events.emit("ipfs:process:started"); cb(); }); }); @@ -37,7 +37,7 @@ class IPFS { if (!err) { return; } - self.logger.info("IPFS node not found, attempting to start own node"); + this.logger.info("IPFS node not found, attempting to start own node"); this.listenToCommands(); this.registerConsoleCommands(); this.events.request('processes:launch', 'ipfs'); diff --git a/packages/embark/src/lib/modules/storage/index.js b/packages/embark/src/lib/modules/storage/index.js index 97f5ff05f..3a20d4b43 100644 --- a/packages/embark/src/lib/modules/storage/index.js +++ b/packages/embark/src/lib/modules/storage/index.js @@ -7,7 +7,9 @@ class Storage { if (!this.storageConfig.enabled) return; this.handleUploadCommand(); - this.addSetProviders(); + this.addSetProviders(() => { + this.embark.events.setCommandHandler("module:storage:initiated", (cb) => { cb(); }); + }); } handleUploadCommand() { @@ -26,7 +28,7 @@ class Storage { }); } - addSetProviders() { + addSetProviders(cb) { let code = `\nEmbarkJS.Storage.setProviders(${JSON.stringify(this.storageConfig.dappConnection || [])});`; let shouldInit = (storageConfig) => { @@ -36,6 +38,7 @@ class Storage { this.embark.addProviderInit('storage', code, shouldInit); this.embark.events.request("runcode:storage:providerRegistered", () => { this.embark.addConsoleProviderInit('storage', code, shouldInit); + cb(); }); } diff --git a/packages/embark/src/lib/modules/swarm/index.js b/packages/embark/src/lib/modules/swarm/index.js index 2b002bb34..9d707c16a 100644 --- a/packages/embark/src/lib/modules/swarm/index.js +++ b/packages/embark/src/lib/modules/swarm/index.js @@ -46,21 +46,21 @@ class Swarm { this.swarm = new SwarmAPI({gateway: this.providerUrl}); this.setServiceCheck(); - this.addProviderToEmbarkJS(); - this.addObjectToConsole(); this.registerUploadCommand(); // swarm needs geth to be running first - this.events.once(constants.blockchain.blockchainReady, () => { - this.swarm.isAvailable((err, isAvailable) => { - if (!err || isAvailable) { - this.logger.info("Swarm node found, using currently running node"); - return; - } - this.logger.info("SWARM: Swarm node not found, attempting to start own node"); - this.listenToCommands(); - this.registerConsoleCommands(); - return this.startProcess(() => {}); + this.swarm.isAvailable((err, isAvailable) => { + if (!err || isAvailable) { + this.logger.info("Swarm node found, using currently running node"); + return; + } + this.logger.info("SWARM: Swarm node not found, attempting to start own node"); + this.listenToCommands(); + this.registerConsoleCommands(); + return this.startProcess(() => { + this.addProviderToEmbarkJS(); + this.addObjectToConsole(); + this.events.emit("swarm:process:started"); }); }); } diff --git a/packages/embark/src/lib/modules/swarm/process.js b/packages/embark/src/lib/modules/swarm/process.js index 0ac013bb7..554c2c890 100644 --- a/packages/embark/src/lib/modules/swarm/process.js +++ b/packages/embark/src/lib/modules/swarm/process.js @@ -62,7 +62,7 @@ class SwarmProcess extends ProcessWrapper { // Swarm logs appear in stderr somehow this.child.stderr.on('data', (data) => { data = data.toString(); - if (!self.readyCalled && data.indexOf('Swarm http proxy started') > -1) { + if (!self.readyCalled && (data.includes('Swarm http proxy started') || data.includes('Swarm network started'))) { self.readyCalled = true; self.send({result: constants.storage.initiated}); }