2018-05-15 19:56:15 +00:00
|
|
|
const constants = require('../constants');
|
2018-05-16 15:25:06 +00:00
|
|
|
const Events = require('./eventsWrapper');
|
2018-05-15 19:56:15 +00:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
};
|
|
|
|
|
|
|
|
class ProcessWrapper {
|
2018-05-16 15:58:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class from which process extend. Should not be instantiated alone.
|
|
|
|
* Manages the log interception so that all console.* get sent back to the parent process
|
2018-05-16 16:03:39 +00:00
|
|
|
* Also creates an Events instance. To use it, just do `this.events.[on|request]`
|
2018-05-16 15:58:18 +00:00
|
|
|
*
|
|
|
|
* @param {Options} _options Nothing for now
|
|
|
|
*/
|
2018-05-15 19:56:15 +00:00
|
|
|
constructor(_options) {
|
|
|
|
this.interceptLogs();
|
2018-05-16 15:25:06 +00:00
|
|
|
this.events = new Events();
|
2018-05-15 19:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interceptLogs() {
|
|
|
|
const context = {};
|
|
|
|
context.console = console;
|
|
|
|
|
2018-05-16 15:58:18 +00:00
|
|
|
context.console.log = this._log.bind(this, 'log');
|
|
|
|
context.console.warn = this._log.bind(this, 'warn');
|
2018-05-24 20:34:27 +00:00
|
|
|
context.console.error = this._log.bind(this, 'error');
|
2018-05-16 15:58:18 +00:00
|
|
|
context.console.info = this._log.bind(this, 'info');
|
|
|
|
context.console.debug = this._log.bind(this, 'debug');
|
|
|
|
context.console.trace = this._log.bind(this, 'trace');
|
|
|
|
context.console.dir = this._log.bind(this, 'dir');
|
2018-05-15 19:56:15 +00:00
|
|
|
}
|
|
|
|
|
2018-05-16 15:58:18 +00:00
|
|
|
_log(type, ...messages) {
|
|
|
|
const isHardSource = messages.some(message => {
|
|
|
|
return (typeof message === 'string' && message.indexOf('hard-source') > -1);
|
|
|
|
});
|
|
|
|
if (isHardSource) {
|
2018-05-15 19:56:15 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-05-15 21:08:08 +00:00
|
|
|
process.send({result: constants.process.log, message: messages, type});
|
2018-05-15 19:56:15 +00:00
|
|
|
}
|
2018-05-22 18:13:56 +00:00
|
|
|
|
|
|
|
send() {
|
|
|
|
process.send(...arguments);
|
|
|
|
}
|
2018-05-15 19:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
process.on('exit', () => {
|
|
|
|
process.exit(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = ProcessWrapper;
|