From 6646c02aeb27cbb40566e2f13ac384a6567a60b1 Mon Sep 17 00:00:00 2001 From: Pamella Bezerra Date: Mon, 8 Jan 2024 11:42:16 -0300 Subject: [PATCH 1/4] Embed the ansi-regex code into the project to fix an import dependency issue --- lib/index.js | 2 +- lib/utils/stripAnsi.js | 27 +++++++++++++++++++++++++++ package.json | 3 +-- 3 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 lib/utils/stripAnsi.js diff --git a/lib/index.js b/lib/index.js index 147bd9e..7694509 100644 --- a/lib/index.js +++ b/lib/index.js @@ -20,7 +20,7 @@ const get = require('lodash.get'); const each = require('lodash.foreach'); const fromPairs = require('lodash.frompairs'); const toPairs = require('lodash.topairs'); -const stripAnsi = require('strip-ansi'); +const stripAnsi = require('./utils/stripAnsi'); function getAssetPath(compilation, name) { return path.join(compilation.getPath(compilation.compiler.outputPath), name.split('?')[0]); diff --git a/lib/utils/stripAnsi.js b/lib/utils/stripAnsi.js new file mode 100644 index 0000000..a301b8c --- /dev/null +++ b/lib/utils/stripAnsi.js @@ -0,0 +1,27 @@ +/* + * This code is based on the strip-ansi library by Chalk. + * Source: https://github.com/chalk/strip-ansi + */ + +function ansiRegex({ onlyFirst = false } = {}) { + const pattern = [ + '[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)', + '(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))', + ].join('|'); + + return new RegExp(pattern, onlyFirst ? undefined : 'g'); +} + +function stripAnsi(string) { + if (typeof string !== 'string') { + throw new TypeError(`Expected a \`string\`, got \`${typeof string}\``); + } + + // Even though the regex is global, we don't need to reset the `.lastIndex` + // because unlike `.exec()` and `.test()`, `.replace()` does it automatically + // and doing it manually has a performance penalty. + const regex = ansiRegex(); + return string.replace(regex, ''); +} + +module.exports = stripAnsi; diff --git a/package.json b/package.json index 2def6c1..1f82a1f 100644 --- a/package.json +++ b/package.json @@ -43,8 +43,7 @@ "lodash.foreach": "^4.5.0", "lodash.frompairs": "^4.0.1", "lodash.get": "^4.4.2", - "lodash.topairs": "^4.3.0", - "strip-ansi": "^6.0.1" + "lodash.topairs": "^4.3.0" }, "devDependencies": { "@types/babel__traverse": "7.0.6", From 93975189b6c86c8933b8b7188da8adb3fa6f7553 Mon Sep 17 00:00:00 2001 From: Pamella Bezerra Date: Mon, 8 Jan 2024 11:54:29 -0300 Subject: [PATCH 2/4] Temp change to run GA workflow --- .github/workflows/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/config.yml b/.github/workflows/config.yml index 4abcce4..c6fd5a8 100644 --- a/.github/workflows/config.yml +++ b/.github/workflows/config.yml @@ -2,7 +2,7 @@ name: test on: push: - branches: [ master ] + branches: [ master, fix-strip-ansi-import ] pull_request: branches: [ master ] From f6f6b3439b16af9183e1629c516b26087c9517a0 Mon Sep 17 00:00:00 2001 From: Pamella Bezerra Date: Mon, 8 Jan 2024 12:34:38 -0300 Subject: [PATCH 3/4] Add unit tests --- lib/utils/tests/stripAnsi.test.js | 48 +++++++++++++++++++++++++++++++ tsconfig.json | 2 +- 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 lib/utils/tests/stripAnsi.test.js diff --git a/lib/utils/tests/stripAnsi.test.js b/lib/utils/tests/stripAnsi.test.js new file mode 100644 index 0000000..288aa3a --- /dev/null +++ b/lib/utils/tests/stripAnsi.test.js @@ -0,0 +1,48 @@ +/* eslint-env jest */ +'use strict'; + +const stripAnsi = require('../stripAnsi'); + +describe('stripAnsi tests', () => { + it('It should handle an empty string', () => { + const output = stripAnsi(''); + expect(output).toBe(''); + }); + + it('It should return the same string if there are no ANSI codes', () => { + const input = 'Hello'; + const output = stripAnsi(input); + expect(output).toBe('Hello'); + }); + + it('It should remove ANSI codes from a string', () => { + const input = '\u001B[4mHello\u001B[0m'; + const output = stripAnsi(input); + expect(output).toBe('Hello'); + }); + + it('It should strip color from string', () => { + const input = '\u001B[0m\u001B[4m\u001B[42m\u001B[31mHe\u001B[39m\u001B[49m\u001B[24mllo\u001B[0m'; + const output = stripAnsi(input); + expect(output).toBe('Hello'); + }); + + it('It should strip color from ls command', () => { + const input = + '\u001B[00m\u001B[01;34mHello\u001B[00m'; + const output = stripAnsi(input); + expect(output).toBe('Hello'); + }); + + it('It should reset;setfg;setbg;italics;strike;underline sequence from string', () => { + const input = '\u001B[0;33;49;3;9;4mHello\u001B[0m'; + const output = stripAnsi(input); + expect(output).toBe('Hello'); + }); + + it('It should strip link from terminal link', () => { + const input = '\u001B]8;;https://github.com\u0007click\u001B]8;;\u0007'; + const output = stripAnsi(input); + expect(output).toBe('click'); + }); +}); diff --git a/tsconfig.json b/tsconfig.json index ca34c20..e5d289c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -33,7 +33,7 @@ "spec", "examples", "dist", - "tests", + "**/tests", "coverage" ] } From c934ed7495a6ee570b3382a9c985a29bf24f2e51 Mon Sep 17 00:00:00 2001 From: Pamella Bezerra Date: Mon, 8 Jan 2024 12:35:07 -0300 Subject: [PATCH 4/4] Undo temp change --- .github/workflows/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/config.yml b/.github/workflows/config.yml index c6fd5a8..4abcce4 100644 --- a/.github/workflows/config.yml +++ b/.github/workflows/config.yml @@ -2,7 +2,7 @@ name: test on: push: - branches: [ master, fix-strip-ansi-import ] + branches: [ master ] pull_request: branches: [ master ]