Skip to content

Commit

Permalink
fix: use util.stripVTControlCharacters instead of strip-ansi
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar committed Jan 19, 2024
1 parent 0d96080 commit d4ebfba
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
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')
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

0 comments on commit d4ebfba

Please sign in to comment.