Repository: conventional-changelog/standard-version
Pull request #573 (2020-04-17 11:32:01)

feat: allows seperate prefixTag version sequences
Solves #572 

coveralls: 
[![Coverage Status](https://coveralls.io/builds/30144290/badge)](https://coveralls.io/builds/30144290)

Coverage increased (+0.0005%) to 99.429% when pulling **c8016dd82408b291b7e1550059e6de4f1d1a6c07 on jahead:master** into **2f04ac8fc1c134a1981c23a093d4eece77d0bbb9 on conventional-changelog:master**.

jvitor83: @bcoe need that PR merged and a new release please.
jahead: there's conflicts I'll fix them on the weekend and update
jvitor83: @jahead don't forget that please. :)
codecov-io: # [Codecov](https://codecov.io/gh/conventional-changelog/standard-version/pull/573?src=pr&el=h1) Report
> Merging [#573](https://codecov.io/gh/conventional-changelog/standard-version/pull/573?src=pr&el=desc) (1c93d7f) into [master](https://codecov.io/gh/conventional-changelog/standard-version/commit/f5bff12515aadf5f532c2e7a992c929c5f633469?el=desc) (f5bff12) will **increase** coverage by `0.00%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/conventional-changelog/standard-version/pull/573/graphs/tree.svg?width=650&height=150&src=pr&token=J7zMN7vTTd)](https://codecov.io/gh/conventional-changelog/standard-version/pull/573?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##           master     #573   +/-   ##
=======================================
  Coverage   97.71%   97.71%           
=======================================
  Files          24       24           
  Lines        1051     1052    +1     
=======================================
+ Hits         1027     1028    +1     
  Misses         24       24           
```


| [Impacted Files](https://codecov.io/gh/conventional-changelog/standard-version/pull/573?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [index.js](https://codecov.io/gh/conventional-changelog/standard-version/pull/573/diff?src=pr&el=tree#diff-aW5kZXguanM=) | `100.00% <100.00%> (ø)` | |
| [lib/latest-semver-tag.js](https://codecov.io/gh/conventional-changelog/standard-version/pull/573/diff?src=pr&el=tree#diff-bGliL2xhdGVzdC1zZW12ZXItdGFnLmpz) | `100.00% <100.00%> (ø)` | |
| [test/git.spec.js](https://codecov.io/gh/conventional-changelog/standard-version/pull/573/diff?src=pr&el=tree#diff-dGVzdC9naXQuc3BlYy5qcw==) | `99.07% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/conventional-changelog/standard-version/pull/573?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/conventional-changelog/standard-version/pull/573?src=pr&el=footer). Last update [f5bff12...1c93d7f](https://codecov.io/gh/conventional-changelog/standard-version/pull/573?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

jahead: Done. Sorry for taking so long. Let me know if anything else is needed
jahead: Oddly, through testing this. 
I discovered/re-discovered that 
```
    if (pkg) {
      version = pkg.version
    } else if (args.gitTagFallback) {
      version = await latestSemverTag(args.tagPrefix)
    } else {
      throw new Error('no package file found')
    }
```

Which means that if there is a `packageFiles` defined we never get to use the gitSemverTags logic to resolve versions of different prefixes. And there's no option to force resolution by tag if `packageFiles` are defined. 

It will solve #702 but people should take care to configure no packageFiles. 

I'm not sure we should solve that problem in this PR, but it did seem like odd to me. 
I've outlined it in the tests. 

Patch:
diff --git a/index.js b/index.js
index aa18e0a..3a35555 100755
--- a/index.js
+++ b/index.js
@@ -56,7 +56,7 @@ module.exports = async function standardVersion (argv) {
     if (pkg) {
       version = pkg.version
     } else if (args.gitTagFallback) {
-      version = await latestSemverTag()
+      version = await latestSemverTag(args.tagPrefix)
     } else {
       throw new Error('no package file found')
     }


Test patch:
diff --git a/lib/latest-semver-tag.js b/lib/latest-semver-tag.js
index 7c63ec4..eec93af 100644
--- a/lib/latest-semver-tag.js
+++ b/lib/latest-semver-tag.js
@@ -1,11 +1,13 @@
 const gitSemverTags = require('git-semver-tags')
 const semver = require('semver')
 
-module.exports = function () {
+module.exports = function (tagPrefix = undefined) {
   return new Promise((resolve, reject) => {
-    gitSemverTags(function (err, tags) {
+    gitSemverTags({ tagPrefix }, function (err, tags) {
       if (err) return reject(err)
       else if (!tags.length) return resolve('1.0.0')
+      // Respect tagPrefix
+      tags = tags.map(tag => tag.replace(new RegExp('^' + tagPrefix), ''))
       // ensure that the largest semver tag is at the head.
       tags = tags.map(tag => { return semver.clean(tag) })
       tags.sort(semver.rcompare)
diff --git a/test/git.spec.js b/test/git.spec.js
index 4489372..dfa9dbb 100644
--- a/test/git.spec.js
+++ b/test/git.spec.js
@@ -66,7 +66,7 @@ function mock ({ bump, changelog, tags }) {
     }
   }))
 
-  mockery.registerMock('git-semver-tags', function (cb) {
+  mockery.registerMock('git-semver-tags', function (_, cb) {
     if (tags instanceof Error) cb(tags)
     else cb(null, tags || [])
   })
@@ -103,6 +103,29 @@ describe('git', function () {
     }
   })
 
+  describe('tagPrefix', () => {
+    // TODO: Use unmocked git-semver-tags and stage a git environment
+    it('will add prefix onto tag based on version from package', async function () {
+      writePackageJson('1.2.0')
+      mock({ bump: 'minor', tags: ['p-v1.2.0'] })
+      await exec('--tag-prefix p-v')
+      shell.exec('git tag').stdout.should.match(/p-v1\.3\.0/)
+    })
+
+    it('will add prefix onto tag via when gitTagFallback is true and no package [cli]', async function () {
+      shell.rm('package.json')
+      mock({ bump: 'minor', tags: ['android/production/v1.2.0', 'android/production/v1.0.0'] })
+      await exec('--tag-prefix android/production/v')
+      shell.exec('git tag').stdout.should.match(/android\/production\/v1\.3\.0/)
+    })
+
+    it('will add prefix onto tag via when gitTagFallback is true and no package [options]', async function () {
+      mock({ bump: 'minor', tags: ['android/production/v1.2.0', 'android/production/v1.0.0'] })
+      await exec({ tagPrefix: 'android/production/v', packageFiles: [] })
+      shell.exec('git tag').stdout.should.match(/android\/production\/v1\.3\.0/)
+    })
+  })
+
   it('formats the commit and tag messages appropriately', async function () {
     mock({ bump: 'minor', tags: ['v1.0.0'] })
     await exec({})