embark/lib/process/processWrapper.js

55 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-05-15 19:56:15 +00:00
const constants = require('../constants');
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();
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');
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
}
}
process.on('exit', () => {
process.exit(0);
});
module.exports = ProcessWrapper;