Skip to content

Commit

Permalink
fix: add optional prefix removal toggle for bin path resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
kchindam-infy committed Dec 9, 2024
1 parent c2483e5 commit 3cf79cc
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 35 deletions.
21 changes: 6 additions & 15 deletions lib/util/is-package-bin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const path = require('path').posix
// Function to determine whether a path is in the package.bin set.
// Used to prevent issues when people publish a package from a
// windows machine, and then install with --no-bin-links.
Expand All @@ -11,24 +10,16 @@ const path = require('path').posix
const binObj = (name, bin) =>
typeof bin === 'string' ? { [name]: bin } : bin

const hasBin = (pkg, filePath) => {
const hasBin = (pkg, path, replace = true) => {
const bin = binObj(pkg.name, pkg.bin)

// Remove 'package/' prefix if present
const relativeFilePath = path.relative('package', filePath)

const normalizedFilePath = path.normalize(relativeFilePath)

for (const binName in bin) {
const binPath = bin[binName]
const normalizedBinPath = path.normalize(binPath)

if (normalizedFilePath === normalizedBinPath) {
const p = replace ? path.replace(/^[^\\/]*\//, '') : path
for (const kv of Object.entries(bin)) {
if (kv[1] === p) {
return true
}
}
return false
}

module.exports = (pkg, filePath) =>
pkg && pkg.bin ? hasBin(pkg, filePath) : false
module.exports = (pkg, path, replace) =>
pkg && pkg.bin ? hasBin(pkg, path, replace) : false
2 changes: 1 addition & 1 deletion lib/util/tar-create-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const tarCreateOptions = manifest => ({
// anything that is not a regular file, ignored by
// .npmignore or package.json "files", etc.
filter: (path, stat) => {
if (isPackageBin(manifest, path)) {
if (isPackageBin(manifest, path, false)) {
stat.mode |= 0o111
}
return true
Expand Down
22 changes: 3 additions & 19 deletions test/util/is-package-bin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const t = require('tap')
const isPackageBin = require('../../lib/util/is-package-bin.js')
const path = require('path').posix

t.ok(isPackageBin({ bin: 'foo' }, 'package/foo'), 'finds string')
t.ok(isPackageBin({ bin: { bar: 'foo' } }, 'package/foo'), 'finds in obj')
Expand All @@ -10,23 +9,8 @@ t.notOk(isPackageBin({ bin: { bar: 'foo' } }, 'package/bar'), 'not in obj')

t.test('bin file not recognized without prefix removal', t => {
const testPkg = { name: 'my-package', bin: 'bin/index.js' }
const testFilePath = 'package/bin/index.js' // includes 'package/' prefix

// Simulate isPackageBin without prefix removal
const faultyIsPackageBin = (pkg, filePath) => {
const bin = typeof pkg.bin === 'string' ? { [pkg.name]: pkg.bin } : pkg.bin
const normalizedFilePath = path.normalize(filePath) // No prefix removal
for (const binPath of Object.values(bin)) {
const normalizedBinPath = path.normalize(binPath)
if (normalizedFilePath === normalizedBinPath) {
return true
}
}
return false
}

t.notOk(faultyIsPackageBin(testPkg, testFilePath),
'fails to recognize bin file without prefix removal')
t.ok(isPackageBin(testPkg, testFilePath), 'correctly recognizes bin file with prefix removal')
const testFilePath = 'bin/index.js' // includes 'package/' prefix
t.ok(isPackageBin(testPkg, testFilePath, false),
'correctly recognizes bin file with prefix removal')
t.end()
})

0 comments on commit 3cf79cc

Please sign in to comment.