1const parse = require('./parse.js')
2
3const diff = (version1, version2) => {
4  const v1 = parse(version1, null, true)
5  const v2 = parse(version2, null, true)
6  const comparison = v1.compare(v2)
7
8  if (comparison === 0) {
9    return null
10  }
11
12  const v1Higher = comparison > 0
13  const highVersion = v1Higher ? v1 : v2
14  const lowVersion = v1Higher ? v2 : v1
15  const highHasPre = !!highVersion.prerelease.length
16  const lowHasPre = !!lowVersion.prerelease.length
17
18  if (lowHasPre && !highHasPre) {
19    // Going from prerelease -> no prerelease requires some special casing
20
21    // If the low version has only a major, then it will always be a major
22    // Some examples:
23    // 1.0.0-1 -> 1.0.0
24    // 1.0.0-1 -> 1.1.1
25    // 1.0.0-1 -> 2.0.0
26    if (!lowVersion.patch && !lowVersion.minor) {
27      return 'major'
28    }
29
30    // Otherwise it can be determined by checking the high version
31
32    if (highVersion.patch) {
33      // anything higher than a patch bump would result in the wrong version
34      return 'patch'
35    }
36
37    if (highVersion.minor) {
38      // anything higher than a minor bump would result in the wrong version
39      return 'minor'
40    }
41
42    // bumping major/minor/patch all have same result
43    return 'major'
44  }
45
46  // add the `pre` prefix if we are going to a prerelease version
47  const prefix = highHasPre ? 'pre' : ''
48
49  if (v1.major !== v2.major) {
50    return prefix + 'major'
51  }
52
53  if (v1.minor !== v2.minor) {
54    return prefix + 'minor'
55  }
56
57  if (v1.patch !== v2.patch) {
58    return prefix + 'patch'
59  }
60
61  // high and low are preleases
62  return 'prerelease'
63}
64
65module.exports = diff
66