Skip to content

Commit

Permalink
Fix the trailing newline by copying the last entry in the contents vs…
Browse files Browse the repository at this point in the history
… a blank line (#202)
  • Loading branch information
dangoslen authored Nov 11, 2023
1 parent 4b4ea65 commit 6230269
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## [UNRELEASED]
### Fixed
- Fixes reverting trailing newlines at end of the file [#199](https://github.com/dangoslen/dependabot-changelog-helper/issues/199)

### Added
- Adds a new `sectionHeader` option. See the [README](./README.md#sectionheader) for more details.

Expand Down
31 changes: 31 additions & 0 deletions __tests__/changelog-updater.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,37 @@ test('updates section with an entry and accounts for multi-line entry', async ()
- Update \`package\` from v1 to v2 ([#123](https://github.com/owner/repo/pull/123))`)
})

const CHANGELOG_WITH_ENDING_NEWLINE = `# Changelog
## [v1.0.0]
### Added
- Some feature
### Dependencies
- Update \`other-package\` from beta to alpha
## [v0.9.0]
`

test('should keep the trailing newline', async () => {
mockReadStream(CHANGELOG_WITH_ENDING_NEWLINE)

await updateChangelog(PACKAGE_ENTRY, 'v1.0.0', './CHANGELOG.md', 'Update', 'Dependencies')

expectWrittenChangelogToBe(`# Changelog
## [v1.0.0]
### Added
- Some feature
### Dependencies
- Update \`other-package\` from beta to alpha
- Update \`package\` from v1 to v2 ([#123](https://github.com/owner/repo/pull/123))
## [v0.9.0]
`)
})

function mockReadStream(changelog: string) {
fs.createReadStream.mockImplementation((_: PathLike) => {
Expand Down
7 changes: 6 additions & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion src/changelog-updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,19 @@ function writeEntry(
changelogEntry: string,
contents: string[]
): void {
const length = contents.push('')
// Push a copy of the last line to the end of the contents
// It will be overwritten when we re-write all the contents
const length = contents.push(contents[-1])

// Copy the contents from the last line up until the line of the entry we want to write
for (let i = length - 1; i > lineNumber; i--) {
contents[i] = contents[i - 1]
}

// Write the entry
contents[lineNumber] = changelogEntry

// Write the contents out, joining with EOL
fs.writeFileSync(changelogPath, contents.join(EOL))
}

Expand Down

0 comments on commit 6230269

Please sign in to comment.