From 22076286a46499e3d0b3f8564b7ba07008317be4 Mon Sep 17 00:00:00 2001 From: Gar Date: Wed, 20 Sep 2023 12:21:45 -0700 Subject: [PATCH] fix: use strip-ansi module instead of internal regex (#6823) --- DEPENDENCIES.md | 1 + lib/commands/outdated.js | 2 +- lib/utils/ansi-trim.js | 3 --- lib/utils/format-search-stream.js | 2 +- package-lock.json | 2 ++ package.json | 2 ++ tap-snapshots/test/lib/commands/search.js.test.cjs | 14 +++++++------- test/fixtures/libnpmsearch-stream-result.js | 3 +++ test/lib/commands/hook.js | 4 ++-- test/lib/commands/org.js | 2 +- test/lib/utils/ansi-trim.js | 8 -------- 11 files changed, 20 insertions(+), 23 deletions(-) delete mode 100644 lib/utils/ansi-trim.js delete mode 100644 test/lib/utils/ansi-trim.js diff --git a/DEPENDENCIES.md b/DEPENDENCIES.md index 0adcea2c7f204..ecb65b8fc5210 100644 --- a/DEPENDENCIES.md +++ b/DEPENDENCIES.md @@ -569,6 +569,7 @@ graph LR; npm-->sigstore-tuf["@sigstore/tuf"]; npm-->spawk; npm-->ssri; + npm-->strip-ansi; npm-->supports-color; npm-->tap; npm-->tar; diff --git a/lib/commands/outdated.js b/lib/commands/outdated.js index 419fb5f5c76ac..7e5805d41806c 100644 --- a/lib/commands/outdated.js +++ b/lib/commands/outdated.js @@ -6,7 +6,7 @@ const npa = require('npm-package-arg') const pickManifest = require('npm-pick-manifest') const localeCompare = require('@isaacs/string-locale-compare')('en') -const ansiTrim = require('../utils/ansi-trim.js') +const ansiTrim = require('strip-ansi') const ArboristWorkspaceCmd = require('../arborist-cmd.js') class Outdated extends ArboristWorkspaceCmd { diff --git a/lib/utils/ansi-trim.js b/lib/utils/ansi-trim.js deleted file mode 100644 index e35a1baf63335..0000000000000 --- a/lib/utils/ansi-trim.js +++ /dev/null @@ -1,3 +0,0 @@ -const r = new RegExp('\x1b(?:\\[(?:\\d+[ABCDEFGJKSTm]|\\d+;\\d+[Hfm]|' + - '\\d+;\\d+;\\d+m|6n|s|u|\\?25[lh])|\\w)', 'g') -module.exports = str => str.replace(r, '') diff --git a/lib/utils/format-search-stream.js b/lib/utils/format-search-stream.js index ed753c27aabc8..280e96b13fa8b 100644 --- a/lib/utils/format-search-stream.js +++ b/lib/utils/format-search-stream.js @@ -1,6 +1,6 @@ const { Minipass } = require('minipass') const columnify = require('columnify') -const ansiTrim = require('../utils/ansi-trim.js') +const ansiTrim = require('strip-ansi') // This module consumes package data in the following format: // diff --git a/package-lock.json b/package-lock.json index 93f52fef38f70..aec93eff85208 100644 --- a/package-lock.json +++ b/package-lock.json @@ -68,6 +68,7 @@ "read", "semver", "ssri", + "strip-ansi", "supports-color", "tar", "text-table", @@ -146,6 +147,7 @@ "read": "^2.1.0", "semver": "^7.5.4", "ssri": "^10.0.5", + "strip-ansi": "^6.0.1", "supports-color": "^9.4.0", "tar": "^6.1.15", "text-table": "~0.2.0", diff --git a/package.json b/package.json index 53620bed0d94e..ec8ba5b118210 100644 --- a/package.json +++ b/package.json @@ -111,6 +111,7 @@ "read": "^2.1.0", "semver": "^7.5.4", "ssri": "^10.0.5", + "strip-ansi": "^6.0.1", "supports-color": "^9.4.0", "tar": "^6.1.15", "text-table": "~0.2.0", @@ -181,6 +182,7 @@ "read", "semver", "ssri", + "strip-ansi", "supports-color", "tar", "text-table", diff --git a/tap-snapshots/test/lib/commands/search.js.test.cjs b/tap-snapshots/test/lib/commands/search.js.test.cjs index 46ca8cbdabd30..8b2796c1f92c3 100644 --- a/tap-snapshots/test/lib/commands/search.js.test.cjs +++ b/tap-snapshots/test/lib/commands/search.js.test.cjs @@ -21,7 +21,7 @@ NAME | DESCRIPTION | AUTHOR | DATE libnpmhook | programmatic API… | =nlf… | 2020-11-03 | 6.0.1 | npm hooks registry npm api libnpmpublish | Programmatic API… | =nlf… | 2020-11-03 | 4.0.0 | libnpmfund | Programmatic API… | =nlf… | 2020-12-08 | 1.0.2 | npm npmcli libnpm cli git fund gitfund -@npmcli/map-workspaces | Retrieves a… | =nlf… | 2020-09-30 | 1.0.1 | npm npmcli libnpm cli workspaces map-workspaces +@npmcli/map-workspaces | Retrieves a… | =nlf… | 2020-09-30 | 1.0.1 | npm bad map npmcli libnpm cli workspaces map-workspaces libnpmversion | library to do the… | =nlf… | 2020-11-04 | 1.0.7 | @types/libnpmsearch | TypeScript… | =types | 2019-09-26 | 2.0.1 | pkg-no-desc | | =lukekarrys | 2019-09-26 | 1.0.0 | @@ -39,7 +39,7 @@ NAME | DESCRIPTION | AUTHOR | DATE libnpmhook | programmatic API… | =nlf… | 2020-11-03 | 6.0.1 | npm hooks registry npm api libnpmpublish | Programmatic API… | =nlf… | 2020-11-03 | 4.0.0 |  libnpmfund | Programmatic API… | =nlf… | 2020-12-08 | 1.0.2 | npm npmcli libnpm cli git fund gitfund -@npmcli/map-workspaces | Retrieves a… | =nlf… | 2020-09-30 | 1.0.1 | npm npmcli libnpm cli workspaces map-workspaces +@npmcli/map-workspaces | Retrieves a… | =nlf… | 2020-09-30 | 1.0.1 | npm bad map npmcli libnpm cli workspaces map-workspaces libnpmversion | library to do the… | =nlf… | 2020-11-04 | 1.0.7 |  @types/libnpmsearch | TypeScript… | =types | 2019-09-26 | 2.0.1 |  pkg-no-desc | | =lukekarrys | 2019-09-26 | 1.0.0 |  @@ -56,7 +56,7 @@ libnpmteam npm Team management APIs =nlf =ruyadorno =darcyclarke =isaacs 2020-11 libnpmhook programmatic API for managing npm registry hooks =nlf =ruyadorno =darcyclarke =isaacs 2020-11-03 6.0.1 npm hooks registry npm api libnpmpublish Programmatic API for the bits behind npm publish and unpublish =nlf =ruyadorno =darcyclarke =isaacs 2020-11-03 4.0.0 libnpmfund Programmatic API for npm fund =nlf =ruyadorno =darcyclarke =isaacs 2020-12-08 1.0.2 npm npmcli libnpm cli git fund gitfund -@npmcli/map-workspaces Retrieves a name:pathname Map for a given workspaces config =nlf =ruyadorno =darcyclarke =isaacs 2020-09-30 1.0.1 npm npmcli libnpm cli workspaces map-workspaces +@npmcli/map-workspaces Retrieves a name:pathname Map for a given workspaces config =nlf =ruyadorno =darcyclarke =isaacs 2020-09-30 1.0.1 npm bad map npmcli libnpm cli workspaces map-workspaces libnpmversion library to do the things that 'npm version' does =nlf =ruyadorno =darcyclarke =isaacs 2020-11-04 1.0.7 @types/libnpmsearch TypeScript definitions for libnpmsearch =types 2019-09-26 2.0.1 pkg-no-desc =lukekarrys 2019-09-26 1.0.0 @@ -80,7 +80,7 @@ libnpmteam | npm Team management… | =nlf… | 2020-11 libnpmhook | programmatic API… | =nlf… | 2020-11-03 | 6.0.1 | npm hooks registry npm api libnpmpublish | Programmatic API… | =nlf… | 2020-11-03 | 4.0.0 | libnpmfund | Programmatic API… | =nlf… | 2020-12-08 | 1.0.2 | npm npmcli libnpm cli git fund gitfund -@npmcli/map-workspaces | Retrieves a… | =nlf… | 2020-09-30 | 1.0.1 | npm npmcli libnpm cli workspaces map-workspaces +@npmcli/map-workspaces | Retrieves a… | =nlf… | 2020-09-30 | 1.0.1 | npm bad map npmcli libnpm cli workspaces map-workspaces libnpmversion | library to do the… | =nlf… | 2020-11-04 | 1.0.7 | @types/libnpmsearch | TypeScript… | =types | 2019-09-26 | 2.0.1 | pkg-no-desc | | =lukekarrys | 2019-09-26 | 1.0.0 | @@ -98,7 +98,7 @@ libnpmteam | npm Team management… | =nlf… | 2020-11 libnpmhook | programmatic API… | =nlf… | 2020-11-03 | 6.0.1 | npm hooks registry npm api libnpmpublish | Programmatic API… | =nlf… | 2020-11-03 | 4.0.0 | libnpmfund | Programmatic API… | =nlf… | 2020-12-08 | 1.0.2 | npm npmcli libnpm cli git fund gitfund -@npmcli/map-workspaces | Retrieves a… | =nlf… | 2020-09-30 | 1.0.1 | npm npmcli libnpm cli workspaces map-workspaces +@npmcli/map-workspaces | Retrieves a… | =nlf… | 2020-09-30 | 1.0.1 | npm bad map npmcli libnpm cli workspaces map-workspaces @types/libnpmsearch | TypeScript… | =types | 2019-09-26 | 2.0.1 | pkg-no-desc | | =lukekarrys | 2019-09-26 | 1.0.0 | ` @@ -115,7 +115,7 @@ libnpmteam | npm Team management… | =nlf… | 2020-11 libnpmhook | programmatic API… | =nlf… | 2020-11-03 | 6.0.1 | npm hooks registry npm api libnpmpublish | Programmatic API… | =nlf… | 2020-11-03 | 4.0.0 | libnpmfund | Programmatic API… | =nlf… | 2020-12-08 | 1.0.2 | npm npmcli libnpm cli git fund gitfund -@npmcli/map-workspaces | Retrieves a… | =nlf… | 2020-09-30 | 1.0.1 | npm npmcli libnpm cli workspaces map-workspaces +@npmcli/map-workspaces | Retrieves a… | =nlf… | 2020-09-30 | 1.0.1 | npm bad map npmcli libnpm cli workspaces map-workspaces @types/libnpmsearch | TypeScript… | =types | 2019-09-26 | 2.0.1 | pkg-no-desc | | =lukekarrys | 2019-09-26 | 1.0.0 | ` @@ -132,7 +132,7 @@ libnpmteam | npm Team management… | =nlf… | 2020-11 libnpmhook | programmatic API… | =nlf… | 2020-11-03 | 6.0.1 | npm hooks registry npm api libnpmpublish | Programmatic API… | =nlf… | 2020-11-03 | 4.0.0 | libnpmfund | Programmatic API… | =nlf… | 2020-12-08 | 1.0.2 | npm npmcli libnpm cli git fund gitfund -@npmcli/map-workspaces | Retrieves a… | =nlf… | 2020-09-30 | 1.0.1 | npm npmcli libnpm cli workspaces map-workspaces +@npmcli/map-workspaces | Retrieves a… | =nlf… | 2020-09-30 | 1.0.1 | npm bad map npmcli libnpm cli workspaces map-workspaces @types/libnpmsearch | TypeScript… | =types | 2019-09-26 | 2.0.1 | pkg-no-desc | | =lukekarrys | 2019-09-26 | 1.0.0 | ` diff --git a/test/fixtures/libnpmsearch-stream-result.js b/test/fixtures/libnpmsearch-stream-result.js index ac792b1c087c8..68245beff6e46 100644 --- a/test/fixtures/libnpmsearch-stream-result.js +++ b/test/fixtures/libnpmsearch-stream-result.js @@ -217,6 +217,9 @@ module.exports = [ description: 'Retrieves a name:pathname Map for a given workspaces config', keywords: [ '\x1B[33mnpm\x1B[39m', + // eslint-disable-next-line no-octal-escape + '\x1b]4;0;?\007', + '\u001b[Hbad map', 'npmcli', 'libnpm', 'cli', diff --git a/test/lib/commands/hook.js b/test/lib/commands/hook.js index e4e1214b812f3..d3de4f5c55efa 100644 --- a/test/lib/commands/hook.js +++ b/test/lib/commands/hook.js @@ -243,7 +243,7 @@ t.test('npm hook ls', async t => { 'received the correct arguments' ) t.equal(outputs[0][0], 'You have 3 hooks configured.', 'prints the correct header') - const out = require('../../../lib/utils/ansi-trim')(outputs[1][0]) + const out = require('strip-ansi')(outputs[1][0]) t.match(out, /semver.*https:\/\/google.com.*\n.*\n.*never triggered/, 'prints package hook') t.match(out, /@npmcli.*https:\/\/google.com.*\n.*\n.*triggered just now/, 'prints scope hook') t.match(out, /~npm.*https:\/\/google.com.*\n.*\n.*never triggered/, 'prints owner hook') @@ -292,7 +292,7 @@ t.test('npm hook ls, single result', async t => { 'received the correct arguments' ) t.equal(outputs[0][0], 'You have one hook configured.', 'prints the correct header') - const out = require('../../../lib/utils/ansi-trim')(outputs[1][0]) + const out = require('strip-ansi')(outputs[1][0]) t.match(out, /semver.*https:\/\/google.com.*\n.*\n.*never triggered/, 'prints package hook') }) diff --git a/test/lib/commands/org.js b/test/lib/commands/org.js index 511251e1bb096..5c239295c3343 100644 --- a/test/lib/commands/org.js +++ b/test/lib/commands/org.js @@ -1,5 +1,5 @@ const t = require('tap') -const ansiTrim = require('../../../lib/utils/ansi-trim.js') +const ansiTrim = require('strip-ansi') const mockNpm = require('../../fixtures/mock-npm') const mockOrg = async (t, { orgSize = 1, orgList = {}, ...npmOpts } = {}) => { diff --git a/test/lib/utils/ansi-trim.js b/test/lib/utils/ansi-trim.js deleted file mode 100644 index 5a9e3b0c87cba..0000000000000 --- a/test/lib/utils/ansi-trim.js +++ /dev/null @@ -1,8 +0,0 @@ -const t = require('tap') -const ansiTrim = require('../../../lib/utils/ansi-trim.js') - -t.test('basic', async t => { - const chalk = await import('chalk').then(v => v.default) - t.equal(ansiTrim('foo'), 'foo', 'does nothing if no ansis') - t.equal(ansiTrim(chalk.red('foo')), 'foo', 'strips out ansis') -})