From 6d0f4b67ae00ea57f1981a73a3d4c14b801bc8cd Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 27 Jul 2018 17:33:50 -0400 Subject: [PATCH] move processes into core --- lib/core/processes/processLauncher.js | 3 - .../processes/processLauncher.js~HEAD} | 7 ++- lib/core/processes/processWrapper.js | 4 -- .../processes/processWrapper.js~HEAD} | 18 +++--- lib/processes/eventsWrapper.js | 55 ------------------- 5 files changed, 14 insertions(+), 73 deletions(-) rename lib/{processes/processLauncher.js => core/processes/processLauncher.js~HEAD} (97%) rename lib/{processes/processWrapper.js => core/processes/processWrapper.js~HEAD} (88%) delete mode 100644 lib/processes/eventsWrapper.js diff --git a/lib/core/processes/processLauncher.js b/lib/core/processes/processLauncher.js index 7ee80fc5f..88ad8f4c3 100644 --- a/lib/core/processes/processLauncher.js +++ b/lib/core/processes/processLauncher.js @@ -42,9 +42,6 @@ class ProcessLauncher { _subscribeToMessages() { const self = this; this.process.on('message', (msg) => { - if (msg.error) { - self.logger.error(msg.error); - } if (msg.result === constants.process.log) { return self._handleLog(msg); } diff --git a/lib/processes/processLauncher.js b/lib/core/processes/processLauncher.js~HEAD similarity index 97% rename from lib/processes/processLauncher.js rename to lib/core/processes/processLauncher.js~HEAD index 9c2c1b1d1..1bbf9153f 100644 --- a/lib/processes/processLauncher.js +++ b/lib/core/processes/processLauncher.js~HEAD @@ -1,7 +1,7 @@ const child_process = require('child_process'); -const constants = require('../constants'); +const constants = require('../../constants'); const path = require('path'); -const utils = require('../utils/utils'); +const utils = require('../../utils/utils'); class ProcessLauncher { @@ -29,6 +29,9 @@ class ProcessLauncher { _subscribeToMessages() { const self = this; this.process.on('message', (msg) => { + if (msg.error) { + self.logger.error(msg.error); + } if (msg.result === constants.process.log) { return self._handleLog(msg); } diff --git a/lib/core/processes/processWrapper.js b/lib/core/processes/processWrapper.js index 26137474d..5948444ec 100644 --- a/lib/core/processes/processWrapper.js +++ b/lib/core/processes/processWrapper.js @@ -1,7 +1,3 @@ -process.on('uncaughtException', function(e){ - process.send({error: e.stack}); -}); - const constants = require('../../constants'); const Events = require('./eventsWrapper'); diff --git a/lib/processes/processWrapper.js b/lib/core/processes/processWrapper.js~HEAD similarity index 88% rename from lib/processes/processWrapper.js rename to lib/core/processes/processWrapper.js~HEAD index 30108fe45..3b934a106 100644 --- a/lib/processes/processWrapper.js +++ b/lib/core/processes/processWrapper.js~HEAD @@ -1,14 +1,14 @@ -const constants = require('../constants'); +process.on('uncaughtException', function(e){ + process.send({error: e.stack}); +}); + +const constants = require('../../constants'); const Events = require('./eventsWrapper'); -// Override process.chdir so that we have a partial-implementation PWD for Windows -const realChdir = process.chdir; -process.chdir = (...args) => { - if (!process.env.PWD) { - process.env.PWD = process.cwd(); - } - realChdir(...args); -}; +// Set PWD to CWD since Windows doesn't have a value for PWD +if (!process.env.PWD) { + process.env.PWD = process.cwd(); +} class ProcessWrapper { diff --git a/lib/processes/eventsWrapper.js b/lib/processes/eventsWrapper.js deleted file mode 100644 index de1021585..000000000 --- a/lib/processes/eventsWrapper.js +++ /dev/null @@ -1,55 +0,0 @@ -const uuid = require('uuid/v1'); -const constants = require('../constants'); - -class Events { - - /** - * Constructs an event wrapper for processes. - * Handles sending an event message to the parent process and waiting for its response - * No need to create an instance of eventsWrapper for your own process, just extend ProcessWrapper - * Then, you an use `this.events.[on|request]` with the usual parameters you would use - */ - constructor() { - this.subscribedEvents = {}; - this.listenToParentProcess(); - } - - listenToParentProcess() { - process.on('message', (msg) => { - if (!msg.event || msg.event !== constants.process.events.response) { - return; - } - if (!this.subscribedEvents[msg.eventId]) { - return; - } - this.subscribedEvents[msg.eventId](msg.result); - }); - } - - sendEvent() { - const eventType = arguments[0]; - const requestName = arguments[1]; - - let args = [].slice.call(arguments, 2); - const eventId = uuid(); - this.subscribedEvents[eventId] = args[args.length - 1]; - args = args.splice(0, args.length - 2); - - process.send({ - event: eventType, - requestName, - args, - eventId: eventId - }); - } - - on() { - this.sendEvent(constants.process.events.on, ...arguments); - } - - request() { - this.sendEvent(constants.process.events.request, ...arguments); - } -} - -module.exports = Events;