From 8a91808966c7817eefc51467e340fcac36b4edab Mon Sep 17 00:00:00 2001 From: Mike Han <56001373+mhan83@users.noreply.github.com> Date: Thu, 26 Aug 2021 15:23:49 -0600 Subject: [PATCH] Strip color and escape ansi codes from console.log (#115) * Strip color and escape ansi codes from console.log * use regex to account for platform diffs? --- .eslintrc | 3 ++- src/playwright-recorder.js | 17 +++++++++++------ src/playwright-runner.js | 3 ++- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/.eslintrc b/.eslintrc index 5d7e872e..634caef4 100644 --- a/.eslintrc +++ b/.eslintrc @@ -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", @@ -13,4 +14,4 @@ "env": { "jest/globals": true } -} \ No newline at end of file +} diff --git a/src/playwright-recorder.js b/src/playwright-recorder.js index c639ef3b..e97ce6d9 100644 --- a/src/playwright-recorder.js +++ b/src/playwright-recorder.js @@ -1,14 +1,19 @@ 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; @@ -16,11 +21,11 @@ function playwrightRecorder () { 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); }); } diff --git a/src/playwright-runner.js b/src/playwright-runner.js index 64ab13bd..9511608d 100644 --- a/src/playwright-runner.js +++ b/src/playwright-runner.js @@ -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