explain.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. const { explainNode } = require('./utils/explain-dep.js')
  2. const completion = require('./utils/completion/installed-deep.js')
  3. const Arborist = require('@npmcli/arborist')
  4. const npa = require('npm-package-arg')
  5. const semver = require('semver')
  6. const { relative, resolve } = require('path')
  7. const validName = require('validate-npm-package-name')
  8. const ArboristWorkspaceCmd = require('./workspaces/arborist-cmd.js')
  9. class Explain extends ArboristWorkspaceCmd {
  10. static get description () {
  11. return 'Explain installed packages'
  12. }
  13. /* istanbul ignore next - see test/lib/load-all-commands.js */
  14. static get name () {
  15. return 'explain'
  16. }
  17. /* istanbul ignore next - see test/lib/load-all-commands.js */
  18. static get usage () {
  19. return ['<folder | specifier>']
  20. }
  21. /* istanbul ignore next - see test/lib/load-all-commands.js */
  22. static get params () {
  23. return [
  24. 'json',
  25. 'workspace',
  26. ]
  27. }
  28. /* istanbul ignore next - see test/lib/load-all-commands.js */
  29. async completion (opts) {
  30. return completion(this.npm, opts)
  31. }
  32. exec (args, cb) {
  33. this.explain(args).then(() => cb()).catch(cb)
  34. }
  35. async explain (args) {
  36. if (!args.length)
  37. throw this.usage
  38. const arb = new Arborist({ path: this.npm.prefix, ...this.npm.flatOptions })
  39. const tree = await arb.loadActual()
  40. if (this.npm.flatOptions.workspacesEnabled
  41. && this.workspaceNames
  42. && this.workspaceNames.length
  43. )
  44. this.filterSet = arb.workspaceDependencySet(tree, this.workspaceNames)
  45. else if (!this.npm.flatOptions.workspacesEnabled) {
  46. this.filterSet =
  47. arb.excludeWorkspacesDependencySet(tree)
  48. }
  49. const nodes = new Set()
  50. for (const arg of args) {
  51. for (const node of this.getNodes(tree, arg)) {
  52. const filteredOut = this.filterSet
  53. && this.filterSet.size > 0
  54. && !this.filterSet.has(node)
  55. if (!filteredOut)
  56. nodes.add(node)
  57. }
  58. }
  59. if (nodes.size === 0)
  60. throw `No dependencies found matching ${args.join(', ')}`
  61. const expls = []
  62. for (const node of nodes) {
  63. const { extraneous, dev, optional, devOptional, peer, inBundle } = node
  64. const expl = node.explain()
  65. if (extraneous)
  66. expl.extraneous = true
  67. else {
  68. expl.dev = dev
  69. expl.optional = optional
  70. expl.devOptional = devOptional
  71. expl.peer = peer
  72. expl.bundled = inBundle
  73. }
  74. expls.push(expl)
  75. }
  76. if (this.npm.flatOptions.json)
  77. this.npm.output(JSON.stringify(expls, null, 2))
  78. else {
  79. this.npm.output(expls.map(expl => {
  80. return explainNode(expl, Infinity, this.npm.color)
  81. }).join('\n\n'))
  82. }
  83. }
  84. getNodes (tree, arg) {
  85. // if it's just a name, return packages by that name
  86. const { validForOldPackages: valid } = validName(arg)
  87. if (valid)
  88. return tree.inventory.query('packageName', arg)
  89. // if it's a location, get that node
  90. const maybeLoc = arg.replace(/\\/g, '/').replace(/\/+$/, '')
  91. const nodeByLoc = tree.inventory.get(maybeLoc)
  92. if (nodeByLoc)
  93. return [nodeByLoc]
  94. // maybe a path to a node_modules folder
  95. const maybePath = relative(this.npm.prefix, resolve(maybeLoc))
  96. .replace(/\\/g, '/').replace(/\/+$/, '')
  97. const nodeByPath = tree.inventory.get(maybePath)
  98. if (nodeByPath)
  99. return [nodeByPath]
  100. // otherwise, try to select all matching nodes
  101. try {
  102. return this.getNodesByVersion(tree, arg)
  103. } catch (er) {
  104. return []
  105. }
  106. }
  107. getNodesByVersion (tree, arg) {
  108. const spec = npa(arg, this.npm.prefix)
  109. if (spec.type !== 'version' && spec.type !== 'range')
  110. return []
  111. return tree.inventory.filter(node => {
  112. return node.package.name === spec.name &&
  113. semver.satisfies(node.package.version, spec.rawSpec)
  114. })
  115. }
  116. }
  117. module.exports = Explain