codekingpro/portable-devtools
114k
1// Function to determine whether a path is in the package.bin set.
2// Used to prevent issues when people publish a package from a
3// windows machine, and then install with --no-bin-links.
4//
5// Note: this is not possible in remote or file fetchers, since
6// we don't have the manifest until AFTER we've unpacked. But the
7// main use case is registry fetching with git a distant second,
8// so that's an acceptable edge case to not handle.
9
10const binObj = (name, bin) =>
11 typeof bin === 'string' ? { [name]: bin } : bin
12
13const hasBin = (pkg, path) => {
14 const bin = binObj(pkg.name, pkg.bin)
15 const p = path.replace(/^[^\\/]*\//, '')
16 for (const kv of Object.entries(bin)) {
17 if (kv[1] === p) {
18 return true
19 }
20 }
21 return false
22}
23
24module.exports = (pkg, path) =>
25 pkg && pkg.bin ? hasBin(pkg, path) : false
26 