From 5d09680cff161000e408a3609aaacbbe0ed44230 Mon Sep 17 00:00:00 2001 From: shipjs Date: Wed, 22 Jan 2020 13:45:42 +0100 Subject: [PATCH] fix: clean up all cases for releaseType --- .../lib/util/__tests__/getReleaseType.spec.js | 26 +++++++++++++++++-- .../shipjs-lib/src/lib/util/getReleaseType.js | 16 +++++++++++- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/packages/shipjs-lib/src/lib/util/__tests__/getReleaseType.spec.js b/packages/shipjs-lib/src/lib/util/__tests__/getReleaseType.spec.js index 0277654c..702a0ccb 100644 --- a/packages/shipjs-lib/src/lib/util/__tests__/getReleaseType.spec.js +++ b/packages/shipjs-lib/src/lib/util/__tests__/getReleaseType.spec.js @@ -1,7 +1,29 @@ import getReleaseType from '../getReleaseType'; describe('getReleaseType', () => { - it('gets release type from current version and next version', () => { - expect(getReleaseType('1.0.0', '0.10.99')).toBe('major'); + it('works', () => { + const list = [ + ['1.2.3', '2.0.0', 'major'], + ['1.2.3', '1.3.0', 'minor'], + ['1.2.3', '1.2.4', 'patch'], + ['1.2.3', '1.2.4-alpha.0', 'prerelease'], + + ['1.2.4-alpha.0', '1.2.4-alpha.1', 'prerelease'], + ['1.2.4-alpha.0', '1.2.4', 'patch'], + ['1.2.4-alpha.0', '1.2.5', 'patch'], + ['1.2.4-alpha.0', '1.3.0', 'minor'], + ['1.2.4-alpha.0', '2.0.0', 'major'], + + ['1.3.0-alpha.0', '1.3.0', 'minor'], + ]; + + list.forEach(([currentVersion, nextVersion, expected]) => { + const actual = getReleaseType(currentVersion, nextVersion); + expect({ currentVersion, nextVersion, result: actual }).toEqual({ + currentVersion, + nextVersion, + result: expected, + }); + }); }); }); diff --git a/packages/shipjs-lib/src/lib/util/getReleaseType.js b/packages/shipjs-lib/src/lib/util/getReleaseType.js index 513b4741..3ce6a56f 100644 --- a/packages/shipjs-lib/src/lib/util/getReleaseType.js +++ b/packages/shipjs-lib/src/lib/util/getReleaseType.js @@ -1,5 +1,19 @@ -import { diff } from 'semver'; +import { diff, prerelease, minor, patch } from 'semver'; export default function getReleaseType(currentVersion, nextVersion) { + if (prerelease(currentVersion) === null && prerelease(nextVersion) !== null) { + return 'prerelease'; + } + if (prerelease(currentVersion) !== null && prerelease(nextVersion) === null) { + if (patch(nextVersion) === 0) { + if (minor(nextVersion) === 0) { + return 'major'; + } else { + return 'minor'; + } + } else { + return 'patch'; + } + } return diff(currentVersion, nextVersion); }