11cb0ef41Sopenharmony_ciconst parse = require('./parse.js')
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst diff = (version1, version2) => {
41cb0ef41Sopenharmony_ci  const v1 = parse(version1, null, true)
51cb0ef41Sopenharmony_ci  const v2 = parse(version2, null, true)
61cb0ef41Sopenharmony_ci  const comparison = v1.compare(v2)
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci  if (comparison === 0) {
91cb0ef41Sopenharmony_ci    return null
101cb0ef41Sopenharmony_ci  }
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci  const v1Higher = comparison > 0
131cb0ef41Sopenharmony_ci  const highVersion = v1Higher ? v1 : v2
141cb0ef41Sopenharmony_ci  const lowVersion = v1Higher ? v2 : v1
151cb0ef41Sopenharmony_ci  const highHasPre = !!highVersion.prerelease.length
161cb0ef41Sopenharmony_ci  const lowHasPre = !!lowVersion.prerelease.length
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci  if (lowHasPre && !highHasPre) {
191cb0ef41Sopenharmony_ci    // Going from prerelease -> no prerelease requires some special casing
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci    // If the low version has only a major, then it will always be a major
221cb0ef41Sopenharmony_ci    // Some examples:
231cb0ef41Sopenharmony_ci    // 1.0.0-1 -> 1.0.0
241cb0ef41Sopenharmony_ci    // 1.0.0-1 -> 1.1.1
251cb0ef41Sopenharmony_ci    // 1.0.0-1 -> 2.0.0
261cb0ef41Sopenharmony_ci    if (!lowVersion.patch && !lowVersion.minor) {
271cb0ef41Sopenharmony_ci      return 'major'
281cb0ef41Sopenharmony_ci    }
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci    // Otherwise it can be determined by checking the high version
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci    if (highVersion.patch) {
331cb0ef41Sopenharmony_ci      // anything higher than a patch bump would result in the wrong version
341cb0ef41Sopenharmony_ci      return 'patch'
351cb0ef41Sopenharmony_ci    }
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci    if (highVersion.minor) {
381cb0ef41Sopenharmony_ci      // anything higher than a minor bump would result in the wrong version
391cb0ef41Sopenharmony_ci      return 'minor'
401cb0ef41Sopenharmony_ci    }
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci    // bumping major/minor/patch all have same result
431cb0ef41Sopenharmony_ci    return 'major'
441cb0ef41Sopenharmony_ci  }
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  // add the `pre` prefix if we are going to a prerelease version
471cb0ef41Sopenharmony_ci  const prefix = highHasPre ? 'pre' : ''
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci  if (v1.major !== v2.major) {
501cb0ef41Sopenharmony_ci    return prefix + 'major'
511cb0ef41Sopenharmony_ci  }
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  if (v1.minor !== v2.minor) {
541cb0ef41Sopenharmony_ci    return prefix + 'minor'
551cb0ef41Sopenharmony_ci  }
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci  if (v1.patch !== v2.patch) {
581cb0ef41Sopenharmony_ci    return prefix + 'patch'
591cb0ef41Sopenharmony_ci  }
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci  // high and low are preleases
621cb0ef41Sopenharmony_ci  return 'prerelease'
631cb0ef41Sopenharmony_ci}
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_cimodule.exports = diff
66