aboutsummaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/libnpx/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'deps/npm/node_modules/libnpx/index.js')
-rw-r--r--deps/npm/node_modules/libnpx/index.js43
1 files changed, 33 insertions, 10 deletions
diff --git a/deps/npm/node_modules/libnpx/index.js b/deps/npm/node_modules/libnpx/index.js
index d42172e500..097d67cd02 100644
--- a/deps/npm/node_modules/libnpx/index.js
+++ b/deps/npm/node_modules/libnpx/index.js
@@ -9,8 +9,6 @@ const parseArgs = require('./parse-args.js')
const path = require('path')
const which = promisify(require('which'))
-const PATH_SEP = process.platform === 'win32' ? ';' : ':'
-
module.exports = npx
module.exports.parseArgs = parseArgs
function npx (argv) {
@@ -41,7 +39,7 @@ function npx (argv) {
return localBinPath(process.cwd()).then(local => {
if (local) {
// Local project paths take priority. Go ahead and prepend it.
- process.env.PATH = `${local}${PATH_SEP}${process.env.PATH}`
+ process.env.PATH = `${local}${path.delimiter}${process.env.PATH}`
}
return Promise.all([
// Figuring out if a command exists, early on, lets us maybe
@@ -85,6 +83,9 @@ function npx (argv) {
argv.package.length === 1
) {
return promisify(fs.readdir)(results.bin).then(bins => {
+ if (process.platform === 'win32') {
+ bins = bins.filter(b => b !== 'etc' && b !== 'node_modules')
+ }
const cmd = new RegExp(`^${argv.command}(?:\\.cmd)?$`, 'i')
const matching = bins.find(b => b.match(cmd))
return path.resolve(results.bin, bins[matching] || bins[0])
@@ -128,7 +129,7 @@ function getEnv (opts) {
const args = ['run', 'env', '--parseable']
return findNodeScript(opts.npm, {isLocal: true}).then(npmPath => {
if (npmPath) {
- args.unshift(opts.npm)
+ args.unshift(child.escapeArg(opts.npm))
return process.argv[0]
} else {
return opts.npm
@@ -155,7 +156,7 @@ function ensurePackages (specs, opts) {
// This will make temp bins _higher priority_ than even local bins.
// This is intentional, since npx assumes that if you went through
// the trouble of doing `-p`, you're rather have that one. Right? ;)
- process.env.PATH = `${bins}${PATH_SEP}${process.env.PATH}`
+ process.env.PATH = `${bins}${path.delimiter}${process.env.PATH}`
if (!info) { info = {} }
info.prefix = prefix
info.bin = bins
@@ -194,7 +195,8 @@ function getNpmCache (opts) {
}
return findNodeScript(opts.npm, {isLocal: true}).then(npmPath => {
if (npmPath) {
- args.unshift(opts.npm)
+ // This one is NOT escaped as a path because it's handed to Node.
+ args.unshift(child.escapeArg(opts.npm))
return process.argv[0]
} else {
return opts.npm
@@ -220,7 +222,11 @@ function installPackages (specs, prefix, opts) {
const args = buildArgs(specs, prefix, opts)
return findNodeScript(opts.npm, {isLocal: true}).then(npmPath => {
if (npmPath) {
- args.unshift(opts.npm)
+ args.unshift(
+ process.platform === 'win32'
+ ? child.escapeArg(opts.npm)
+ : opts.npm
+ )
return process.argv[0]
} else {
return opts.npm
@@ -319,8 +325,7 @@ function findNodeScript (existing, opts) {
throw new Error(Y()`command not found: ${existing}`)
}
} else if (process.platform !== 'win32') {
- const line = '#!/usr/bin/env node\n'
- const bytecount = line.length
+ const bytecount = 400
const buf = Buffer.alloc(bytecount)
return promisify(fs.open)(existing, 'r').then(fd => {
return promisify(fs.read)(fd, buf, 0, bytecount, 0).then(() => {
@@ -329,8 +334,26 @@ function findNodeScript (existing, opts) {
return promisify(fs.close)(fd).then(() => { throw err })
})
}).then(() => {
- return buf.toString('utf8') === line && existing
+ const re = /#!\s*(?:\/usr\/bin\/env\s*node|\/usr\/local\/bin\/node|\/usr\/bin\/node)\s*\r?\n/i
+ return buf.toString('utf8').match(re) && existing
})
+ } else if (process.platform === 'win32') {
+ const buf = Buffer.alloc(1000)
+ return promisify(fs.open)(existing, 'r').then(fd => {
+ return promisify(fs.read)(fd, buf, 0, 1000, 0).then(() => {
+ return promisify(fs.close)(fd)
+ }, err => {
+ return promisify(fs.close)(fd).then(() => { throw err })
+ })
+ }).then(() => {
+ return buf.toString('utf8').trim()
+ }).then(str => {
+ const cmd = /"%~dp0\\node\.exe"\s+"%~dp0\\(.*)"\s+%\*/
+ const mingw = /"\$basedir\/node"\s+"\$basedir\/(.*)"\s+"\$@"/i
+ return str.match(cmd) || str.match(mingw)
+ }).then(match => {
+ return match && path.join(path.dirname(existing), match[1])
+ }).then(x => console.log(x) || x)
}
})
}