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

Revert "fix: remove test coverage map" #4869

Closed
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
18 changes: 6 additions & 12 deletions lib/commands/completion.js
Expand Up @@ -29,26 +29,18 @@
// as an array.
//

const fs = require('@npmcli/fs')
const nopt = require('nopt')

const { definitions, shorthands } = require('../utils/config/index.js')
const { aliases, cmdList, plumbing } = require('../utils/cmd-list.js')
const aliasNames = Object.keys(aliases)
const fullList = cmdList.concat(aliasNames).filter(c => !plumbing.includes(c))
const nopt = require('nopt')
const configNames = Object.keys(definitions)
const shorthandNames = Object.keys(shorthands)
const allConfs = configNames.concat(shorthandNames)
const { isWindowsShell } = require('../utils/is-windows.js')
const fileExists = async (file) => {
try {
const stat = await fs.stat(file)
return stat.isFile()
} catch {
return false
}
}
const fileExists = require('../utils/file-exists.js')

const { promisify } = require('util')
const BaseCommand = require('../base-command.js')

class Completion extends BaseCommand {
Expand Down Expand Up @@ -197,10 +189,12 @@ class Completion extends BaseCommand {
}

const dumpScript = async () => {
const fs = require('fs')
const readFile = promisify(fs.readFile)
const { resolve } = require('path')
const p = resolve(__dirname, '..', 'utils', 'completion.sh')

const d = (await fs.readFile(p, 'utf8')).replace(/^#!.*?\n/, '')
const d = (await readFile(p, 'utf8')).replace(/^#!.*?\n/, '')
await new Promise((res, rej) => {
let done = false
process.stdout.on('error', er => {
Expand Down
47 changes: 20 additions & 27 deletions lib/commands/search.js
Expand Up @@ -3,33 +3,26 @@ const Pipeline = require('minipass-pipeline')
const libSearch = require('libnpmsearch')
const log = require('../utils/log-shim.js')

const formatSearchStream = require('../utils/format-search-stream.js')

function filter (data, include, exclude) {
const words = [data.name]
.concat(data.maintainers.map(m => `=${m.username}`))
.concat(data.keywords || [])
.map(f => f && f.trim && f.trim())
.filter(f => f)
.join(' ')
.toLowerCase()

if (exclude.find(e => match(words, e))) {
return false
}
const formatPackageStream = require('../search/format-package-stream.js')
const packageFilter = require('../search/package-filter.js')

return true
function prepareIncludes (args) {
return args
.map(s => s.toLowerCase())
.filter(s => s)
}

function match (words, pattern) {
if (pattern.startsWith('/')) {
if (pattern.endsWith('/')) {
pattern = pattern.slice(0, -1)
}
pattern = new RegExp(pattern.slice(1))
return words.match(pattern)
function prepareExcludes (searchexclude) {
var exclude
if (typeof searchexclude === 'string') {
exclude = searchexclude.split(/\s+/)
} else {
exclude = []
}
return words.indexOf(pattern) !== -1

return exclude
.map(s => s.toLowerCase())
.filter(s => s)
}

const BaseCommand = require('../base-command.js')
Expand Down Expand Up @@ -57,8 +50,8 @@ class Search extends BaseCommand {
const opts = {
...this.npm.flatOptions,
...this.npm.flatOptions.search,
include: args.map(s => s.toLowerCase()).filter(s => s),
exclude: this.npm.flatOptions.search.exclude.split(/\s+/),
include: prepareIncludes(args),
exclude: prepareExcludes(this.npm.flatOptions.search.exclude),
}

if (opts.include.length === 0) {
Expand All @@ -70,7 +63,7 @@ class Search extends BaseCommand {

class FilterStream extends Minipass {
write (pkg) {
if (filter(pkg, opts.include, opts.exclude)) {
if (packageFilter(pkg, opts.include, opts.exclude)) {
super.write(pkg)
}
}
Expand All @@ -80,7 +73,7 @@ class Search extends BaseCommand {

// Grab a configured output stream that will spit out packages in the
// desired format.
const outputStream = formatSearchStream({
const outputStream = formatPackageStream({
args, // --searchinclude options are not highlighted
...opts,
})
Expand Down
3 changes: 3 additions & 0 deletions lib/commands/view.js
Expand Up @@ -57,6 +57,9 @@ class View extends BaseCommand {

function getFields (d, f, pref) {
f = f || []
if (!d) {
return f
}
pref = pref || []
Object.keys(d).forEach((k) => {
if (k.charAt(0) === '_' || k.indexOf('.') !== -1) {
Expand Down
@@ -1,3 +1,6 @@
// XXX these output classes should not live in here forever. it'd be good to
// split them out, perhaps to libnpmsearch

const Minipass = require('minipass')
const columnify = require('columnify')

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

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

class JSONOutputStream extends Minipass {
#didFirst = false
constructor () {
super()
this._didFirst = false
}

write (obj) {
if (!this.#didFirst) {
if (!this._didFirst) {
super.write('[\n')
this.#didFirst = true
this._didFirst = true
} else {
super.write('\n,\n')
}

return super.write(JSON.stringify(obj))
try {
return super.write(JSON.stringify(obj))
} catch (er) {
return this.emit('error', er)
}
}

end () {
super.write(this.#didFirst ? ']\n' : '\n[]\n')
super.write(this._didFirst ? ']\n' : '\n[]\n')
super.end()
}
}
Expand All @@ -52,22 +61,22 @@ class TextOutputStream extends Minipass {
}

function prettify (data, num, opts) {
opts = opts || {}
var truncate = !opts.long

var pkg = normalizePackage(data, opts)

var columns = ['name', 'description', 'author', 'date', 'version', 'keywords']
var columns = opts.description
? ['name', 'description', 'author', 'date', 'version', 'keywords']
: ['name', 'author', 'date', 'version', 'keywords']

if (opts.parseable) {
return columns.map(function (col) {
return pkg[col] && ('' + pkg[col]).replace(/\t/g, ' ')
}).join('\t')
}

// stdout in tap is never a tty
/* istanbul ignore next */
const maxWidth = process.stdout.isTTY ? process.stdout.getWindowSize()[0] : Infinity
let output = columnify(
var output = columnify(
[pkg],
{
include: columns,
Expand All @@ -83,8 +92,8 @@ function prettify (data, num, opts) {
keywords: { maxWidth: Infinity },
},
}
).split('\n').map(line => line.slice(0, maxWidth)).join('\n')

)
output = trimToMaxWidth(output)
if (opts.color) {
output = highlightSearchTerms(output, opts.args)
}
Expand Down Expand Up @@ -131,6 +140,26 @@ function colorize (line) {
return line.split('\u0000').join(uncolor)
}

function getMaxWidth () {
var cols
try {
var tty = require('tty')
var stdout = process.stdout
cols = !tty.isatty(stdout.fd) ? Infinity : process.stdout.getWindowSize()[0]
cols = (cols === 0) ? Infinity : cols
} catch (ex) {
cols = Infinity
}
return cols
}

function trimToMaxWidth (str) {
var maxWidth = getMaxWidth()
return str.split('\n').map(function (line) {
return line.slice(0, maxWidth)
}).join('\n')
}

function highlightSearchTerms (str, terms) {
terms.forEach(function (arg, i) {
str = addColorMarker(str, arg, i)
Expand All @@ -140,10 +169,13 @@ function highlightSearchTerms (str, terms) {
}

function normalizePackage (data, opts) {
opts = opts || {}
return {
name: data.name,
description: data.description,
author: data.maintainers.map((m) => `=${m.username}`).join(' '),
description: opts.description ? data.description : '',
author: (data.maintainers || []).map(function (m) {
return '=' + m.username
}).join(' '),
keywords: Array.isArray(data.keywords)
? data.keywords.join(' ')
: typeof data.keywords === 'string'
Expand Down
43 changes: 43 additions & 0 deletions lib/search/package-filter.js
@@ -0,0 +1,43 @@
module.exports = filter
function filter (data, include, exclude, opts) {
return typeof data === 'object' &&
filterWords(data, include, exclude, opts)
}

function getWords (data, opts) {
return [data.name]
.concat((opts && opts.description) ? data.description : [])
.concat((data.maintainers || []).map(m => `=${m.name}`))
.concat(data.versions && data.versions.length && data.url && ('<' + data.url + '>'))
.concat(data.keywords || [])
.map(f => f && f.trim && f.trim())
.filter(f => f)
.join(' ')
.toLowerCase()
}

function filterWords (data, include, exclude, opts) {
var words = getWords(data, opts)
for (var i = 0, l = include.length; i < l; i++) {
if (!match(words, include[i])) {
return false
}
}

for (i = 0, l = exclude.length; i < l; i++) {
if (match(words, exclude[i])) {
return false
}
}

return true
}

function match (words, pattern) {
if (pattern.charAt(0) === '/') {
pattern = pattern.replace(/\/$/, '')
pattern = new RegExp(pattern.slice(1))
return words.match(pattern)
}
return words.indexOf(pattern) !== -1
}
2 changes: 1 addition & 1 deletion lib/utils/config/definitions.js
Expand Up @@ -1856,7 +1856,7 @@ define('searchexclude', {
`,
flatten (key, obj, flatOptions) {
flatOptions.search = flatOptions.search || { limit: 20 }
flatOptions.search.exclude = obj[key].toLowerCase()
flatOptions.search.exclude = obj[key]
},
})

Expand Down
10 changes: 10 additions & 0 deletions lib/utils/file-exists.js
@@ -0,0 +1,10 @@
const fs = require('fs')
const util = require('util')

const stat = util.promisify(fs.stat)

const fileExists = (file) => stat(file)
.then((stat) => stat.isFile())
.catch(() => false)

module.exports = fileExists
1 change: 0 additions & 1 deletion lib/utils/format-bytes.js
Expand Up @@ -23,7 +23,6 @@ const formatBytes = (bytes, space = true) => {
return `${(bytes / 1000000).toFixed(1)}${spacer}MB`
}

// GB
return `${(bytes / 1000000000).toFixed(1)}${spacer}GB`
}

Expand Down
9 changes: 9 additions & 0 deletions lib/utils/read-package-name.js
@@ -0,0 +1,9 @@
const { resolve } = require('path')
const readJson = require('read-package-json-fast')
async function readLocalPackageName (prefix) {
const filepath = resolve(prefix, 'package.json')
const json = await readJson(filepath)
return json.name
}

module.exports = readLocalPackageName
9 changes: 2 additions & 7 deletions package.json
Expand Up @@ -230,13 +230,8 @@
],
"color": 1,
"files": "test/{lib,bin,index.js}",
"timeout": 600,
"nyc-arg": [
"--exclude",
"workspaces/**",
"--exclude",
"tap-snapshots/**"
]
"coverage-map": "test/coverage-map.js",
"timeout": 600
},
"templateOSS": {
"rootRepo": false,
Expand Down