UNPKG

1.38 kBJavaScriptView Raw
1'use strict'
2
3const la = require('lazy-ass')
4const is = require('check-more-types')
5const debug = require('debug')('next-ver')
6const semver = require('semver')
7
8/*
9 given current version, like "2.3.1" and a change type
10 "break", "feat", "fix" or undefined
11 returns the next version.
12
13 2.3.1 + "break" -> 3.0.0
14 2.3.1 + "feat" -> 2.4.0
15 2.3.1 + "fix" -> 2.3.2
16
17 everything else should not change the version
18*/
19function increment (version, type) {
20 debug('incrementing %s by %s', version, type)
21 if (!type) {
22 return
23 }
24 la(isSemanticChange(type), 'invalid change', type)
25
26 if (!is.semver(version)) {
27 debug('cleaning starting version %s', version)
28 version = semver.clean(version)
29 debug('got version %s', version)
30 }
31
32 // cannot use check-more-types.semver because it is too
33 // strict and does not allow things like "0.0.0-alpha"
34 // la(is.semver(version), 'invalid starting version', version)
35 la(is.unemptyString(version), 'missing starting version', version)
36
37 const semverIncrement = semverType(type)
38 if (!semverIncrement) {
39 return
40 }
41 return semver.inc(version, semverIncrement)
42}
43
44function isSemanticChange (type) {
45 return is.oneOf(['major', 'feat', 'fix', 'chore'], type)
46}
47
48function semverType (type) {
49 const types = {
50 major: 'major',
51 feat: 'minor',
52 fix: 'patch'
53 }
54 return types[type]
55}
56
57module.exports = increment