diff.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. const parse = require('./parse.js')
  2. const diff = (version1, version2) => {
  3. const v1 = parse(version1, null, true)
  4. const v2 = parse(version2, null, true)
  5. const comparison = v1.compare(v2)
  6. if (comparison === 0) {
  7. return null
  8. }
  9. const v1Higher = comparison > 0
  10. const highVersion = v1Higher ? v1 : v2
  11. const lowVersion = v1Higher ? v2 : v1
  12. const highHasPre = !!highVersion.prerelease.length
  13. // add the `pre` prefix if we are going to a prerelease version
  14. const prefix = highHasPre ? 'pre' : ''
  15. if (v1.major !== v2.major) {
  16. return prefix + 'major'
  17. }
  18. if (v1.minor !== v2.minor) {
  19. return prefix + 'minor'
  20. }
  21. if (v1.patch !== v2.patch) {
  22. return prefix + 'patch'
  23. }
  24. // at this point we know stable versions match but overall versions are not equal,
  25. // so either they are both prereleases, or the lower version is a prerelease
  26. if (highHasPre) {
  27. // high and low are preleases
  28. return 'prerelease'
  29. }
  30. if (lowVersion.patch) {
  31. // anything higher than a patch bump would result in the wrong version
  32. return 'patch'
  33. }
  34. if (lowVersion.minor) {
  35. // anything higher than a minor bump would result in the wrong version
  36. return 'minor'
  37. }
  38. // bumping major/minor/patch all have same result
  39. return 'major'
  40. }
  41. module.exports = diff