Skip to content

Commit

Permalink
Strip color and escape ansi codes from console.log (#115)
Browse files Browse the repository at this point in the history
* Strip color and escape ansi codes from console.log

* use regex to account for platform diffs?
  • Loading branch information
mhan83 authored Aug 26, 2021
1 parent 0b04e37 commit 8a91808
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"plugins": ["jest"],
"rules": {
"no-console": "off",
"no-control-regex": "off",
"promise/no-native": "off",
"promise/prefer-await-to-callbacks": "off",
"promise/prefer-await-to-then": "off",
Expand All @@ -13,4 +14,4 @@
"env": {
"jest/globals": true
}
}
}
17 changes: 11 additions & 6 deletions src/playwright-recorder.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@

const fs = require('fs');
const path = require('path');
const stream = require('stream');
const { Transform } = require('stream');
const childProcess = require('child_process');

const escapeSequenceRegex = new RegExp('[\\u001b]\\[2K|[\\u001b]\\[0G', 'g');

function playwrightRecorder () {
// console.log is saved out of reportsDir since it is cleared on startup.
const fd = fs.openSync(path.join(process.cwd(), 'console.log'), 'w+', 0o644);
const ws = stream.Writable({
write (data, encoding, cb) { fs.write(fd, data, undefined, encoding, cb); },
const ws = fs.createWriteStream(path.join(process.cwd(), 'console.log'), { flags: 'w+', mode: 0o644 });
const stripAsciiTransform = new Transform({
transform (chunk, encoding, callback) {
// list reporter uses escape codes to rewrite lines, strip them to make console output more readable
callback(null, chunk.toString().replace(escapeSequenceRegex, ''));
},
});

const [nodeBin] = process.argv;
const child = childProcess.spawn(nodeBin, [path.join(__dirname, 'playwright-runner.js'), ...process.argv.slice(2)]);

child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);
child.stdout.pipe(ws);
child.stdout.pipe(stripAsciiTransform).pipe(ws);
child.stderr.pipe(ws);

child.on('exit', (exitCode) => {
fs.closeSync(fd);
ws.end();
process.exit(exitCode);
});
}
Expand Down
3 changes: 2 additions & 1 deletion src/playwright-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,8 @@ async function run (nodeBin, runCfgPath, suiteName) {
let env = {
...process.env,
...suite.env,
PLAYWRIGHT_JUNIT_OUTPUT_NAME: path.join(cwd, '__assets__', 'junit.xml')
PLAYWRIGHT_JUNIT_OUTPUT_NAME: path.join(cwd, '__assets__', 'junit.xml'),
FORCE_COLOR: 0,
};

// Install NPM dependencies
Expand Down

0 comments on commit 8a91808

Please sign in to comment.