@release-it/keep-a-changelog 3.0.0 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.js +28 -17
  2. package/package.json +4 -4
  3. package/test.js +24 -5
package/index.js CHANGED
@@ -53,37 +53,48 @@ class KeepAChangelog extends Plugin {
53
53
  }
54
54
 
55
55
  getChangelog(latestVersion) {
56
- const { isIncrement } = this.config;
57
- // Return the unchanged changelog content when no increment is made
58
- if (!isIncrement) {
59
- return this.changelogContent;
60
- }
61
-
62
56
  const { changelog } = this.getContext();
63
57
  if (changelog) return changelog;
64
58
 
65
59
  const { filename, strictLatest } = this;
66
-
67
60
  const previousReleaseTitle = strictLatest ? `## [${latestVersion}]` : `## [`;
68
61
  const hasPreviousReleaseSection = this.changelogContent.includes(previousReleaseTitle);
69
-
70
62
  if (strictLatest && !hasPreviousReleaseSection) {
71
63
  throw Error(`Missing section for previous release ("${latestVersion}") in ${filename}.`);
72
64
  }
73
65
 
74
- const startIndex = this.changelogContent.indexOf(this.unreleasedTitle) + this.unreleasedTitle.length;
75
- let endIndex = this.changelogContent.indexOf(previousReleaseTitle, startIndex);
76
- if (!strictLatest && endIndex === -1) {
77
- endIndex = this.changelogContent.length;
66
+ const { isIncrement } = this.config;
67
+ const titleToFind = isIncrement ? this.unreleasedTitleRaw : latestVersion;
68
+ const changelogContent = this.getChangelogEntryContent(titleToFind);
69
+
70
+ this.setContext({ changelog: changelogContent });
71
+ return changelogContent;
72
+ }
73
+
74
+ getChangelogEntryContent(releaseTitleRaw) {
75
+ const { filename, changelogContent, EOL } = this;
76
+
77
+ const releaseTitleMarkdown = `## [${releaseTitleRaw}]`;
78
+ const previousReleaseTitle = `## [`;
79
+
80
+ const indexOfReleaseTitle = changelogContent.indexOf(releaseTitleMarkdown);
81
+
82
+ if (indexOfReleaseTitle === -1) {
83
+ throw Error(`Missing section for previous release ("${releaseTitleRaw}") in ${filename}.`);
84
+ }
85
+
86
+ const entryContentStartIndex = changelogContent.indexOf(EOL, indexOfReleaseTitle);
87
+ let entryContentEndIndex = changelogContent.indexOf(previousReleaseTitle, entryContentStartIndex);
88
+ if (entryContentEndIndex === -1) {
89
+ entryContentEndIndex = changelogContent.length;
78
90
  }
79
91
 
80
- const changelogContent = this.changelogContent.substring(startIndex, endIndex).trim();
81
- if (!changelogContent) {
82
- throw Error(`There are no entries under "${this.unreleasedTitleRaw}" section in ${filename}.`);
92
+ const changelogEntryContent = changelogContent.substring(entryContentStartIndex, entryContentEndIndex).trim();
93
+ if (!changelogEntryContent) {
94
+ throw Error(`There are no entries under "${releaseTitleRaw}" section in ${filename}.`);
83
95
  }
84
96
 
85
- this.setContext({ changelog: changelogContent });
86
- return changelogContent;
97
+ return changelogEntryContent;
87
98
  }
88
99
 
89
100
  bump(version) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@release-it/keep-a-changelog",
3
- "version": "3.0.0",
3
+ "version": "3.1.0",
4
4
  "description": "Keep-a-changelog plugin for release-it",
5
5
  "type": "module",
6
6
  "exports": "./index.js",
@@ -35,9 +35,9 @@
35
35
  },
36
36
  "devDependencies": {
37
37
  "bron": "^2.0.3",
38
- "mock-fs": "^5.0.0",
39
- "release-it": "^15.0.0-esm.4",
40
- "sinon": "^13.0.2"
38
+ "mock-fs": "^5.1.4",
39
+ "release-it": "^15.3.0",
40
+ "sinon": "^14.0.0"
41
41
  },
42
42
  "peerDependencies": {
43
43
  "release-it": "^15.0.0-esm.4"
package/test.js CHANGED
@@ -1,9 +1,9 @@
1
- import fs from 'fs';
2
- import test from 'bron';
3
1
  import { strict as assert } from 'assert';
2
+ import test from 'bron';
3
+ import fs from 'fs';
4
4
  import mock from 'mock-fs';
5
+ import { factory, runTasks } from 'release-it/test/util/index.js';
5
6
  import sinon from 'sinon';
6
- import { factory, runTasks } from 'release-it/test/util';
7
7
  import Plugin from './index.js';
8
8
 
9
9
  const initialDryRunFileContents =
@@ -56,11 +56,30 @@ test('should throw for empty "unreleased" section', async t => {
56
56
  await assert.rejects(runTasks(plugin), /There are no entries under "Unreleased" section in CHANGELOG-EMPTY\.md/);
57
57
  });
58
58
 
59
- test('should not throw for empty "unreleased" section when no-increment flag is set', async t => {
59
+ test('should throw for missing "unreleased" section when no-increment flag is set if changelog is misformatted', async t => {
60
+ const options = { increment: false, [namespace]: { filename: 'CHANGELOG-FOO.md' } };
61
+ const plugin = factory(Plugin, { namespace, options });
62
+ await assert.rejects(runTasks(plugin), /Missing "Unreleased" section in CHANGELOG-FOO.md/);
63
+ });
64
+
65
+ test('should throw for missing "1.0.0" section when no-increment flag is set', async t => {
66
+ const options = { increment: false, [namespace]: { filename: 'CHANGELOG-MISSING.md' } };
67
+ const plugin = factory(Plugin, { namespace, options });
68
+ await assert.rejects(runTasks(plugin), /Missing section for previous release \("1\.0\.0"\) in CHANGELOG-MISSING\.md/);
69
+ });
70
+
71
+ test('should find "1.0.0" section when no-increment flag is set when items under version', async t => {
60
72
  const options = { increment: false, [namespace]: { filename: 'CHANGELOG-EMPTY.md' } };
61
73
  const plugin = factory(Plugin, { namespace, options });
62
74
  await runTasks(plugin);
63
- assert.equal(plugin.getChangelog(), readFile('./CHANGELOG-EMPTY.md'));
75
+ assert.equal(plugin.getChangelog('1.0.0'), '* Item A\n* Item B');
76
+ });
77
+
78
+ test('should find "1.0.0" section when no-increment flag is set when items under unreleased and version', async t => {
79
+ const options = { increment: false, [namespace]: { filename: 'CHANGELOG-FULL.md' } };
80
+ const plugin = factory(Plugin, { namespace, options });
81
+ await runTasks(plugin);
82
+ assert.equal(plugin.getChangelog('1.0.0'), '* Item C\n* Item D');
64
83
  });
65
84
 
66
85
  test('should throw for missing section for previous release', async t => {