Skip to content
This repository has been archived by the owner on Jul 6, 2019. It is now read-only.

Commit

Permalink
fix(windows): get magic shim detection working on Windows
Browse files Browse the repository at this point in the history
Fixes: #85
Fixes: #43
  • Loading branch information
zkat committed Jul 17, 2017
1 parent 314e5eb commit 92a73e6
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,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])
Expand Down Expand Up @@ -334,6 +337,23 @@ function findNodeScript (existing, opts) {
}).then(() => {
return buf.toString('utf8') === line && 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" "%~dp0\\(.*)" %\*/
const mingw = /"\$basedir\/node" "\$basedir\/(.*)" "\$@"/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)
}
})
}
Expand Down

0 comments on commit 92a73e6

Please sign in to comment.