From 3083a1c27b164b7ce7b7b0be110cdf4dfe025dc0 Mon Sep 17 00:00:00 2001 From: Robert Jackson Date: Fri, 24 Apr 2020 10:43:12 -0400 Subject: [PATCH 1/2] Ensure `CHANGELOG.md` has correct version when `git.tagName` is not present Previously we would call `format(null, { version })` which returns `''`. --- index.js | 10 +++------- test.js | 6 +++++- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 706ba36..a538da9 100644 --- a/index.js +++ b/index.js @@ -23,7 +23,9 @@ module.exports = class LernaChangelogGeneratorPlugin extends Plugin { get nextVersion() { let { version } = this.config.getContext(); - let nextVersion = this.getTagNameFromVersion(version); + + let tagName = this.config.getContext('git.tagName'); + let nextVersion = tagName ? format(tagName, { version }) : version; return nextVersion; } @@ -33,12 +35,6 @@ module.exports = class LernaChangelogGeneratorPlugin extends Plugin { return this.changelog; } - getTagNameFromVersion(version) { - let tagName = this.config.getContext('git.tagName'); - - return format(tagName, { version }); - } - async getTagForHEAD() { try { return await this.exec('git describe --tags --abbrev=0', { options: { write: false } }); diff --git a/test.js b/test.js index 824b369..d3849ef 100644 --- a/test.js +++ b/test.js @@ -95,7 +95,8 @@ test('it invokes lerna-changelog', async (t) => { }); test('it honors custom git.tagName formatting', async (t) => { - let plugin = buildPlugin(); + let infile = tmp.fileSync().name; + let plugin = buildPlugin({ infile }); plugin.config.setContext({ git: { tagName: 'v${version}' } }); @@ -105,6 +106,9 @@ test('it honors custom git.tagName formatting', async (t) => { ['git describe --tags --abbrev=0', { write: false }], [`${LERNA_PATH} --next-version=Unreleased --from=v1.0.0`, { write: false }], ]); + + const changelog = fs.readFileSync(infile, { encoding: 'utf8' }); + t.is(changelog, `### v1.0.1 (2020-03-18)\n\nThe changelog\n\n`); }); test('it sets the changelog without version information onto the config', async (t) => { From 1d52bd4bb48d8ebadc0d9ad99182a60271cd427b Mon Sep 17 00:00:00 2001 From: Robert Jackson Date: Fri, 24 Apr 2020 10:44:19 -0400 Subject: [PATCH 2/2] Ensure that even if there are no changes, we add _something_ to `CHANGELOG.md.` --- index.js | 17 ++++++++++++----- test.js | 12 ++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index a538da9..22f5a13 100644 --- a/index.js +++ b/index.js @@ -12,6 +12,12 @@ const LERNA_PATH = require.resolve('lerna-changelog/bin/cli'); // and this makes it much simpler const UNRELEASED = 'Unreleased'; +function getToday() { + const date = new Date().toISOString(); + + return date.slice(0, date.indexOf('T')); +} + module.exports = class LernaChangelogGeneratorPlugin extends Plugin { async init() { let from = (await this.getTagForHEAD()) || (await this.getFirstCommit()); @@ -63,8 +69,11 @@ module.exports = class LernaChangelogGeneratorPlugin extends Plugin { return changelog; } - async processChangelog(_changelog) { - let changelog = _changelog.replace(UNRELEASED, this.nextVersion); + async processChangelog() { + // this is populated in `init` + let changelog = this.changelog + ? this.changelog.replace(UNRELEASED, this.nextVersion) + : `## ${this.nextVersion} (${getToday()})`; let finalChangelog = await this.reviewChangelog(changelog); @@ -159,9 +168,7 @@ module.exports = class LernaChangelogGeneratorPlugin extends Plugin { } async beforeRelease() { - // this is populated in `init` - let changelog = this.changelog || ''; - let processedChangelog = await this.processChangelog(changelog); + let processedChangelog = await this.processChangelog(); this.debug({ changelog: processedChangelog }); diff --git a/test.js b/test.js index d3849ef..f23b055 100644 --- a/test.js +++ b/test.js @@ -121,6 +121,18 @@ test('it sets the changelog without version information onto the config', async t.is(changelog, 'The changelog'); }); +test('it prints something to CHANGELOG.md when lerna-changelog returns no content', async (t) => { + let infile = tmp.fileSync().name; + let plugin = buildPlugin({ infile }); + + plugin.responses[`${LERNA_PATH} --next-version=Unreleased --from=v1.0.0`] = ''; + + await runTasks(plugin); + + const changelog = fs.readFileSync(infile, { encoding: 'utf8' }); + t.is(changelog, `## 1.0.1 (${new Date().toISOString().slice(0, 10)})\n\n`); +}); + test('it uses the first commit when no tags exist', async (t) => { let infile = tmp.fileSync().name;