Skip to content

Commit

Permalink
Add flag to force Node colors
Browse files Browse the repository at this point in the history
So that process that runs in Electron renderer process can log to
the terminal if it runs in a headless mode, but to the browser console
if it runs with the browser window.

Use case: run browser tests in electron-mocha renderer with or without
browser window.
  • Loading branch information
artemave committed Jul 2, 2019
1 parent f5afa63 commit e407309
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ change the behavior of the debug logging:
| `DEBUG` | Enables/disables specific debugging namespaces. |
| `DEBUG_HIDE_DATE` | Hide date from debug output (non-TTY). |
| `DEBUG_COLORS`| Whether or not to use colors in the debug output. |
| `DEBUG_COLORS_NODE` | Assume runtime is Node, even if it looks like a browser (e.g. Electron). |
| `DEBUG_DEPTH` | Object inspection depth. |
| `DEBUG_SHOW_HIDDEN` | Shows hidden properties on inspected objects. |

Expand Down
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
* treat as a browser.
*/

if (typeof process === 'undefined' || process.type === 'renderer' || process.browser === true || process.__nwjs) {
const isBrowserNode = process.type === 'renderer' || process.browser === true || process.__nwjs;
const isBrowser = typeof process === 'undefined';

if (isBrowser || (isBrowserNode && !process.env.DEBUG_COLORS_NODE)) {
module.exports = require('./browser.js');
} else {
module.exports = require('./node.js');
Expand Down
5 changes: 5 additions & 0 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ exports.inspectOpts = Object.keys(process.env).filter(key => {
return k.toUpperCase();
});

if (prop === 'colorsNode') {
obj.colors = true;
return obj;
}

// Coerce string value into JS value
let val = process.env[key];
if (/^(yes|on|true|enabled)$/i.test(val)) {
Expand Down

0 comments on commit e407309

Please sign in to comment.