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

Function exec

workspaces/libnpmexec/lib/index.js:98–330  ·  view source on GitHub ↗
(opts)

Source from the content-addressed store, hash-verified

96 .then(manifest => manifest?.bin?.[cmd]).catch(() => null)
97
98const exec = async (opts) => {
99 const {
100 args = [],
101 call = '',
102 localBin = resolve('./node_modules/.bin'),
103 locationMsg = undefined,
104 globalBin = '',
105 globalPath,
106 // dereference values because we manipulate it later
107 packages: [...packages] = [],
108 path = '.',
109 runPath = '.',
110 scriptShell = isWindows ? process.env.ComSpec || 'cmd' : 'sh',
111 ...flatOptions
112 } = opts
113
114 const binPaths = []
115
116 let pkgPaths = opts.pkgPath
117 if (typeof pkgPaths === 'string') {
118 pkgPaths = [pkgPaths]
119 }
120 if (!pkgPaths) {
121 pkgPaths = ['.']
122 }
123 let yes = opts.yes
124 const run = () => runScript({
125 args,
126 call,
127 flatOptions,
128 locationMsg,
129 path,
130 binPaths,
131 runPath,
132 scriptShell,
133 })
134
135 // interactive mode
136 if (!call && !args.length && !packages.length) {
137 return run()
138 }
139
140 // Look in the local tree too
141 pkgPaths.push(path)
142
143 let needPackageCommandSwap = (args.length > 0) && (packages.length === 0)
144 // If they asked for a command w/o specifying a package, see if there is a
145 // bin that directly matches that name:
146 // - in any local packages (pkgPaths can have workspaces in them or just the root)
147 // - in the local tree (path)
148 // - globally
149 if (needPackageCommandSwap) {
150 // Local packages and local tree
151 for (const p of pkgPaths) {
152 if (await hasPkgBin(p, args[0], flatOptions)) {
153 // we have to install the local package into the npx cache so that its
154 // bin links get set up
155 flatOptions.installLinks = false

Callers

nothing calls this directly

Calls 15

reifyMethod · 0.95
resolveFunction · 0.85
hasPkgBinFunction · 0.85
localFileExistsFunction · 0.85
missingFromTreeFunction · 0.85
getBinFromManifestFunction · 0.85
withLockFunction · 0.85
pushMethod · 0.80
loadActualMethod · 0.80
updateMethod · 0.80
replaceMethod · 0.80
warnMethod · 0.80

Tested by

no test coverage detected