From 1c125a019c34c42d539b99c3f7b9e077d43706cc Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Wed, 16 May 2018 16:41:15 -0400 Subject: [PATCH] conflict in en.json --- lib/i18n/locales/en.json | 1 + lib/pipeline/pipeline.js | 24 ++++---- lib/process/processLauncher.js | 103 +++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 14 deletions(-) create mode 100644 lib/process/processLauncher.js diff --git a/lib/i18n/locales/en.json b/lib/i18n/locales/en.json index 988c3a2e0..da47b60be 100644 --- a/lib/i18n/locales/en.json +++ b/lib/i18n/locales/en.json @@ -96,6 +96,7 @@ "help": "help", "quit": "quit", "Error Compiling/Building contracts: ": "Error Compiling/Building contracts: ", + "file not found, creating it...": "file not found, creating it..." "{{className}} has code associated to it but it's configured as an instanceOf {{parentContractName}}": "{{className}} has code associated to it but it's configured as an instanceOf {{parentContractName}}", "downloading {{packageName}} {{version}}....": "downloading {{packageName}} {{version}}....", "Swarm node is offline...": "Swarm node is offline...", diff --git a/lib/pipeline/pipeline.js b/lib/pipeline/pipeline.js index 579dd4cc9..e7fad4f1a 100644 --- a/lib/pipeline/pipeline.js +++ b/lib/pipeline/pipeline.js @@ -1,6 +1,6 @@ const fs = require('../core/fs.js'); const async = require('async'); -const child_process = require('child_process'); +const ProcessLauncher = require('../process/processLauncher'); const utils = require('../utils/utils.js'); const constants = require('../constants'); @@ -69,22 +69,18 @@ class Pipeline { // JS files async.waterfall([ function runWebpack(next) { - const webpackProcess = child_process.fork(utils.joinPath(__dirname, 'webpackProcess.js')); + const webpackProcess = new ProcessLauncher({ + modulePath: utils.joinPath(__dirname, 'webpackProcess.js'), + logger: self.logger, + normalizeInput: self.normalizeInput + }); + console.log('Starting process'); webpackProcess.send({action: constants.pipeline.init, options: {}}); webpackProcess.send({action: constants.pipeline.build, file, importsList}); - webpackProcess.on('message', function (msg) { - if (msg.result === constants.pipeline.built) { - webpackProcess.disconnect(); - return next(msg.error); - } - - if (msg.result === constants.process.log) { - if (self.logger[msg.type]) { - return self.logger[msg.type](self.normalizeInput(msg.message)); - } - self.logger.debug(self.normalizeInput(msg.message)); - } + webpackProcess.subscribeTo('result', constants.pipeline.built, (msg) => { + webpackProcess.disconnect(); + return next(msg.error); }); }, diff --git a/lib/process/processLauncher.js b/lib/process/processLauncher.js new file mode 100644 index 000000000..0e0550ee2 --- /dev/null +++ b/lib/process/processLauncher.js @@ -0,0 +1,103 @@ +const child_process = require('child_process'); +const constants = require('../constants'); + +class ProcessLauncher { + constructor(options) { + this.process = child_process.fork(options.modulePath); + this.logger = options.logger; + this.normalizeInput = options.normalizeInput; + + this.subscriptions = {}; + this._subscribeToMessages(); + } + + _subscribeToMessages() { + const self = this; + this.process.on('message', (msg) => { + console.log('Received message', msg); + if (msg.result === constants.process.log) { + if (self.logger[msg.type]) { + return self.logger[msg.type](self.normalizeInput(msg.message)); + } + self.logger.debug(self.normalizeInput(msg.message)); + return; + } + + const messageKeys = Object.keys(msg); + const subscriptionsKeys = Object.keys(self.subscriptions); + let subscriptions; + let messageKey; + // Find if the message contains a key that we are subscribed to + messageKeys.some(_messageKey => { + return subscriptionsKeys.some(subscriptionKey => { + if (_messageKey === subscriptionKey) { + subscriptions = self.subscriptions[subscriptionKey]; + messageKey = _messageKey; + return true; + } + return false; + }); + }); + + if (subscriptions) { + console.log('Found the subscriptions'); + let subscription; + // Find if we are subscribed to one of the values + subscriptions.some(sub => { + if (msg[messageKey] === sub.value) { + subscription = sub; + return true; + } + return false; + }); + + if (subscription) { + console.log('Found the sub and calling'); + // We are subscribed to that message, call the callback + subscription.callback(msg); + } + } + }); + } + + subscribeTo(key, value, callback) { + console.log('Subscribe to ', key, value); + if (this.subscriptions[key]) { + this.subscriptions[key].push(value); + return; + } + this.subscriptions[key] = [{value, callback}]; + console.log('Subs', this.subscriptions); + } + + unsubscribeTo(key, value) { + if (!value) { + this.subscriptions[key] = []; + } + if (this.subscriptions[key]) { + this.subscriptions[key].filter((val, index) => { + if (val.value === value) { + this.subscriptions[key] = this.subscriptions[key].splice(index, 1); + } + }); + } + } + + unsubsribeToAll() { + this.subscriptions = {}; + } + + send() { + this.process.send(...arguments); + } + + disconnect() { + this.process.disconnect(); + } + + kill() { + this.process.kill(...arguments); + } +} + +module.exports = ProcessLauncher;