From 459d0cc2d6f15712cedeb9545ec5de04405c2b67 Mon Sep 17 00:00:00 2001
From: Jonathan Rainville <rainville.jonathan@gmail.com>
Date: Wed, 1 Aug 2018 11:14:02 -0400
Subject: [PATCH] small conflicts

---
 lib/core/logger.js                            |  4 +--
 lib/core/plugin.js                            |  8 +++++-
 lib/core/processes/processLauncher.js         | 18 ++++++++++++
 lib/core/processes/processManager.js          | 28 +++++++++++++++++--
 lib/modules/ipfs/index.js                     |  8 ++++++
 .../storage/storageProcessesLauncher.js       |  3 ++
 lib/modules/webserver/server.js               |  3 +-
 7 files changed, 65 insertions(+), 7 deletions(-)

diff --git a/lib/core/logger.js b/lib/core/logger.js
index 6339a3e6..f8f3e125 100644
--- a/lib/core/logger.js
+++ b/lib/core/logger.js
@@ -26,7 +26,7 @@ Logger.prototype.registerAPICall = function (plugins) {
   plugin.registerAPICall(
     'ws',
     '/embark-api/logs',
-    (ws, req) => {
+    (ws, _req) => {
       self.events.on("log", function(logLevel, logMsg) {
         ws.send(JSON.stringify({msg: logMsg, msg_clear: logMsg.stripColors, logLevel: logLevel}), () => {});
       });
@@ -34,7 +34,7 @@ Logger.prototype.registerAPICall = function (plugins) {
   );
 };
 
-Logger.prototype.writeToFile = function (txt) {
+Logger.prototype.writeToFile = function (_txt) {
   if (!this.logfile) {
     return;
   }
diff --git a/lib/core/plugin.js b/lib/core/plugin.js
index c5758af9..fdeeb396 100644
--- a/lib/core/plugin.js
+++ b/lib/core/plugin.js
@@ -24,6 +24,7 @@ var Plugin = function(options) {
   this.pluginTypes = [];
   this.uploadCmds = [];
   this.apiCalls = [];
+  this.processes = [];
   this.imports = [];
   this.embarkjs_code = [];
   this.embarkjs_init_code = {};
@@ -228,7 +229,12 @@ Plugin.prototype.registerActionForEvent = function(eventName, cb) {
 Plugin.prototype.registerAPICall = function(method, endpoint, cb) {
   console.dir("registerAPICall " + method + " " + endpoint);
   this.apiCalls.push({method: method, endpoint: endpoint, cb: cb});
-  this.pluginTypes.push('apiCalls');
+  this.addPluginType('apiCalls');
+};
+
+Plugin.prototype.registerProcess = function(processName) {
+  this.processes.push({processName});
+  this.addPluginType('processes');
 };
 
 Plugin.prototype.runFilePipeline = function() {
diff --git a/lib/core/processes/processLauncher.js b/lib/core/processes/processLauncher.js
index 88ad8f4c..3d60390a 100644
--- a/lib/core/processes/processLauncher.js
+++ b/lib/core/processes/processLauncher.js
@@ -28,9 +28,13 @@ class ProcessLauncher {
     this.events = options.events;
     this.silent = options.silent;
     this.exitCallback = options.exitCallback;
+    this.embark = options.embark;
 
     this.subscriptions = {};
     this._subscribeToMessages();
+    if (this.embark) {
+      this._registerAsPlugin();
+    }
   }
 
   _isDebug() {
@@ -61,8 +65,22 @@ class ProcessLauncher {
     });
   }
 
+  _registerAsPlugin() {
+    const self = this;
+    self.embark.registerAPICall(
+      'ws',
+      '/embark/process-logs/' + self.name,
+      (ws, _req) => {
+        self.events.on('log-' + self.name, function(logLevel, msg) {
+          ws.send(JSON.stringify({msg, msg_clear: msg.stripColors, logLevel}), () => {});
+        });
+      }
+    );
+  }
+
   // Translates logs from the child process to the logger
   _handleLog(msg) {
+    this.events.emit('log-' + this.name, msg.type, msg.message);
     if (this.silent && msg.type !== 'error') {
       return;
     }
diff --git a/lib/core/processes/processManager.js b/lib/core/processes/processManager.js
index 2556acb5..d3117594 100644
--- a/lib/core/processes/processManager.js
+++ b/lib/core/processes/processManager.js
@@ -1,12 +1,35 @@
 
 class ProcessManager {
   constructor(options) {
-    const self = this;
     this.logger = options.logger;
     this.events = options.events;
     this.plugins = options.plugins;
     this.processes = {};
 
+    this._registerAsPlugin();
+    this._registerEvents();
+  }
+
+  _registerAsPlugin() {
+    const self  =this;
+    self.plugin = this.plugins.createPlugin('processManager', {});
+    self.plugin.registerAPICall(
+      'get',
+      '/embark/processes',
+      (req, res) => {
+        let parsedProcesses = {};
+        Object.keys(self.processes).forEach(processName => {
+          parsedProcesses[processName] = {
+            state: self.processes[processName]
+          };
+        });
+        res.send(parsedProcesses);
+      }
+    );
+  }
+
+  _registerEvents() {
+    const self = this;
     self.events.setCommandHandler('processes:register', (name, cb) => {
       this.processes[name] = {
         state: 'unstarted',
@@ -16,7 +39,7 @@ class ProcessManager {
 
     self.events.setCommandHandler('processes:launch', (name, cb) => {
       let process = self.processes[name];
-      if (process.state != 'unstarted') {
+      if (process.state !== 'unstarted') {
         return cb();
       }
       process.state = 'starting';
@@ -28,7 +51,6 @@ class ProcessManager {
       ]);
     });
   }
-
 }
 
 module.exports = ProcessManager;
diff --git a/lib/modules/ipfs/index.js b/lib/modules/ipfs/index.js
index 7eec9a8f..1601a66a 100644
--- a/lib/modules/ipfs/index.js
+++ b/lib/modules/ipfs/index.js
@@ -34,6 +34,7 @@ class IPFS {
         this.listenToCommands();
         this.registerConsoleCommands();
         self.startProcess(() => {});
+        this.events.request("processes:launch", "ipfs", () => {});
       });
     }
   }
@@ -123,8 +124,15 @@ class IPFS {
       events: self.events,
       storageConfig: self.storageConfig,
       webServerConfig: self.webServerConfig,
+<<<<<<< HEAD
       blockchainConfig: self.blockchainConfig,
       corsParts: self.embark.config.corsParts
+||||||| merged common ancestors
+      blockchainConfig: self.blockchainConfig
+=======
+      blockchainConfig: self.blockchainConfig,
+      embark: self.embark
+>>>>>>> small conflicts
     });
     self.logger.trace(`Storage module: Launching ipfs process...`);
     return storageProcessesLauncher.launchProcess('ipfs', callback);
diff --git a/lib/modules/storage/storageProcessesLauncher.js b/lib/modules/storage/storageProcessesLauncher.js
index 82af571a..d0220634 100644
--- a/lib/modules/storage/storageProcessesLauncher.js
+++ b/lib/modules/storage/storageProcessesLauncher.js
@@ -18,6 +18,7 @@ class StorageProcessesLauncher {
     this.storageConfig = options.storageConfig;
     this.webServerConfig = options.webServerConfig;
     this.blockchainConfig = options.blockchainConfig;
+    this.embark = options.embark;
     this.processes = {};
     this.corsParts = options.corsParts || [];
 
@@ -106,8 +107,10 @@ class StorageProcessesLauncher {
       self.logger.info(__(`Starting %s process`, storageName).cyan);
       self.processes[storageName] = new ProcessLauncher({
         modulePath: filePath,
+        name: storageName,
         logger: self.logger,
         events: self.events,
+        embark: self.embark,
         silent: self.logger.logLevel !== 'trace',
         exitCallback: self.processExited.bind(this, storageName)
       });
diff --git a/lib/modules/webserver/server.js b/lib/modules/webserver/server.js
index eb985b74..a4ef7b87 100644
--- a/lib/modules/webserver/server.js
+++ b/lib/modules/webserver/server.js
@@ -65,6 +65,7 @@ class Server {
       next();
     });
     this.app.use(main);
+    this.app.use(cors());
     this.app.use('/coverage', coverage);
     this.app.use(coverageStyle);
 
@@ -73,7 +74,7 @@ class Server {
     this.app.use(cors());
 
     app.use(bodyParser.json()); // support json encoded bodies
-    app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
+    app.use(bodyParser.urlencoded({extended: true})); // support encoded bodies
 
     expressWebSocket(this.app);