Skip to content

Isaacs/doctor #1638

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
refactor and test for installed-shallow completion
  • Loading branch information
isaacs committed Aug 7, 2020
commit eaf9ba659a2a5ec3b03d36689aea0747efef0e3c
95 changes: 14 additions & 81 deletions lib/utils/completion/installed-shallow.js
Original file line number Diff line number Diff line change
@@ -1,87 +1,20 @@
const npm = require('../../npm.js')
const { promisify } = require('util')
const readdir = promisify(require('readdir-scoped-modules'))

module.exports = installedShallow
const names = global => readdir(global ? npm.globalDir : npm.localDir)

var npm = require('../../npm.js')
var fs = require('graceful-fs')
var path = require('path')
var readJson = require('read-package-json')
var asyncMap = require('slide').asyncMap

function installedShallow (opts, filter, cb) {
if (typeof cb !== 'function') {
cb = filter
filter = null
}
var conf = opts.conf
var args = conf.argv.remain
if (args.length > 3) return cb()
var local
var global
var localDir = npm.dir
var globalDir = npm.globalDir
if (npm.config.get('global')) {
local = []
next()
} else {
fs.readdir(localDir, function (er, pkgs) {
local = (pkgs || []).filter(function (p) {
return p.charAt(0) !== '.'
})
next()
})
const installedShallow = async opts => {
const { conf: { argv: { remain } } } = opts
if (remain.length > 3) {
return null
}

fs.readdir(globalDir, function (er, pkgs) {
global = (pkgs || []).filter(function (p) {
return p.charAt(0) !== '.'
})
next()
})
function next () {
if (!local || !global) return
filterInstalled(local, global, filter, cb)
}
const { global } = npm.flatOptions
const locals = global ? [] : await names(false)
const globals = (await names(true)).map(n => global ? n : `${n} -g`)
return [...locals, ...globals]
}

function filterInstalled (local, global, filter, cb) {
var fl
var fg

if (!filter) {
fl = local
fg = global
return next()
}

asyncMap(local, function (p, cb) {
readJson(path.join(npm.dir, p, 'package.json'), function (er, d) {
if (!d || !filter(d)) return cb(null, [])
return cb(null, d.name)
})
}, function (er, local) {
fl = local || []
next()
})

var globalDir = npm.globalDir
asyncMap(global, function (p, cb) {
readJson(path.join(globalDir, p, 'package.json'), function (er, d) {
if (!d || !filter(d)) return cb(null, [])
return cb(null, d.name)
})
}, function (er, global) {
fg = global || []
next()
})

function next () {
if (!fg || !fl) return
if (!npm.config.get('global')) {
fg = fg.map(function (g) {
return [g, '-g']
})
}
console.error('filtered', fl, fg)
return cb(null, fl.concat(fg))
}
}
module.exports = (opts, cb) =>
installedShallow(opts).then(list => cb(null, list)).catch(cb)
111 changes: 111 additions & 0 deletions test/lib/utils/completion/installed-shallow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
const requireInject = require('require-inject')
const flatOptions = { global: false }
const npm = { flatOptions }
const t = require('tap')
const { resolve } = require('path')

const p = '../../../../lib/utils/completion/installed-shallow.js'
const installed = requireInject(p, {
'../../../../lib/npm.js': npm
})

t.test('global not set, include globals with -g', t => {
const dir = t.testdir({
global: {
node_modules: {
x: {},
'@scope': {
y: {}
}
}
},
local: {
node_modules: {
a: {},
'@scope': {
b: {}
}
}
}
})
npm.globalDir = resolve(dir, 'global/node_modules')
npm.localDir = resolve(dir, 'local/node_modules')
flatOptions.global = false
const opt = { conf: { argv: { remain: [] } } }
installed(opt, (er, res) => {
if (er) {
throw er
}
t.strictSame(res.sort(), [
'@scope/y -g',
'x -g',
'a',
'@scope/b'
].sort())
t.end()
})
})

t.test('global set, include globals and not locals', t => {
const dir = t.testdir({
global: {
node_modules: {
x: {},
'@scope': {
y: {}
}
}
},
local: {
node_modules: {
a: {},
'@scope': {
b: {}
}
}
}
})
npm.globalDir = resolve(dir, 'global/node_modules')
npm.localDir = resolve(dir, 'local/node_modules')
flatOptions.global = true
const opt = { conf: { argv: { remain: [] } } }
installed(opt, (er, res) => {
t.strictSame(res.sort(), [
'@scope/y',
'x'
].sort())
t.end()
})
})

t.test('more than 3 items in argv, skip it', t => {
const dir = t.testdir({
global: {
node_modules: {
x: {},
'@scope': {
y: {}
}
}
},
local: {
node_modules: {
a: {},
'@scope': {
b: {}
}
}
}
})
npm.globalDir = resolve(dir, 'global/node_modules')
npm.localDir = resolve(dir, 'local/node_modules')
flatOptions.global = false
const opt = { conf: { argv: { remain: [1, 2, 3, 4, 5, 6] } } }
installed(opt, (er, res) => {
if (er) {
throw er
}
t.strictSame(res, null)
t.end()
})
})