diff --git a/index.js b/index.js index aa18e0a41..3a35555fc 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') } diff --git a/lib/latest-semver-tag.js b/lib/latest-semver-tag.js index 7c63ec42a..eec93af2b 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 448937286..dfa9dbb11 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({})