All files / lib/utils/completion installed-deep.js

100% Statements 25/25
100% Branches 6/6
100% Functions 8/8
100% Lines 24/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45    1x 1x 1x   1x         4x   4x 6x   30x   30x 20x 20x   4x 4x 4x   4x 10x   4x 2x 2x 2x 16x     4x       4x     1x  
'use strict'
 
const { resolve } = require('path')
const Arborist = require('@npmcli/arborist')
const npm = require('../../npm.js')
 
const readNames = async () => {
  const {
    depth,
    global,
    prefix,
  } = npm.flatOptions
 
  const getValues = (tree) =>
    [...tree.inventory.values()]
      .map(i => {
        return i
      })
      .filter(i => (i.depth - 1) <= depth)
      .sort((a, b) => a.depth - b.depth)
      .sort((a, b) => a.depth === b.depth ? a.name.localeCompare(b.name) : 0)
 
  const res = new Set()
  const gArb = new Arborist({ global: true, path: resolve(npm.globalDir, '..') })
  const gTree = await gArb.loadActual({ global: true })
 
  for (const node of getValues(gTree))
    res.add(global ? node.name : [node.name, '-g'])
 
  if (!global) {
    const arb = new Arborist({ global: false, path: prefix })
    const tree = await arb.loadActual()
    for (const node of getValues(tree))
      res.add(node.name)
  }
 
  return [...res]
}
 
function installedDeep (opts, cb) {
  return readNames().then(res => cb(null, res)).catch(cb)
}
 
module.exports = installedDeep