MCPcopy Create free account
hub / github.com/npm/cli / missingFromTree

Function missingFromTree

workspaces/libnpmexec/lib/index.js:41–86  ·  view source on GitHub ↗
({ spec, tree, flatOptions, isNpxTree, shallow })

Source from the content-addressed store, hash-verified

39// Returns the required manifest if the spec is missing from the tree
40// Returns the found node if it is in the tree
41const missingFromTree = async ({ spec, tree, flatOptions, isNpxTree, shallow }) => {
42 // If asking for a spec by name only (spec.raw === spec.name):
43 // - In local or global mode go with anything in the tree that matches
44 // - If looking in the npx cache check if a newer version is available
45 const npxByNameOnly = isNpxTree && spec.name === spec.raw
46 // If they gave a range and not a tag we still need to check if it's outdated.
47 if (spec.registry && spec.type !== 'tag' && !npxByNameOnly) {
48 // registry spec that is not a specific tag.
49 const nodesBySpec = tree.inventory.query('packageName', spec.name)
50 for (const node of nodesBySpec) {
51 // continue if node is not a top level node
52 if (shallow && node.depth) {
53 continue
54 }
55 if (spec.rawSpec === '*') {
56 return { node }
57 }
58 // package requested by specific version
59 if (spec.type === 'version' && (node.pkgid === spec.raw)) {
60 return { node }
61 }
62 // package requested by version range, only remaining registry type
63 // the npx tree shouldn't be ok w/ an outdated version
64 if (!isNpxTree && semver.satisfies(node.package.version, spec.rawSpec)) {
65 return { node }
66 }
67 }
68 const manifest = await getManifest(spec, flatOptions)
69 return { manifest }
70 } else {
71 // non-registry spec, or a specific tag, or name only in npx tree. Look up
72 // manifest and check resolved to see if it's in the tree.
73 const manifest = await getManifest(spec, flatOptions)
74 if (spec.type === 'directory' && !isNpxTree) {
75 return { manifest }
76 }
77 const nodesByManifest = tree.inventory.query('packageName', manifest.name)
78 for (const node of nodesByManifest) {
79 if (node.package.resolved === manifest._resolved || node.realpath === manifest._resolved) {
80 // we have a package by the same name and the same resolved destination, nothing to add.
81 return { node }
82 }
83 }
84 return { manifest }
85 }
86}
87
88// Strict-mode pre-flight for `npm exec` / `npx` lives in
89// ./strict-allow-scripts-preflight.js

Callers 1

execFunction · 0.85

Calls 3

getManifestFunction · 0.85
satisfiesMethod · 0.80
queryMethod · 0.45

Tested by

no test coverage detected