installed-deep.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. const { resolve } = require('path')
  2. const Arborist = require('@npmcli/arborist')
  3. const localeCompare = require('@isaacs/string-locale-compare')('en')
  4. const installedDeep = async (npm) => {
  5. const {
  6. depth,
  7. global,
  8. prefix,
  9. workspacesEnabled,
  10. } = npm.flatOptions
  11. const getValues = (tree) =>
  12. [...tree.inventory.values()]
  13. .filter(i => i.location !== '' && !i.isRoot)
  14. .map(i => {
  15. return i
  16. })
  17. .filter(i => (i.depth - 1) <= depth)
  18. .sort((a, b) => (a.depth - b.depth) || localeCompare(a.name, b.name))
  19. const res = new Set()
  20. const gArb = new Arborist({
  21. global: true,
  22. path: resolve(npm.globalDir, '..'),
  23. workspacesEnabled,
  24. })
  25. const gTree = await gArb.loadActual({ global: true })
  26. for (const node of getValues(gTree))
  27. res.add(global ? node.name : [node.name, '-g'])
  28. if (!global) {
  29. const arb = new Arborist({ global: false, path: prefix, workspacesEnabled })
  30. const tree = await arb.loadActual()
  31. for (const node of getValues(tree))
  32. res.add(node.name)
  33. }
  34. return [...res]
  35. }
  36. module.exports = installedDeep