diff --git a/index.js b/index.js index 510b5fe..c51d3bc 100644 --- a/index.js +++ b/index.js @@ -1,18 +1,29 @@ const { EOL } = require('os'); const fs = require('fs'); const { Plugin } = require('release-it'); +const { format } = require('release-it/lib/util'); module.exports = class LernaChangelogGeneratorPlugin extends Plugin { get lernaPath() { return require.resolve('lerna-changelog/bin/cli'); } + getTagNameFromVersion(version) { + let tagName = this.config.getContext('git.tagName'); + + return format(tagName, { version }); + } + async hasTag(tag) { try { - await this.exec(`git rev-parse --verify ${tag}`, { options: { write: false } }); + await this.exec(`git show-ref --tags --quiet --verify -- "refs/tags/${tag}"`, { + options: { write: false }, + }); + return true; } catch (e) { this.debug(`hasTag(${tag}): ${e}`); + return false; } } @@ -25,13 +36,14 @@ module.exports = class LernaChangelogGeneratorPlugin extends Plugin { async getChangelog(_from) { let { version, latestVersion } = this.config.getContext(); - let from = _from || `v${latestVersion}`; + let from = _from || this.getTagNameFromVersion(latestVersion); + let nextVersion = this.getTagNameFromVersion(version); if (!(await this.hasTag(from))) { from = await this.getFirstCommit(); } - return this.exec(`${this.lernaPath} --next-version=v${version} --from=${from}`, { + return this.exec(`${this.lernaPath} --next-version=${nextVersion} --from=${from}`, { options: { write: false }, }); } diff --git a/test.js b/test.js index 2dfb2f6..f97a7e3 100644 --- a/test.js +++ b/test.js @@ -33,7 +33,20 @@ test('it invokes lerna-changelog', async t => { await runTasks(plugin); t.deepEqual(plugin.commands, [ - [`git rev-parse --verify v1.0.0`, { options: { write: false } }], + [`git show-ref --tags --quiet --verify -- "refs/tags/1.0.0"`, { options: { write: false } }], + [`${plugin.lernaPath} --next-version=1.0.1 --from=1.0.0`, { options: { write: false } }], + ]); +}); + +test('it honors custom git.tagName formatting', async t => { + let plugin = buildPlugin(); + + plugin.config.setContext({ git: { tagName: 'v${version}' } }); + + await runTasks(plugin); + + t.deepEqual(plugin.commands, [ + [`git show-ref --tags --quiet --verify -- "refs/tags/v1.0.0"`, { options: { write: false } }], [`${plugin.lernaPath} --next-version=v1.0.1 --from=v1.0.0`, { options: { write: false } }], ]); });