basant307/AI_Governance_Project
048
1// pass in a manifest with a 'bin' field here, and it'll turn it2// into a properly santized bin object3const { join, basename } = require('path')4 5const normalize = pkg =>6 !pkg.bin ? removeBin(pkg)7 : typeof pkg.bin === 'string' ? normalizeString(pkg)8 : Array.isArray(pkg.bin) ? normalizeArray(pkg)9 : typeof pkg.bin === 'object' ? normalizeObject(pkg)10 : removeBin(pkg)11 12const normalizeString = pkg => {13 if (!pkg.name) {14 return removeBin(pkg)15 }16 pkg.bin = { [pkg.name]: pkg.bin }17 return normalizeObject(pkg)18}19 20const normalizeArray = pkg => {21 pkg.bin = pkg.bin.reduce((acc, k) => {22 acc[basename(k)] = k23 return acc24 }, {})25 return normalizeObject(pkg)26}27 28const removeBin = pkg => {29 delete pkg.bin30 return pkg31}32 33const normalizeObject = pkg => {34 const orig = pkg.bin35 const clean = {}36 let hasBins = false37 Object.keys(orig).forEach(binKey => {38 const base = join('/', basename(binKey.replace(/\\|:/g, '/'))).slice(1)39 40 if (typeof orig[binKey] !== 'string' || !base) {41 return42 }43 44 const binTarget = join('/', orig[binKey].replace(/\\/g, '/'))45 .replace(/\\/g, '/').slice(1)46 47 if (!binTarget) {48 return49 }50 51 clean[base] = binTarget52 hasBins = true53 })54 55 if (hasBins) {56 pkg.bin = clean57 } else {58 delete pkg.bin59 }60 61 return pkg62}63 64module.exports = normalize65 