PHP 8.2.30
Preview: query.js Size: 3.51 KB
/opt/alt/alt-nodejs24/root/usr/lib/node_modules/npm/lib/commands/query.js

const { resolve } = require('node:path')
const BaseCommand = require('../base-cmd.js')
const { log, output } = require('proc-log')

class QuerySelectorItem {
  constructor (node) {
    // all enumerable properties from the target
    Object.assign(this, node.target.package)

    // append extra info
    this.pkgid = node.target.pkgid
    this.location = node.target.location
    this.path = node.target.path
    this.realpath = node.target.realpath
    this.resolved = node.target.resolved
    this.from = []
    this.to = []
    this.dev = node.target.dev
    this.inBundle = node.target.inBundle
    this.deduped = this.from.length > 1
    this.overridden = node.overridden
    this.queryContext = node.queryContext
    for (const edge of node.target.edgesIn) {
      this.from.push(edge.from.location)
    }
    for (const [, edge] of node.target.edgesOut) {
      if (edge.to) {
        this.to.push(edge.to.location)
      }
    }
  }
}

class Query extends BaseCommand {
  #response = [] // response is the query response
  #seen = new Set() // paths we've seen so we can keep response deduped

  static description = 'Retrieve a filtered list of packages'
  static name = 'query'
  static usage = ['<selector>']

  static workspaces = true
  static ignoreImplicitWorkspace = false

  static params = [
    'global',
    'workspace',
    'workspaces',
    'include-workspace-root',
    'package-lock-only',
    'expect-results',
  ]

  constructor (...args) {
    super(...args)
    this.npm.config.set('json', true)
  }

  async exec (args) {
    const packageLock = this.npm.config.get('package-lock-only')
    const Arborist = require('@npmcli/arborist')
    const arb = new Arborist({
      ...this.npm.flatOptions,
      // one dir up from wherever node_modules lives
      path: resolve(this.npm.dir, '..'),
      forceActual: !packageLock,
    })
    let tree
    if (packageLock) {
      try {
        tree = await arb.loadVirtual()
      } catch (err) {
        log.verbose('loadVirtual', err.stack)
        throw this.usageError(
          'A package lock or shrinkwrap file is required in package-lock-only mode'
        )
      }
    } else {
      tree = await arb.loadActual()
    }
    await this.#queryTree(tree, args[0])
    this.#output()
  }

  async execWorkspaces (args) {
    await this.setWorkspaces()
    const Arborist = require('@npmcli/arborist')
    const arb = new Arborist({
      ...this.npm.flatOptions,
      path: this.npm.prefix,
    })
    // FIXME: Workspace support in query does not work as expected so this does not
    // do the same package-lock-only check as this.exec().
    // https://github.com/npm/cli/pull/6732#issuecomment-1708804921
    const tree = await arb.loadActual()
    for (const path of this.workspacePaths) {
      const wsTree = path === tree.root.path
        ? tree // --includes-workspace-root
        : await tree.querySelectorAll(`.workspace:path(${path})`).then(r => r[0].target)
      await this.#queryTree(wsTree, args[0])
    }
    this.#output()
  }

  #output () {
    this.checkExpected(this.#response.length)
    output.buffer(this.#response)
  }

  // builds a normalized inventory
  async #queryTree (tree, arg) {
    const items = await tree.querySelectorAll(arg, this.npm.flatOptions)
    for (const node of items) {
      const { location } = node.target
      if (!location || !this.#seen.has(location)) {
        const item = new QuerySelectorItem(node)
        this.#response.push(item)
        if (location) {
          this.#seen.add(item.location)
        }
      }
    }
  }
}

module.exports = Query

Directory Contents

Dirs: 0 × Files: 67

Name Size Perms Modified Actions
6.17 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
1.29 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
3.14 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
847 B lrw-r--r-- 2026-02-23 07:38:41
Edit Download
11.48 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
4.22 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
8.90 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
10.99 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
1.41 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
2.43 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
7.98 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
5.52 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
449 B lrw-r--r-- 2026-02-23 07:38:41
Edit Download
10.06 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
1.76 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
3.42 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
3.57 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
2.16 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
634 B lrw-r--r-- 2026-02-23 07:38:41
Edit Download
6.46 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
577 B lrw-r--r-- 2026-02-23 07:38:41
Edit Download
5.53 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
3.67 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
7.07 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
373 B lrw-r--r-- 2026-02-23 07:38:41
Edit Download
370 B lrw-r--r-- 2026-02-23 07:38:41
Edit Download
5.10 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
5.25 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
234 B lrw-r--r-- 2026-02-23 07:38:41
Edit Download
1.29 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
1.42 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
16.80 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
3.98 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
7.95 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
5.85 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
2.69 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
873 B lrw-r--r-- 2026-02-23 07:38:41
Edit Download
3.60 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
309 B lrw-r--r-- 2026-02-23 07:38:41
Edit Download
10.45 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
799 B lrw-r--r-- 2026-02-23 07:38:41
Edit Download
9.50 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
3.51 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
2.19 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
1.24 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
303 B lrw-r--r-- 2026-02-23 07:38:41
Edit Download
295 B lrw-r--r-- 2026-02-23 07:38:41
Edit Download
6.27 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
4.42 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
1.83 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
671 B lrw-r--r-- 2026-02-23 07:38:41
Edit Download
2.65 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
1.88 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
1.03 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
293 B lrw-r--r-- 2026-02-23 07:38:41
Edit Download
288 B lrw-r--r-- 2026-02-23 07:38:41
Edit Download
4.36 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
288 B lrw-r--r-- 2026-02-23 07:38:41
Edit Download
5.92 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
302 B lrw-r--r-- 2026-02-23 07:38:41
Edit Download
1.52 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
5.38 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
183 B lrw-r--r-- 2026-02-23 07:38:41
Edit Download
1.74 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
3.50 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
13.54 KB lrw-r--r-- 2026-02-23 07:38:41
Edit Download
527 B lrw-r--r-- 2026-02-23 07:38:41
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).