normalize.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. module.exports = normalize
  2. var fixer = require('./fixer')
  3. normalize.fixer = fixer
  4. var makeWarning = require('./make_warning')
  5. var fieldsToFix = ['name', 'version', 'description', 'repository', 'modules', 'scripts',
  6. 'files', 'bin', 'man', 'bugs', 'keywords', 'readme', 'homepage', 'license']
  7. var otherThingsToFix = ['dependencies', 'people', 'typos']
  8. var thingsToFix = fieldsToFix.map(function (fieldName) {
  9. return ucFirst(fieldName) + 'Field'
  10. })
  11. // two ways to do this in CoffeeScript on only one line, sub-70 chars:
  12. // thingsToFix = fieldsToFix.map (name) -> ucFirst(name) + "Field"
  13. // thingsToFix = (ucFirst(name) + "Field" for name in fieldsToFix)
  14. thingsToFix = thingsToFix.concat(otherThingsToFix)
  15. function normalize (data, warn, strict) {
  16. if (warn === true) {
  17. warn = null
  18. strict = true
  19. }
  20. if (!strict) {
  21. strict = false
  22. }
  23. if (!warn || data.private) {
  24. warn = function (msg) { /* noop */ }
  25. }
  26. if (data.scripts &&
  27. data.scripts.install === 'node-gyp rebuild' &&
  28. !data.scripts.preinstall) {
  29. data.gypfile = true
  30. }
  31. fixer.warn = function () {
  32. warn(makeWarning.apply(null, arguments))
  33. }
  34. thingsToFix.forEach(function (thingName) {
  35. fixer['fix' + ucFirst(thingName)](data, strict)
  36. })
  37. data._id = data.name + '@' + data.version
  38. }
  39. function ucFirst (string) {
  40. return string.charAt(0).toUpperCase() + string.slice(1)
  41. }