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

'use strict'

const { resolve } = require('path')
const Arborist = require('@npmcli/arborist')
const BaseCommand = require('../base-command.js')

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
    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 ignoreImplicitWorkspace = false

  static params = [
    'global',
    'workspace',
    'workspaces',
    'include-workspace-root',
  ]

  get parsedResponse () {
    return JSON.stringify(this.#response, null, 2)
  }

  async exec (args) {
    // one dir up from wherever node_modules lives
    const where = resolve(this.npm.dir, '..')
    const opts = {
      ...this.npm.flatOptions,
      path: where,
      forceActual: true,
    }
    const arb = new Arborist(opts)
    const tree = await arb.loadActual(opts)
    const items = await tree.querySelectorAll(args[0])
    this.buildResponse(items)

    this.npm.output(this.parsedResponse)
  }

  async execWorkspaces (args, filters) {
    await this.setWorkspaces(filters)
    const opts = {
      ...this.npm.flatOptions,
      path: this.npm.prefix,
    }
    const arb = new Arborist(opts)
    const tree = await arb.loadActual(opts)
    for (const workspacePath of this.workspacePaths) {
      let items
      if (workspacePath === tree.root.path) {
        // include-workspace-root
        items = await tree.querySelectorAll(args[0])
      } else {
        const [workspace] = await tree.querySelectorAll(`.workspace:path(${workspacePath})`)
        items = await workspace.target.querySelectorAll(args[0])
      }
      this.buildResponse(items)
    }
    this.npm.output(this.parsedResponse)
  }

  // builds a normalized inventory
  buildResponse (items) {
    for (const node of items) {
      if (!this.#seen.has(node.target.location)) {
        const item = new QuerySelectorItem(node)
        this.#response.push(item)
        this.#seen.add(item.location)
      }
    }
  }
}

module.exports = Query

Directory Contents

Dirs: 0 × Files: 68

Name Size Perms Modified Actions
5.45 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
2.20 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
11.95 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
729 B lrw-r--r-- 2026-03-17 06:55:03
Edit Download
508 B lrw-r--r-- 2026-03-17 06:55:03
Edit Download
815 B lrw-r--r-- 2026-03-17 06:55:03
Edit Download
7.08 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
3.63 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
8.91 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
8.11 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
1.37 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
2.06 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
8.10 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
5.47 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
447 B lrw-r--r-- 2026-03-17 06:55:03
Edit Download
9.22 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
2.00 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
2.44 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
3.55 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
2.33 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
602 B lrw-r--r-- 2026-03-17 06:55:03
Edit Download
6.37 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
524 B lrw-r--r-- 2026-03-17 06:55:03
Edit Download
5.62 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
4.53 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
3.93 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
6.81 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
377 B lrw-r--r-- 2026-03-17 06:55:03
Edit Download
374 B lrw-r--r-- 2026-03-17 06:55:03
Edit Download
5.11 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
5.02 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
234 B lrw-r--r-- 2026-03-17 06:55:03
Edit Download
1.34 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
16.94 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
4.20 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
8.84 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
5.88 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
2.36 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
874 B lrw-r--r-- 2026-03-17 06:55:03
Edit Download
3.47 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
343 B lrw-r--r-- 2026-03-17 06:55:03
Edit Download
11.25 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
779 B lrw-r--r-- 2026-03-17 06:55:03
Edit Download
6.33 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
2.81 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
2.16 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
1.24 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
351 B lrw-r--r-- 2026-03-17 06:55:03
Edit Download
298 B lrw-r--r-- 2026-03-17 06:55:03
Edit Download
6.90 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
2.72 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
2.63 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
572 B lrw-r--r-- 2026-03-17 06:55:03
Edit Download
2.64 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
1.87 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
1.03 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
341 B lrw-r--r-- 2026-03-17 06:55:03
Edit Download
336 B lrw-r--r-- 2026-03-17 06:55:03
Edit Download
4.44 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
336 B lrw-r--r-- 2026-03-17 06:55:03
Edit Download
6.79 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
1.52 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
4.51 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
182 B lrw-r--r-- 2026-03-17 06:55:03
Edit Download
1.70 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
3.60 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
14.38 KB lrw-r--r-- 2026-03-17 06:55:03
Edit Download
514 B lrw-r--r-- 2026-03-17 06:55:03
Edit Download

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