Skip to content

Commit

Permalink
Add ability to prepend to an existing changelog
Browse files Browse the repository at this point in the history
Fixes #156
  • Loading branch information
cookpete committed Jun 14, 2020
1 parent dcc35c4 commit 9925190
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ auto-changelog --issue-url https://www.redmine.org/issues/{id}
auto-changelog --compare-url https://example.com/repo/compare/{from}...{to}
```

#### Add to an existing changelog

If you’d like to keep an existing changelog below your generated one, just add `<!-- auto-changelog-above -->` to your current changelog. The generated changelog will be added above this token, and anything below will remain.

#### Configuration

You can set any option in `package.json` under the `auto-changelog` key, using camelCase options.
Expand Down
21 changes: 18 additions & 3 deletions src/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { fetchRemote } = require('./remote')
const { fetchTags } = require('./tags')
const { parseReleases } = require('./releases')
const { compileTemplate } = require('./template')
const { parseLimit, readJson, writeFile, fileExists, updateLog, formatBytes } = require('./utils')
const { parseLimit, readFile, readJson, writeFile, fileExists, updateLog, formatBytes } = require('./utils')

const DEFAULT_OPTIONS = {
output: 'CHANGELOG.md',
Expand All @@ -21,6 +21,7 @@ const DEFAULT_OPTIONS = {

const PACKAGE_FILE = 'package.json'
const PACKAGE_OPTIONS_KEY = 'auto-changelog'
const PREPEND_TOKEN = '<!-- auto-changelog-above -->'

async function getOptions (argv) {
const options = new Command()
Expand Down Expand Up @@ -95,12 +96,26 @@ async function run (argv) {
const onParsed = ({ title }) => log(`Fetched ${title}…`)
const releases = await parseReleases(tags, remote, latestVersion, options, onParsed)
const changelog = await compileTemplate(options, { releases })
await write(changelog, options, log)
}

async function write (changelog, options, log) {
if (options.stdout) {
process.stdout.write(changelog)
process.exit(0)
return
}
await writeFile(options.output, changelog)
const bytes = Buffer.byteLength(changelog, 'utf8')
const existing = await fileExists(options.output) && await readFile(options.output, 'utf8')
if (existing) {
const index = existing.indexOf(PREPEND_TOKEN)
if (index !== -1) {
const prepended = `${changelog}\n${existing.slice(index)}`
await writeFile(options.output, prepended)
log(`${formatBytes(bytes)} prepended to ${options.output}\n`)
return
}
}
await writeFile(options.output, changelog)
log(`${formatBytes(bytes)} written to ${options.output}\n`)
}

Expand Down
4 changes: 2 additions & 2 deletions test/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ describe('run', () => {
})

it('command line options override options from package.json', async () => {
mock('fileExists', () => true)
mock('fileExists', path => path === '.auto-changelog')
mock('readJson', () => ({
'auto-changelog': {
output: 'should-not-be-this.md'
Expand All @@ -168,7 +168,7 @@ describe('run', () => {
})

it('command line options override options from .auto-changelog', async () => {
mock('fileExists', () => true)
mock('fileExists', path => path === '.auto-changelog')
mock('readJson', (path) => {
return path === '.auto-changelog' ? { output: 'should-not-be-this.md' } : null
})
Expand Down

0 comments on commit 9925190

Please sign in to comment.