Skip to content

Commit

Permalink
refactor: change original return key to current
Browse files Browse the repository at this point in the history
Signed-off-by: Tomer Figenblat <[email protected]>
  • Loading branch information
TomerFi committed Feb 6, 2024
1 parent 92ff1d7 commit 7139074
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 29 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ A Node.js executable package for determining [semantic version][semver-spec] bum
The output was changed from a space-delimited text to a <em>JSON</em> object:<br/>
<ul>
<li><strong>old</strong> <code>2.1.5 2.1.6-dev</code><br/></li>
<li><strong>New</strong><code>{"original":"2.1.4","bump":"patch","next":"2.1.5","dev":"2.1.6-dev"}</code></li>
<li><strong>New</strong><code>{"current":"2.1.4","bump":"patch","next":"2.1.5","dev":"2.1.6-dev"}</code></li>
</ul>
</li>
<br/>
Expand Down Expand Up @@ -83,19 +83,19 @@ $ podman run --privileged --rm -v $PWD:/repo:ro docker.io/tomerfi/version-bumper
For commits with a _fix_ type, the output of the above commands will be:

```json
{"original":"2.1.4","bump":"patch","next":"2.1.5","dev":"2.1.6-dev"}
{"current":"2.1.4","bump":"patch","next":"2.1.5","dev":"2.1.6-dev"}
```

For commits with a _feat_ type, the output of the above commands will be:

```json
{"original":"2.1.4","bump":"minor","next":"2.2.0","dev":"2.2.1-dev"}
{"current":"2.1.4","bump":"minor","next":"2.2.0","dev":"2.2.1-dev"}
```

For commits containing the text _BREAKING CHANGE_ in the message body, the output of the above commands will be:

```json
{"original":"2.1.4","bump":"major","next":"3.0.0","dev":"3.0.1-dev"}
{"current":"2.1.4","bump":"major","next":"3.0.0","dev":"3.0.1-dev"}
```

### Manual Bumps
Expand All @@ -107,23 +107,23 @@ $ npx version-bumper -s 2.1.4 -b patch

$ docker run --rm tomerfi/version-bumper:latest -s 2.1.4 -b patch

{"original":"2.1.4","bump":"patch","next":"2.1.5","dev":"2.1.6-dev"}
{"current":"2.1.4","bump":"patch","next":"2.1.5","dev":"2.1.6-dev"}
```

```shell
$ npx version-bumper -s 2.1.4 -b minor

$ docker run --rm tomerfi/version-bumper:latest -s 2.1.4 -b minor

{"original":"2.1.4","bump":"minor","next":"2.2.0","dev":"2.2.1-dev"}
{"current":"2.1.4","bump":"minor","next":"2.2.0","dev":"2.2.1-dev"}
```

```shell
$ npx version-bumper -s 2.1.4 -b major

$ docker run --rm tomerfi/version-bumper:latest -s 2.1.4 -b major

{"original":"2.1.4","bump":"major","next":"3.0.0","dev":"3.0.1-dev"}
{"current":"2.1.4","bump":"major","next":"3.0.0","dev":"3.0.1-dev"}
```

## Contributors [![all-contributors-badge]][all-contributors]
Expand Down
24 changes: 12 additions & 12 deletions src/bumper.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const bumpTypes = ['major', 'minor', 'patch']
* or bump a target semver based on the options passed. The option fields are documented in index.js.
*
* @param {{source: string, path: string, bump: string, label: string, preset: string}} opts options to configure bumper
* @returns {Promise<{original: string, bump: string, next: string, dev: string}>}
* @returns {Promise<{current: string, bump: string, next: string, dev: string}>}
*/
async function bumper(opts) {
// options verification
Expand All @@ -38,32 +38,32 @@ async function bumper(opts) {
throw new Error(`development iteration label ${opts.label} breaks semver`)
}

let next = '1.0.0' // default when no original
let next = '1.0.0' // default when no current
let bump = 'none' // default when no bump is performed

let original = opts.source === 'git' // if source is 'git' fetch latest semver tag from git
let current = opts.source === 'git' // if source is 'git' fetch latest semver tag from git
? (await semverTags({cwd: opts.path, skipUnstable: true}))[0] || 'none' // none is default when no tags
: opts?.source

if ('none' !== original) {
let cleanOrig = semver.clean(original) // for robustness, we work with the clean version internally
if (!semver.valid(cleanOrig)) {
throw new Error(`${original} is not a valid semver`)
if ('none' !== current) {
let cleanCurrent = semver.clean(current) // for robustness, we work with the clean version internally
if (!semver.valid(cleanCurrent)) {
throw new Error(`${current} is not a valid semver`)
}

bump = bumpTypes.includes(opts.bump) // if not known manual bump type, use auto type based on commits
? opts.bump
: (await recBump({preset: opts.preset, cwd: opts.path})).releaseType

next = original.startsWith('v') // patch for versions that starts with v
? `v${semver.inc(cleanOrig, bump)}`
: semver.inc(cleanOrig, bump)
next = current.startsWith('v') // patch for versions that starts with v
? `v${semver.inc(cleanCurrent, bump)}`
: semver.inc(cleanCurrent, bump)
}

// concatenate development iteration label
let dev = original.startsWith('v') // patch for versions that starts with v
let dev = current.startsWith('v') // patch for versions that starts with v
? `v${semver.inc(next, 'patch')}${opts.label}`
: `${semver.inc(next, 'patch')}${opts.label}`

return {original, bump, next, dev}
return {current, bump, next, dev}
}
2 changes: 1 addition & 1 deletion src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function help() {
version-bumper path/to/git/repo
Output Example
{ original: '2.1.4', bump: 'patch', next: '2.1.5', dev: '2.1.5-dev' }
{ current: '2.1.4', bump: 'patch', next: '2.1.5', dev: '2.1.5-dev' }
Options
-s, --source Source for the bump, any semver string or 'git' to fetch from tags. Defaults to 'git'.
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @param {string} bump Target bump, 'major' | 'minor' | 'patch' | 'auto'. Defaults to 'auto' which can only be used with a 'git' source.
* @param {string} label Development iteration build label. Defaults to '-dev'.
* @param {string} preset Conventional preset to use. Defaults to 'angular'.
* @returns {Promise<{original: string, bump: string, next: string, dev: string}>}
* @returns {Promise<{current: string, bump: string, next: string, dev: string}>}
*/
module.exports = async function (source = 'git', path = './', bump = 'auto', label = '-dev', preset = 'angular') {
return require('./bumper')({source, path, bump, label, preset})
Expand Down
16 changes: 8 additions & 8 deletions tests/bumper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ chai.use(require('chai-as-promised'))
suite('Test manual bumps', () => {
[{
opts: {source: '1.2.3', bump: 'major', label: '-dev'},
output: {original: '1.2.3', bump: 'major', next: '2.0.0', dev: '2.0.1-dev'}
output: {current: '1.2.3', bump: 'major', next: '2.0.0', dev: '2.0.1-dev'}
},{
opts: {source: '1.2.3', bump: 'minor', label: '-alpha1'},
output: {original: '1.2.3', bump: 'minor', next: '1.3.0', dev: '1.3.1-alpha1'}
output: {current: '1.2.3', bump: 'minor', next: '1.3.0', dev: '1.3.1-alpha1'}
},{
opts: {source: 'v1.2.3', bump: 'patch', label: '-beta1'},
output: {original: 'v1.2.3', bump: 'patch', next: 'v1.2.4', dev: 'v1.2.5-beta1'}
output: {current: 'v1.2.3', bump: 'patch', next: 'v1.2.4', dev: 'v1.2.5-beta1'}
}].forEach(t => {
test(`testing with ${JSON.stringify(t.opts)}
expecting output ${JSON.stringify(t.output)}`, async () => {
Expand Down Expand Up @@ -72,7 +72,7 @@ suite('Test automatic bumps', () => {
createRepoContent(noTags)

return expect(bumperSut({source: 'git', bump: 'auto', label: '-alpha1', preset: 'angular', path: noTags}))
.to.eventually.deep.equal({original: 'none', bump: 'none', next: '1.0.0', dev: '1.0.1-alpha1'})
.to.eventually.deep.equal({current: 'none', bump: 'none', next: '1.0.0', dev: '1.0.1-alpha1'})
})

test(`testing with a repository with no new commits
Expand All @@ -82,7 +82,7 @@ suite('Test automatic bumps', () => {
createRepoContent(noCommits, true)

return expect(bumperSut({source: 'git', bump: 'auto', label: '-dev.0', preset: 'angular', path: noCommits}))
.to.eventually.deep.equal({original: '1.2.3', bump: 'patch', next: '1.2.4', dev: '1.2.5-dev.0'})
.to.eventually.deep.equal({current: '1.2.3', bump: 'patch', next: '1.2.4', dev: '1.2.5-dev.0'})
})

test(`testing with a fix type commit
Expand All @@ -98,7 +98,7 @@ suite('Test automatic bumps', () => {
)

return expect(bumperSut({source: 'git', bump: 'auto', label: '-alpha1', preset: 'angular', path: patchBump}))
.to.eventually.deep.equal({original: '1.2.3', bump: 'patch', next: '1.2.4', dev: '1.2.5-alpha1'})
.to.eventually.deep.equal({current: '1.2.3', bump: 'patch', next: '1.2.4', dev: '1.2.5-alpha1'})
})

test(`testing with a feat type commit
Expand All @@ -114,7 +114,7 @@ suite('Test automatic bumps', () => {
)

return expect(bumperSut({source: 'git', bump: 'auto', label: '-alpha1', preset: 'angular', path: minorBump}))
.to.eventually.deep.equal({original: '1.2.3', bump: 'minor', next: '1.3.0', dev: '1.3.1-alpha1'})
.to.eventually.deep.equal({current: '1.2.3', bump: 'minor', next: '1.3.0', dev: '1.3.1-alpha1'})
})

test(`testing with a breaking change commit body
Expand All @@ -133,7 +133,7 @@ suite('Test automatic bumps', () => {
)

return expect(bumperSut({source: 'git', bump: 'auto', label: '-alpha1', preset: 'angular', path: minorBump}))
.to.eventually.deep.equal({original: '1.2.3', bump: 'major', next: '2.0.0', dev: '2.0.1-alpha1'})
.to.eventually.deep.equal({current: '1.2.3', bump: 'major', next: '2.0.0', dev: '2.0.1-alpha1'})
})
})

Expand Down

0 comments on commit 7139074

Please sign in to comment.