diff options
author | isaacs <i@izs.me> | 2019-12-10 03:03:14 +0300 |
---|---|---|
committer | isaacs <i@izs.me> | 2019-12-10 03:30:31 +0300 |
commit | 19ce061a2ee165d8de862c8f0f733c222846b9e1 (patch) | |
tree | ad92a87f5a7d1f16da7f912a7e37e2050f2525f2 /node_modules/npm-normalize-package-bin/index.js | |
parent | 0a0fdff3edca1ea2f0a2d87a0568751f369fd0c4 (diff) |
bin-links@1.1.5
Diffstat (limited to 'node_modules/npm-normalize-package-bin/index.js')
-rw-r--r-- | node_modules/npm-normalize-package-bin/index.js | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/node_modules/npm-normalize-package-bin/index.js b/node_modules/npm-normalize-package-bin/index.js new file mode 100644 index 000000000..5a738ff82 --- /dev/null +++ b/node_modules/npm-normalize-package-bin/index.js @@ -0,0 +1,60 @@ +// pass in a manifest with a 'bin' field here, and it'll turn it +// into a properly santized bin object +const {join, basename} = require('path') + +const normalize = pkg => + !pkg.bin ? removeBin(pkg) + : typeof pkg.bin === 'string' ? normalizeString(pkg) + : Array.isArray(pkg.bin) ? normalizeArray(pkg) + : typeof pkg.bin === 'object' ? normalizeObject(pkg) + : removeBin(pkg) + +const normalizeString = pkg => { + if (!pkg.name) + return removeBin(pkg) + pkg.bin = { [pkg.name]: pkg.bin } + return normalizeObject(pkg) +} + +const normalizeArray = pkg => { + pkg.bin = pkg.bin.reduce((acc, k) => { + acc[basename(k)] = k + return acc + }, {}) + return normalizeObject(pkg) +} + +const removeBin = pkg => { + delete pkg.bin + return pkg +} + +const normalizeObject = pkg => { + const orig = pkg.bin + const clean = {} + let hasBins = false + Object.keys(orig).forEach(binKey => { + const base = join('/', basename(binKey.replace(/\\|:/g, '/'))).substr(1) + + if (typeof orig[binKey] !== 'string' || !base) + return + + const binTarget = join('/', orig[binKey]) + .replace(/\\/g, '/').substr(1) + + if (!binTarget) + return + + clean[base] = binTarget + hasBins = true + }) + + if (hasBins) + pkg.bin = clean + else + delete pkg.bin + + return pkg +} + +module.exports = normalize |