Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util.stripVTControlCharacters #7157

Merged
merged 2 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion DEPENDENCIES.md
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,6 @@ graph LR;
npm-->spawk;
npm-->spdx-expression-parse;
npm-->ssri;
npm-->strip-ansi;
npm-->supports-color;
npm-->tap;
npm-->tar;
Expand Down
8 changes: 4 additions & 4 deletions lib/commands/outdated.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const os = require('os')
const { resolve } = require('path')
const os = require('node:os')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a plan to update all node modules require statements with the prefix?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So far no, just updating as we go.

const { resolve } = require('node:path')
const { stripVTControlCharacters } = require('node:util')
const pacote = require('pacote')
const table = require('text-table')
const npa = require('npm-package-arg')
Expand All @@ -22,7 +23,6 @@ class Outdated extends ArboristWorkspaceCmd {
]

async exec (args) {
const { default: stripAnsi } = await import('strip-ansi')
const global = resolve(this.npm.globalDir, '..')
const where = this.npm.global
? global
Expand Down Expand Up @@ -106,7 +106,7 @@ class Outdated extends ArboristWorkspaceCmd {

const tableOpts = {
align: ['l', 'r', 'r', 'r', 'l'],
stringLength: s => stripAnsi(s).length,
stringLength: s => stripVTControlCharacters(s).length,
}
this.npm.output(table(outTable, tableOpts))
}
Expand Down
3 changes: 1 addition & 2 deletions lib/commands/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,11 @@ class Search extends BaseCommand {

const filterStream = new FilterStream()

const { default: stripAnsi } = await import('strip-ansi')
// Grab a configured output stream that will spit out packages in the desired format.
const outputStream = await formatSearchStream({
args, // --searchinclude options are not highlighted
...opts,
}, stripAnsi)
})

log.silly('search', 'searching packages')
const p = new Pipeline(
Expand Down
19 changes: 9 additions & 10 deletions lib/utils/format-search-stream.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { stripVTControlCharacters } = require('node:util')
const { Minipass } = require('minipass')
const columnify = require('columnify')

Expand All @@ -15,8 +16,8 @@ const columnify = require('columnify')
// The returned stream will format this package data
// into a byte stream of formatted, displayable output.

module.exports = async (opts, clean) => {
return opts.json ? new JSONOutputStream() : new TextOutputStream(opts, clean)
module.exports = async (opts) => {
return opts.json ? new JSONOutputStream() : new TextOutputStream(opts)
}

class JSONOutputStream extends Minipass {
Expand All @@ -40,13 +41,11 @@ class JSONOutputStream extends Minipass {
}

class TextOutputStream extends Minipass {
#clean
#opts
#line = 0

constructor (opts, clean) {
constructor (opts) {
super()
this.#clean = clean
this.#opts = opts
}

Expand All @@ -56,17 +55,17 @@ class TextOutputStream extends Minipass {

#prettify (data) {
const pkg = {
author: data.maintainers.map((m) => `=${this.#clean(m.username)}`).join(' '),
author: data.maintainers.map((m) => `=${stripVTControlCharacters(m.username)}`).join(' '),
date: 'prehistoric',
description: this.#clean(data.description ?? ''),
description: stripVTControlCharacters(data.description ?? ''),
keywords: '',
name: this.#clean(data.name),
name: stripVTControlCharacters(data.name),
version: data.version,
}
if (Array.isArray(data.keywords)) {
pkg.keywords = data.keywords.map((k) => this.#clean(k)).join(' ')
pkg.keywords = data.keywords.map((k) => stripVTControlCharacters(k)).join(' ')
} else if (typeof data.keywords === 'string') {
pkg.keywords = this.#clean(data.keywords.replace(/[,\s]+/, ' '))
pkg.keywords = stripVTControlCharacters(data.keywords.replace(/[,\s]+/, ' '))
}
if (data.date) {
pkg.date = data.date.toISOString().split('T')[0] // remove time
Expand Down
2 changes: 0 additions & 2 deletions package-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
"semver",
"spdx-expression-parse",
"ssri",
"strip-ansi",
"supports-color",
"tar",
"text-table",
Expand Down Expand Up @@ -151,7 +150,6 @@
"semver": "^7.5.4",
"spdx-expression-parse": "^3.0.1",
"ssri": "^10.0.5",
"strip-ansi": "^7.1.0",
"supports-color": "^9.4.0",
"tar": "^6.2.0",
"text-table": "~0.2.0",
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@
"semver": "^7.5.4",
"spdx-expression-parse": "^3.0.1",
"ssri": "^10.0.5",
"strip-ansi": "^7.1.0",
"supports-color": "^9.4.0",
"tar": "^6.2.0",
"text-table": "~0.2.0",
Expand Down Expand Up @@ -186,7 +185,6 @@
"semver",
"spdx-expression-parse",
"ssri",
"strip-ansi",
"supports-color",
"tar",
"text-table",
Expand Down
7 changes: 3 additions & 4 deletions test/lib/commands/hook.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const t = require('tap')
const mockNpm = require('../../fixtures/mock-npm')
const { stripVTControlCharacters } = require('node:util')

const mockHook = async (t, { hookResponse, ...npmOpts } = {}) => {
const now = Date.now()
Expand Down Expand Up @@ -243,8 +244,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 { default: stripAnsi } = await import('strip-ansi')
const out = stripAnsi(outputs[1][0])
const out = stripVTControlCharacters(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')
Expand Down Expand Up @@ -293,8 +293,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 { default: stripAnsi } = await import('strip-ansi')
const out = stripAnsi(outputs[1][0])
const out = stripVTControlCharacters(outputs[1][0])
t.match(out, /semver.*https:\/\/google.com.*\n.*\n.*never triggered/, 'prints package hook')
})

Expand Down
10 changes: 4 additions & 6 deletions test/lib/commands/org.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const t = require('tap')
const mockNpm = require('../../fixtures/mock-npm')
const { stripVTControlCharacters } = require('node:util')

const mockOrg = async (t, { orgSize = 1, orgList = {}, ...npmOpts } = {}) => {
let setArgs = null
Expand Down Expand Up @@ -426,8 +427,7 @@ t.test('npm org ls', async t => {
},
'receieved the correct args'
)
const { default: stripAnsi } = await import('strip-ansi')
const out = stripAnsi(outputs[0][0])
const out = stripVTControlCharacters(outputs[0][0])
t.match(out, /one.*developer/, 'contains the developer member')
t.match(out, /two.*admin/, 'contains the admin member')
t.match(out, /three.*owner/, 'contains the owner member')
Expand All @@ -452,8 +452,7 @@ t.test('npm org ls - user filter', async t => {
},
'receieved the correct args'
)
const { default: stripAnsi } = await import('strip-ansi')
const out = stripAnsi(outputs[0][0])
const out = stripVTControlCharacters(outputs[0][0])
t.match(out, /username.*admin/, 'contains the filtered member')
t.notMatch(out, /missing.*admin/, 'does not contain other members')
})
Expand All @@ -476,8 +475,7 @@ t.test('npm org ls - user filter, missing user', async t => {
},
'receieved the correct args'
)
const { default: stripAnsi } = await import('strip-ansi')
const out = stripAnsi(outputs[0][0])
const out = stripVTControlCharacters(outputs[0][0])
t.notMatch(out, /username/, 'does not contain the requested member')
t.notMatch(out, /missing.*admin/, 'does not contain other members')
})
Expand Down