diff.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. const lowHasPre = !!lowVersion.prerelease.length
  14. if (lowHasPre && !highHasPre) {
  15. // Going from prerelease -> no prerelease requires some special casing
  16. // If the low version has only a major, then it will always be a major
  17. // Some examples:
  18. // 1.0.0-1 -> 1.0.0
  19. // 1.0.0-1 -> 1.1.1
  20. // 1.0.0-1 -> 2.0.0
  21. if (!lowVersion.patch && !lowVersion.minor) {
  22. return 'major'
  23. }
  24. // Otherwise it can be determined by checking the high version
  25. if (highVersion.patch) {
  26. // anything higher than a patch bump would result in the wrong version
  27. return 'patch'
  28. }
  29. if (highVersion.minor) {
  30. // anything higher than a minor bump would result in the wrong version
  31. return 'minor'
  32. }
  33. // bumping major/minor/patch all have same result
  34. return 'major'
  35. }
  36. // add the `pre` prefix if we are going to a prerelease version
  37. const prefix = highHasPre ? 'pre' : ''
  38. if (v1.major !== v2.major) {
  39. return prefix + 'major'
  40. }
  41. if (v1.minor !== v2.minor) {
  42. return prefix + 'minor'
  43. }
  44. if (v1.patch !== v2.patch) {
  45. return prefix + 'patch'
  46. }
  47. // high and low are preleases
  48. return 'prerelease'
  49. }
  50. module.exports = diff