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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 104x 1x 104x 104x 104x 104x 1x 193x 183x 10x 10x 9x 1x 1x 401x 1x 793x 1x 447x 447x 10x 447x 13x 447x 24x 447x 1x 313x 313x 313x 258x 258x 281x 281x 313x 1x 193x 193x 193x 45x 45x 4x 41x 193x 193x 193x 193x 193x 1x 120x 120x 113x 120x 7x 120x 120x 120x 25x 120x 15x 15x 15x 15x 15x 15x 15x 15x 120x 6x 120x 3x 120x 1x 1x 120x 10x 120x 1x 378x 378x 378x 378x 362x 1x 378x 401x 378x 311x 311x 13x 13x 13x 311x 298x 311x 311x 311x 311x 1x 343x 82x 1x 378x 343x 18x 325x 343x 343x 343x 343x 1x 219x 1x 45x 193x 130x 45x 5x 45x 45x 1x 29x 6x 29x 1x 1x 29x 120x 84x 51x 84x 29x 1x 30x 30x 132x 116x 116x 33x 33x 33x 33x 116x 30x 1x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 447x 447x 447x 447x 447x 447x 379x 42x 447x 447x 104x 9x 104x 104x 4x 104x 3x 101x 23x 1x | 'use strict' const { resolve } = require('path') const { EOL } = require('os') const archy = require('archy') const chalk = require('chalk') const Arborist = require('@npmcli/arborist') const { breadth } = require('treeverse') const npa = require('npm-package-arg') const npm = require('./npm.js') const usageUtil = require('./utils/usage.js') const completion = require('./utils/completion/installed-deep.js') const output = require('./utils/output.js') const _depth = Symbol('depth') const _dedupe = Symbol('dedupe') const _filteredBy = Symbol('filteredBy') const _include = Symbol('include') const _invalid = Symbol('invalid') const _name = Symbol('name') const _missing = Symbol('missing') const _parent = Symbol('parent') const _problems = Symbol('problems') const _required = Symbol('required') const _type = Symbol('type') const usage = usageUtil( 'ls', 'npm ls [[<@scope>/]<pkg> ...]' ) const cmd = (args, cb) => ls(args).then(() => cb()).catch(cb) const initTree = async ({ arb, args, json }) => { const tree = await arb.loadActual() tree[_include] = args.length === 0 tree[_depth] = 0 return tree } const isGitNode = (node) => { if (!node.resolved) return try { const { type } = npa(node.resolved) return type === 'git' || type === 'hosted' } catch (err) { return false } } const isOptional = (node) => node[_type] === 'optional' || node[_type] === 'peerOptional' const isExtraneous = (node, { global }) => node.extraneous && !global const getProblems = (node, { global }) => { const problems = new Set() if (node[_missing] && !isOptional(node)) problems.add(`missing: ${node.pkgid}, required by ${node[_missing]}`) if (node[_invalid]) problems.add(`invalid: ${node.pkgid} ${node.path}`) if (isExtraneous(node, { global })) problems.add(`extraneous: ${node.pkgid} ${node.path}`) return problems } // annotates _parent and _include metadata into the resulting // item obj allowing for filtering out results during output const augmentItemWithIncludeMetadata = (node, item) => { item[_parent] = node[_parent] item[_include] = node[_include] // append current item to its parent.nodes which is the // structure expected by archy in order to print tree if (node[_include]) { // includes all ancestors of included node let p = node[_parent] while (p) { p[_include] = true p = p[_parent] } } return item } const getHumanOutputItem = (node, { args, color, global, long }) => { const { pkgid, path } = node let printable = pkgid // special formatting for top-level package name if (node.isRoot) { const hasNoPackageJson = !Object.keys(node.package).length if (hasNoPackageJson) printable = path else printable += `${long ? EOL : ' '}${path}` } const highlightDepName = color && args.length && node[_filteredBy] const missingColor = isOptional(node) ? chalk.yellow.bgBlack : chalk.red.bgBlack const missingMsg = `UNMET ${isOptional(node) ? 'OPTIONAL ' : ''}DEPENDENCY` const label = ( node[_missing] ? (color ? missingColor(missingMsg) : missingMsg) + ' ' : '' ) + `${highlightDepName ? chalk.yellow.bgBlack(printable) : printable}` + ( node[_dedupe] ? ' ' + (color ? chalk.gray('deduped') : 'deduped') : '' ) + ( node[_invalid] ? ' ' + (color ? chalk.red.bgBlack('invalid') : 'invalid') : '' ) + ( isExtraneous(node, { global }) ? ' ' + (color ? chalk.green.bgBlack('extraneous') : 'extraneous') : '' ) + (isGitNode(node) ? ` (${node.resolved})` : '') + (node.isLink ? ` -> ${node.realpath}` : '') + (long ? `${EOL}${node.package.description || ''}` : '') return augmentItemWithIncludeMetadata(node, { label, nodes: [] }) } const getJsonOutputItem = (node, { global, long }) => { const item = {} if (node.version) item.version = node.version if (node.resolved) item.resolved = node.resolved item[_name] = node.name // special formatting for top-level package name const hasPackageJson = node && node.package && Object.keys(node.package).length if (node.isRoot && hasPackageJson) item.name = node.package.name || node.name if (long) { item.name = item[_name] const { dependencies, ...packageInfo } = node.package Object.assign(item, packageInfo) item.extraneous = false item.path = node.path item._dependencies = node.package.dependencies || {} item.devDependencies = node.package.devDependencies || {} item.peerDependencies = node.package.peerDependencies || {} } // augment json output items with extra metadata if (isExtraneous(node, { global })) item.extraneous = true if (node[_invalid]) item.invalid = true if (node[_missing] && !isOptional(node)) { item.required = node[_required] item.missing = true } if (node[_include] && node[_problems] && node[_problems].size) item.problems = [...node[_problems]] return augmentItemWithIncludeMetadata(node, item) } const filterByEdgesTypes = ({ dev, development, link, node, prod, production, only, tree, }) => { // filter deps by type, allows for: `npm ls --dev`, `npm ls --prod`, // `npm ls --link`, `npm ls --only=dev`, etc const filterDev = node === tree && (dev || development || /^dev(elopment)?$/.test(only)) const filterProd = node === tree && (prod || production || /^prod(uction)?$/.test(only)) const filterLink = node === tree && link return (edge) => (filterDev ? edge.dev : true) && (filterProd ? (!edge.dev && !edge.peer && !edge.peerOptional) : true) && (filterLink ? (edge.to && edge.to.isLink) : true) } const appendExtraneousChildren = ({ node, seenPaths }) => // extraneous children are not represented // in edges out, so here we add them to the list: [...node.children.values()] .filter(i => !seenPaths.has(i.path) && i.extraneous) const mapEdgesToNodes = ({ seenPaths }) => (edge) => { let node = edge.to // if the edge is linking to a missing node, we go ahead // and create a new obj that will represent the missing node if (edge.missing || (edge.optional && !node)) { const { name, spec } = edge const pkgid = `${name}@${spec}` node = { name, pkgid, [_missing]: edge.from.pkgid } } // 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, // 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 if (node.path) seenPaths.add(node.path) node[_required] = edge.spec node[_type] = edge.type node[_invalid] = edge.invalid return node } const filterByPositionalArgs = (args, { node }) => args.length > 0 ? args.some( (spec) => (node.satisfies && node.satisfies(spec)) ) : true const augmentNodesWithMetadata = ({ args, currentDepth, nodeResult, parseable, seenNodes, }) => (node) => { // 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: if (seenNodes.has(node.path)) { node = { name: node.name, version: node.version, pkgid: node.pkgid, package: node.package, path: node.path, isLink: node.isLink, realpath: node.realpath, [_invalid]: node[_invalid], [_missing]: node[_missing], [_dedupe]: true, } } else { // keeps track of already seen nodes in order to check for dedupes seenNodes.set(node.path, node) } // _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 node[_parent] = nodeResult // _include is the property that allow us to filter based on position args // e.g: `npm ls foo`, `npm ls simple-output@2` // _filteredBy is used to apply extra color info to the item that // was used in args in order to filter node[_filteredBy] = node[_include] = filterByPositionalArgs(args, { node: seenNodes.get(node.path), seenNodes }) // _depth keeps track of how many levels deep tree traversal currently is // so that we can `npm ls --depth=1` node[_depth] = currentDepth + 1 return node } const sortAlphabetically = (a, b) => a.pkgid.localeCompare(b.pkgid) const humanOutput = ({ color, result, seenItems, unicode }) => { // 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) // here is where we put items in their expected place for archy output for (const item of seenItems) { if (item[_include] && item[_parent]) item[_parent].nodes.push(item) } if (!result.nodes.length) result.nodes = ['(empty)'] const archyOutput = archy(result, '', { unicode }) return color ? chalk.reset(archyOutput) : archyOutput } const jsonOutput = ({ path, problems, result, rootError, seenItems }) => { if (problems.size) result.problems = [...problems] if (rootError) { result.problems = [ ...(result.problems || []), ...[`error in ${path}: Failed to parse root package.json`], ] result.invalid = true } // 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) // here is where we put items in their expected place for json output for (const item of seenItems) { // append current item to its parent item.dependencies obj in order // to provide a json object structure that represents the installed tree if (item[_include] && item[_parent]) { if (!item[_parent].dependencies) item[_parent].dependencies = {} item[_parent].dependencies[item[_name]] = item } } return JSON.stringify(result, null, 2) } const parseableOutput = ({ global, long, seenNodes }) => { let out = '' for (const node of seenNodes.values()) { if (node.path && node[_include]) { out += node.path if (long) { out += `:${node.pkgid}` out += node.path !== node.realpath ? `:${node.realpath}` : '' out += isExtraneous(node, { global }) ? ':EXTRANEOUS' : '' out += node[_invalid] ? ':INVALID' : '' } out += EOL } } return out.trim() } const ls = async (args) => { const { all, color, depth, json, long, global, parseable, prefix, unicode, } = npm.flatOptions const path = global ? resolve(npm.globalDir, '..') : prefix const dev = npm.config.get('dev') const development = npm.config.get('development') const link = npm.config.get('link') const only = npm.config.get('only') const prod = npm.config.get('prod') const production = npm.config.get('production') const arb = new Arborist({ global, ...npm.flatOptions, legacyPeerDeps: false, path, }) const tree = await initTree({ arb, args, global, json, }) const seenItems = new Set() const seenNodes = new Map() const problems = new Set() // defines special handling of printed depth when filtering with args const filterDefaultDepth = depth === null ? Infinity : depth const depthToPrint = (all || args.length) ? filterDefaultDepth : (depth || 0) // add root node of tree to list of seenNodes seenNodes.set(tree.path, tree) // tree traversal happens here, using treeverse.breadth const result = await breadth({ tree, // 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` getChildren (node, nodeResult) { const seenPaths = new Set() const shouldSkipChildren = !(node instanceof Arborist.Node) || (node[_depth] > depthToPrint) return (shouldSkipChildren) ? [] : [...(node.target || node).edgesOut.values()] .filter(filterByEdgesTypes({ dev, development, link, node, prod, production, only, tree, })) .map(mapEdgesToNodes({ seenPaths })) .concat(appendExtraneousChildren({ node, seenPaths })) .sort(sortAlphabetically) .map(augmentNodesWithMetadata({ args, currentDepth: node[_depth], nodeResult, parseable, seenNodes, })) }, // visit each `node` of the `tree`, returning an `item` - these are // the elements that will be used to build the final output visit (node) { node[_problems] = getProblems(node, { global }) const item = json ? getJsonOutputItem(node, { global, long }) : parseable ? null : getHumanOutputItem(node, { args, color, global, long }) // loop through list of node problems to add them to global list if (node[_include]) { for (const problem of node[_problems]) problems.add(problem) } seenItems.add(item) // return a promise so we don't blow the stack return Promise.resolve(item) }, }) // handle the special case of a broken package.json in the root folder const [rootError] = tree.errors.filter(e => e.code === 'EJSONPARSE' && e.path === resolve(path, 'package.json')) output( json ? jsonOutput({ path, problems, result, rootError, seenItems }) : parseable ? parseableOutput({ seenNodes, global, long }) : humanOutput({ color, result, seenItems, unicode }) ) // if filtering items, should exit with error code on no results if (result && !result[_include] && args.length) process.exitCode = 1 if (rootError) { throw Object.assign( new Error('Failed to parse root package.json'), { code: 'EJSONPARSE' } ) } if (problems.size) { throw Object.assign( new Error([...problems].join(EOL)), { code: 'ELSPROBLEMS' } ) } } module.exports = Object.assign(cmd, { usage, completion }) |