Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

index.js 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // pass in a manifest with a 'bin' field here, and it'll turn it
  2. // into a properly santized bin object
  3. const {join, basename} = require('path')
  4. const normalize = pkg =>
  5. !pkg.bin ? removeBin(pkg)
  6. : typeof pkg.bin === 'string' ? normalizeString(pkg)
  7. : Array.isArray(pkg.bin) ? normalizeArray(pkg)
  8. : typeof pkg.bin === 'object' ? normalizeObject(pkg)
  9. : removeBin(pkg)
  10. const normalizeString = pkg => {
  11. if (!pkg.name)
  12. return removeBin(pkg)
  13. pkg.bin = { [pkg.name]: pkg.bin }
  14. return normalizeObject(pkg)
  15. }
  16. const normalizeArray = pkg => {
  17. pkg.bin = pkg.bin.reduce((acc, k) => {
  18. acc[basename(k)] = k
  19. return acc
  20. }, {})
  21. return normalizeObject(pkg)
  22. }
  23. const removeBin = pkg => {
  24. delete pkg.bin
  25. return pkg
  26. }
  27. const normalizeObject = pkg => {
  28. const orig = pkg.bin
  29. const clean = {}
  30. let hasBins = false
  31. Object.keys(orig).forEach(binKey => {
  32. const base = join('/', basename(binKey.replace(/\\|:/g, '/'))).substr(1)
  33. if (typeof orig[binKey] !== 'string' || !base)
  34. return
  35. const binTarget = join('/', orig[binKey])
  36. .replace(/\\/g, '/').substr(1)
  37. if (!binTarget)
  38. return
  39. clean[base] = binTarget
  40. hasBins = true
  41. })
  42. if (hasBins)
  43. pkg.bin = clean
  44. else
  45. delete pkg.bin
  46. return pkg
  47. }
  48. module.exports = normalize