ls.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. const { resolve, relative, sep } = require('node:path')
  2. const archy = require('archy')
  3. const { breadth } = require('treeverse')
  4. const npa = require('npm-package-arg')
  5. const { output } = require('proc-log')
  6. const ArboristWorkspaceCmd = require('../arborist-cmd.js')
  7. const localeCompare = require('@isaacs/string-locale-compare')('en')
  8. const relativePrefix = `.${sep}`
  9. const _depth = Symbol('depth')
  10. const _dedupe = Symbol('dedupe')
  11. const _filteredBy = Symbol('filteredBy')
  12. const _include = Symbol('include')
  13. const _invalid = Symbol('invalid')
  14. const _name = Symbol('name')
  15. const _missing = Symbol('missing')
  16. const _parent = Symbol('parent')
  17. const _problems = Symbol('problems')
  18. const _required = Symbol('required')
  19. const _type = Symbol('type')
  20. class LS extends ArboristWorkspaceCmd {
  21. static description = 'List installed packages'
  22. static name = 'ls'
  23. static usage = ['<package-spec>']
  24. static params = [
  25. 'all',
  26. 'json',
  27. 'long',
  28. 'parseable',
  29. 'global',
  30. 'depth',
  31. 'omit',
  32. 'include',
  33. 'link',
  34. 'package-lock-only',
  35. 'unicode',
  36. ...super.params,
  37. ]
  38. static async completion (opts, npm) {
  39. const completion = require('../utils/installed-deep.js')
  40. return completion(npm, opts)
  41. }
  42. async exec (args) {
  43. const all = this.npm.config.get('all')
  44. const chalk = this.npm.chalk
  45. const depth = this.npm.config.get('depth')
  46. const global = this.npm.global
  47. const json = this.npm.config.get('json')
  48. const link = this.npm.config.get('link')
  49. const long = this.npm.config.get('long')
  50. const omit = this.npm.flatOptions.omit
  51. const parseable = this.npm.config.get('parseable')
  52. const unicode = this.npm.config.get('unicode')
  53. const packageLockOnly = this.npm.config.get('package-lock-only')
  54. const workspacesEnabled = this.npm.flatOptions.workspacesEnabled
  55. const installStrategy = this.npm.flatOptions.installStrategy
  56. const path = global ? resolve(this.npm.globalDir, '..') : this.npm.prefix
  57. const Arborist = require('@npmcli/arborist')
  58. const arb = new Arborist({
  59. global,
  60. ...this.npm.flatOptions,
  61. legacyPeerDeps: false,
  62. path,
  63. })
  64. const tree = await this.initTree({ arb, args, packageLockOnly })
  65. // filters by workspaces nodes when using -w <workspace-name>
  66. // We only have to filter the first layer of edges, so we don't explore anything that isn't part of the selected workspace set.
  67. let wsNodes
  68. if (this.workspaceNames && this.workspaceNames.length) {
  69. wsNodes = arb.workspaceNodes(tree, this.workspaceNames)
  70. }
  71. const filterBySelectedWorkspaces = edge => {
  72. if (!workspacesEnabled
  73. && edge.from.isProjectRoot
  74. && edge.to.isWorkspace
  75. ) {
  76. return false
  77. }
  78. if (!wsNodes || !wsNodes.length) {
  79. return true
  80. }
  81. if (this.npm.flatOptions.includeWorkspaceRoot
  82. && edge.to && !edge.to.isWorkspace) {
  83. return true
  84. }
  85. if (edge.from.isProjectRoot) {
  86. return (edge.to
  87. && edge.to.isWorkspace
  88. && wsNodes.includes(edge.to.target))
  89. }
  90. return true
  91. }
  92. const seenItems = new Set()
  93. const seenNodes = new Map()
  94. const problems = new Set()
  95. // defines special handling of printed depth when filtering with args
  96. const filterDefaultDepth = depth === null ? Infinity : depth
  97. const depthToPrint = (all || args.length)
  98. ? filterDefaultDepth
  99. : (depth || 0)
  100. // add root node of tree to list of seenNodes
  101. seenNodes.set(tree.path, tree)
  102. // tree traversal happens here, using treeverse.breadth
  103. const result = await breadth({
  104. tree,
  105. // recursive method, `node` is going to be the current elem (starting from the `tree` obj) that was just visited in the `visit` method below `nodeResult` is going to be the returned `item` from `visit`
  106. getChildren (node, nodeResult) {
  107. const seenPaths = new Set()
  108. const workspace = node.isWorkspace
  109. const currentDepth = workspace ? 0 : node[_depth]
  110. const shouldSkipChildren =
  111. !(node instanceof Arborist.Node) || (currentDepth > depthToPrint)
  112. return (shouldSkipChildren)
  113. ? []
  114. : [...(node.target).edgesOut.values()]
  115. .filter(filterBySelectedWorkspaces)
  116. .filter(currentDepth === 0 ? filterByEdgesTypes({
  117. link,
  118. omit,
  119. }) : () => true)
  120. .filter(installStrategy === 'linked'
  121. ? filterLinkedStrategyEdges({ node, currentDepth })
  122. : () => true)
  123. .map(mapEdgesToNodes({ seenPaths }))
  124. .concat(appendExtraneousChildren({ node, seenPaths }))
  125. .sort(sortAlphabetically)
  126. .map(augmentNodesWithMetadata({
  127. args,
  128. currentDepth,
  129. nodeResult,
  130. seenNodes,
  131. }))
  132. },
  133. // visit each `node` of the `tree`, returning an `item` - these are the elements that will be used to build the final output
  134. visit (node) {
  135. node[_problems] = getProblems(node, { global })
  136. const item = json
  137. ? getJsonOutputItem(node, { global, long })
  138. : parseable
  139. ? null
  140. : getHumanOutputItem(node, { args, chalk, global, long })
  141. // loop through list of node problems to add them to global list
  142. if (node[_include]) {
  143. for (const problem of node[_problems]) {
  144. problems.add(problem)
  145. }
  146. }
  147. seenItems.add(item)
  148. // return a promise so we don't blow the stack
  149. return Promise.resolve(item)
  150. },
  151. })
  152. // handle the special case of a broken package.json in the root folder
  153. const [rootError] = tree.errors.filter(e =>
  154. e.code === 'EJSONPARSE' && e.path === resolve(path, 'package.json'))
  155. if (json) {
  156. output.buffer(jsonOutput({ path, problems, result, rootError, seenItems }))
  157. } else {
  158. output.standard(parseable
  159. ? parseableOutput({ seenNodes, global, long })
  160. : humanOutput({ chalk, result, seenItems, unicode })
  161. )
  162. }
  163. // if filtering items, should exit with error code on no results
  164. if (result && !result[_include] && args.length) {
  165. process.exitCode = 1
  166. }
  167. if (rootError) {
  168. throw Object.assign(
  169. new Error('Failed to parse root package.json'),
  170. { code: 'EJSONPARSE' }
  171. )
  172. }
  173. const shouldThrow = problems.size &&
  174. ![...problems].every(problem => problem.startsWith('extraneous:'))
  175. if (shouldThrow) {
  176. throw Object.assign(
  177. new Error([...problems].join('\n')),
  178. { code: 'ELSPROBLEMS' }
  179. )
  180. }
  181. }
  182. async initTree ({ arb, args, packageLockOnly }) {
  183. const tree = await (
  184. packageLockOnly
  185. ? arb.loadVirtual()
  186. : arb.loadActual()
  187. )
  188. tree[_include] = args.length === 0
  189. tree[_depth] = 0
  190. return tree
  191. }
  192. }
  193. module.exports = LS
  194. const isGitNode = (node) => {
  195. if (!node.resolved) {
  196. return
  197. }
  198. try {
  199. const { type } = npa(node.resolved)
  200. return type === 'git' || type === 'hosted'
  201. } catch {
  202. return false
  203. }
  204. }
  205. const isOptional = (node) =>
  206. node[_type] === 'optional' || node[_type] === 'peerOptional'
  207. const isExtraneous = (node, { global }) =>
  208. node.extraneous && !global
  209. const getProblems = (node, { global }) => {
  210. const problems = new Set()
  211. if (node[_missing] && !isOptional(node)) {
  212. problems.add(`missing: ${node.pkgid}, required by ${node[_missing]}`)
  213. }
  214. if (node[_invalid]) {
  215. problems.add(`invalid: ${node.pkgid} ${node.path}`)
  216. }
  217. if (isExtraneous(node, { global })) {
  218. problems.add(`extraneous: ${node.pkgid} ${node.path}`)
  219. }
  220. return problems
  221. }
  222. // annotates _parent and _include metadata into the resulting item obj allowing for filtering out results during output
  223. const augmentItemWithIncludeMetadata = (node, item) => {
  224. item[_parent] = node[_parent]
  225. item[_include] = node[_include]
  226. // append current item to its parent.nodes which is the structure expected by archy in order to print tree
  227. if (node[_include]) {
  228. // includes all ancestors of included node
  229. let p = node[_parent]
  230. while (p) {
  231. p[_include] = true
  232. p = p[_parent]
  233. }
  234. }
  235. return item
  236. }
  237. const getHumanOutputItem = (node, { args, chalk, global, long }) => {
  238. const { pkgid, path } = node
  239. const workspacePkgId = chalk.blueBright(pkgid)
  240. let printable = node.isWorkspace ? workspacePkgId : pkgid
  241. // special formatting for top-level package name
  242. if (node.isRoot) {
  243. const hasNoPackageJson = !Object.keys(node.package).length
  244. if (hasNoPackageJson || global) {
  245. printable = path
  246. } else {
  247. printable += `${long ? '\n' : ' '}${path}`
  248. }
  249. }
  250. // TODO there is a LOT of overlap with lib/utils/explain-dep.js here
  251. const highlightDepName = args.length && node[_filteredBy]
  252. const missingColor = isOptional(node)
  253. ? chalk.yellow
  254. : chalk.red
  255. const missingMsg = `UNMET ${isOptional(node) ? 'OPTIONAL ' : ''}DEPENDENCY`
  256. const targetLocation = node.root
  257. ? relative(node.root.realpath, node.realpath)
  258. : node.targetLocation
  259. const invalid = node[_invalid]
  260. ? `invalid: ${node[_invalid]}`
  261. : ''
  262. const label =
  263. (
  264. node[_missing]
  265. ? missingColor(missingMsg) + ' '
  266. : ''
  267. ) +
  268. `${highlightDepName ? chalk.yellow(printable) : printable}` +
  269. (
  270. node[_dedupe]
  271. ? ' ' + chalk.dim('deduped')
  272. : ''
  273. ) +
  274. (
  275. invalid
  276. ? ' ' + chalk.red(invalid)
  277. : ''
  278. ) +
  279. (
  280. isExtraneous(node, { global })
  281. ? ' ' + chalk.red('extraneous')
  282. : ''
  283. ) +
  284. (
  285. node.overridden
  286. ? ' ' + chalk.dim('overridden')
  287. : ''
  288. ) +
  289. (isGitNode(node) ? ` (${node.resolved})` : '') +
  290. (node.isLink ? ` -> ${relativePrefix}${targetLocation}` : '') +
  291. (long ? `\n${node.package.description || ''}` : '')
  292. return augmentItemWithIncludeMetadata(node, { label, nodes: [] })
  293. }
  294. const getJsonOutputItem = (node, { global, long }) => {
  295. const item = {}
  296. if (node.version) {
  297. item.version = node.version
  298. }
  299. if (node.resolved) {
  300. item.resolved = node.resolved
  301. }
  302. // if the node is the project root, do not add the overridden flag.
  303. // the project root can't be overridden anyway, and if we add the flag it causes undesirable behavior when `npm ls --json` is ran in an empty directory since we end up printing an object with only an overridden prop
  304. if (!node.isProjectRoot) {
  305. item.overridden = node.overridden
  306. }
  307. item[_name] = node.name
  308. // special formatting for top-level package name
  309. const hasPackageJson =
  310. node && node.package && Object.keys(node.package).length
  311. if (node.isRoot && hasPackageJson) {
  312. item.name = node.package.name || node.name
  313. }
  314. if (long && !node[_missing]) {
  315. item.name = item[_name]
  316. const { dependencies, ...packageInfo } = node.package
  317. Object.assign(item, packageInfo)
  318. item.extraneous = false
  319. item.path = node.path
  320. item._dependencies = {
  321. ...node.package.dependencies,
  322. ...node.package.optionalDependencies,
  323. }
  324. item.devDependencies = node.package.devDependencies || {}
  325. item.peerDependencies = node.package.peerDependencies || {}
  326. }
  327. // augment json output items with extra metadata
  328. if (isExtraneous(node, { global })) {
  329. item.extraneous = true
  330. }
  331. if (node[_invalid]) {
  332. item.invalid = node[_invalid]
  333. }
  334. if (node[_missing] && !isOptional(node)) {
  335. item.required = node[_required]
  336. item.missing = true
  337. }
  338. if (node[_include] && node[_problems] && node[_problems].size) {
  339. item.problems = [...node[_problems]]
  340. }
  341. return augmentItemWithIncludeMetadata(node, item)
  342. }
  343. // In linked strategy, two types of edges produce false UNMET DEPENDENCYs:
  344. // 1. Workspace edges for undeclared workspaces: the lockfile records edges from root to ALL workspaces, but only declared workspaces are hoisted to root/node_modules in linked mode. Undeclared ones are intentionally absent.
  345. // 2. Dev edges on non-root packages: store package link targets have no parent in the node tree, so they are treated as "top" nodes and their devDependencies are loaded as edges. Those devDeps are never installed.
  346. const filterLinkedStrategyEdges = ({ node, currentDepth }) => {
  347. const declaredDeps = new Set(Object.keys(Object.assign({},
  348. node.target.package.dependencies,
  349. node.target.package.devDependencies,
  350. node.target.package.optionalDependencies,
  351. node.target.package.peerDependencies
  352. )))
  353. return (edge) => {
  354. // Skip workspace edges for undeclared workspaces at root level
  355. if (currentDepth === 0 && edge.type === 'workspace' && edge.missing) {
  356. if (!declaredDeps.has(edge.name)) {
  357. return false
  358. }
  359. }
  360. // Skip dev edges for non-root packages (store packages)
  361. if (currentDepth > 0 && edge.dev) {
  362. return false
  363. }
  364. return true
  365. }
  366. }
  367. const filterByEdgesTypes = ({ link, omit }) => (edge) => {
  368. for (const omitType of omit) {
  369. if (edge[omitType]) {
  370. return false
  371. }
  372. }
  373. return link ? edge.to && edge.to.isLink : true
  374. }
  375. const appendExtraneousChildren = ({ node, seenPaths }) =>
  376. // extraneous children are not represented
  377. // in edges out, so here we add them to the list:
  378. [...node.children.values()]
  379. .filter(i => !seenPaths.has(i.path) && i.extraneous)
  380. const mapEdgesToNodes = ({ seenPaths }) => (edge) => {
  381. let node = edge.to
  382. // if the edge is linking to a missing node, we go ahead and create a new obj that will represent the missing node
  383. if (edge.missing || (edge.optional && !node)) {
  384. const { name, spec } = edge
  385. const pkgid = `${name}@${spec}`
  386. node = { name, pkgid, [_missing]: edge.from.pkgid }
  387. }
  388. // keeps track of a set of seen paths to avoid the edge case in which a tree item would appear twice given that it's a children of an extraneous item
  389. // so it's marked extraneous but it will ALSO show up in edgesOuts of its parent so it ends up as two diff nodes if we don't track it
  390. if (node.path) {
  391. seenPaths.add(node.path)
  392. }
  393. node[_required] = edge.spec || '*'
  394. node[_type] = edge.type
  395. if (edge.invalid) {
  396. const spec = JSON.stringify(node[_required])
  397. const from = edge.from.location || 'the root project'
  398. node[_invalid] = (node[_invalid] ? node[_invalid] + ', ' : '') +
  399. (`${spec} from ${from}`)
  400. }
  401. return node
  402. }
  403. const filterByPositionalArgs = (args, { node }) =>
  404. args.length > 0 ? args.some(
  405. (spec) => (node.satisfies && node.satisfies(spec))
  406. ) : true
  407. const augmentNodesWithMetadata = ({
  408. args,
  409. currentDepth,
  410. nodeResult,
  411. seenNodes,
  412. }) => (node) => {
  413. // if the original edge was a deduped dep, treeverse will fail to revisit that node in tree traversal logic, so we make it so that we have a diff obj for deduped nodes:
  414. if (seenNodes.has(node.path)) {
  415. const { realpath, root } = node
  416. const targetLocation = root ? relative(root.realpath, realpath)
  417. : node.targetLocation
  418. node = {
  419. name: node.name,
  420. version: node.version,
  421. pkgid: node.pkgid,
  422. package: node.package,
  423. path: node.path,
  424. isLink: node.isLink,
  425. realpath: node.realpath,
  426. targetLocation,
  427. [_type]: node[_type],
  428. [_invalid]: node[_invalid],
  429. [_missing]: node[_missing],
  430. // if it's missing, it's not deduped, it's just missing
  431. [_dedupe]: !node[_missing],
  432. }
  433. } else {
  434. // keeps track of already seen nodes in order to check for dedupes
  435. seenNodes.set(node.path, node)
  436. }
  437. // _parent is going to be a ref to a treeverse-visited node (returned from getHumanOutputItem, getJsonOutputItem, etc) so that we have an easy shortcut to place new nodes in their right place during tree traversal
  438. node[_parent] = nodeResult
  439. // _include is the property that allow us to filter based on position args
  440. // e.g: `npm ls foo`, `npm ls simple-output@2`
  441. // _filteredBy is used to apply extra color info to the item that was used in args in order to filter
  442. node[_filteredBy] = node[_include] =
  443. filterByPositionalArgs(args, { node: seenNodes.get(node.path) })
  444. // _depth keeps track of how many levels deep tree traversal currently is so that we can `npm ls --depth=1`
  445. node[_depth] = currentDepth + 1
  446. return node
  447. }
  448. const sortAlphabetically = ({ pkgid: a }, { pkgid: b }) => localeCompare(a, b)
  449. const humanOutput = ({ chalk, result, seenItems, unicode }) => {
  450. // we need to traverse the entire tree in order to determine which items should be included (since a nested transitive included dep will make it so that all its ancestors should be displayed)
  451. // here is where we put items in their expected place for archy output
  452. for (const item of seenItems) {
  453. if (item[_include] && item[_parent]) {
  454. item[_parent].nodes.push(item)
  455. }
  456. }
  457. if (!result.nodes.length) {
  458. result.nodes = ['(empty)']
  459. }
  460. const archyOutput = archy(result, '', { unicode })
  461. return chalk.reset(archyOutput)
  462. }
  463. const jsonOutput = ({ path, problems, result, rootError, seenItems }) => {
  464. if (problems.size) {
  465. result.problems = [...problems]
  466. }
  467. if (rootError) {
  468. result.problems = [
  469. ...(result.problems || []),
  470. ...[`error in ${path}: Failed to parse root package.json`],
  471. ]
  472. result.invalid = true
  473. }
  474. // we need to traverse the entire tree in order to determine which items should be included (since a nested transitive included dep will make it so that all its ancestors should be displayed)
  475. // here is where we put items in their expected place for json output
  476. for (const item of seenItems) {
  477. // append current item to its parent item.dependencies obj in order to provide a json object structure that represents the installed tree
  478. if (item[_include] && item[_parent]) {
  479. if (!item[_parent].dependencies) {
  480. item[_parent].dependencies = {}
  481. }
  482. item[_parent].dependencies[item[_name]] = item
  483. }
  484. }
  485. return result
  486. }
  487. const parseableOutput = ({ global, long, seenNodes }) => {
  488. let out = ''
  489. for (const node of seenNodes.values()) {
  490. if (node.path && node[_include]) {
  491. out += node.path
  492. if (long) {
  493. out += `:${node.pkgid}`
  494. out += node.path !== node.realpath ? `:${node.realpath}` : ''
  495. out += isExtraneous(node, { global }) ? ':EXTRANEOUS' : ''
  496. out += node[_invalid] ? ':INVALID' : ''
  497. out += node.overridden ? ':OVERRIDDEN' : ''
  498. }
  499. out += '\n'
  500. }
  501. }
  502. return out.trim()
  503. }