Skip to content

Commit

Permalink
Merge pull request #3 from rwjblue/ensure-custom-tagname-formatting-w…
Browse files Browse the repository at this point in the history
…orks

Ensure git.tagName formatting works properly.
  • Loading branch information
rwjblue authored May 7, 2019
2 parents 4926032 + a95365d commit 9e0fcf0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
18 changes: 15 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -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;
}
}
Expand All @@ -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 },
});
}
Expand Down
15 changes: 14 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 } }],
]);
});
Expand Down

0 comments on commit 9e0fcf0

Please sign in to comment.