Skip to content
This repository was archived by the owner on Apr 7, 2021. It is now read-only.

Commit 255aeeb

Browse files
authored
fix(windows): get magic shim detection working on Windows (#88)
Fixes: #85 Fixes: #43
1 parent 314e5eb commit 255aeeb

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ function npx (argv) {
8383
argv.package.length === 1
8484
) {
8585
return promisify(fs.readdir)(results.bin).then(bins => {
86+
if (process.platform === 'win32') {
87+
bins = bins.filter(b => b !== 'etc' && b !== 'node_modules')
88+
}
8689
const cmd = new RegExp(`^${argv.command}(?:\\.cmd)?$`, 'i')
8790
const matching = bins.find(b => b.match(cmd))
8891
return path.resolve(results.bin, bins[matching] || bins[0])
@@ -334,6 +337,23 @@ function findNodeScript (existing, opts) {
334337
}).then(() => {
335338
return buf.toString('utf8') === line && existing
336339
})
340+
} else if (process.platform === 'win32') {
341+
const buf = Buffer.alloc(1000)
342+
return promisify(fs.open)(existing, 'r').then(fd => {
343+
return promisify(fs.read)(fd, buf, 0, 1000, 0).then(() => {
344+
return promisify(fs.close)(fd)
345+
}, err => {
346+
return promisify(fs.close)(fd).then(() => { throw err })
347+
})
348+
}).then(() => {
349+
return buf.toString('utf8').trim()
350+
}).then(str => {
351+
const cmd = /"%~dp0\\node\.exe"\s+"%~dp0\\(.*)"\s+%\*/
352+
const mingw = /"\$basedir\/node"\s+"\$basedir\/(.*)"\s+"\$@"/i
353+
return str.match(cmd) || str.match(mingw)
354+
}).then(match => {
355+
return match && path.join(path.dirname(existing), match[1])
356+
}).then(x => console.log(x) || x)
337357
}
338358
})
339359
}

0 commit comments

Comments
 (0)