Skip to content

Commit

Permalink
feat(conventional-changelog-writer)!: align methods with other packag…
Browse files Browse the repository at this point in the history
…es (#1182)

BREAKING CHANGES: Now package exports `writeChangelog` method. `writeChangelogStream` and `writeChangelogString` methods are exported for backward compatibility.
  • Loading branch information
dangreen committed Dec 6, 2023
1 parent f600a6c commit 7b13ec9
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 148 deletions.
4 changes: 2 additions & 2 deletions packages/conventional-changelog-core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { execFileSync } from 'child_process'
import addStream from 'add-stream'
import gitRawCommits from 'git-raw-commits'
import { parseCommitsStream } from 'conventional-commits-parser'
import { createChangelogWriterStream } from 'conventional-changelog-writer'
import { writeChangelogStream } from 'conventional-changelog-writer'
import mergeConfig from './lib/merge-config.js'

export default function conventionalChangelog (options, context, gitRawCommitsOpts, parserOpts, writerOpts, gitRawExecOpts) {
Expand Down Expand Up @@ -117,7 +117,7 @@ export default function conventionalChangelog (options, context, gitRawCommitsOp
err.message = 'Error in options.transform: ' + err.message
setImmediate(readable.emit.bind(readable), 'error', err)
})
.pipe(createChangelogWriterStream(context, writerOpts, writerOpts.includeDetails))
.pipe(writeChangelogStream(context, writerOpts, writerOpts.includeDetails))
.on('error', function (err) {
err.message = 'Error in conventional-changelog-writer: ' + err.message
setImmediate(readable.emit.bind(readable), 'error', err)
Expand Down
43 changes: 25 additions & 18 deletions packages/conventional-changelog-writer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,30 @@ npm i -D conventional-changelog-writer

```js
import {
createChangelogAsyncGeneratorFromCommits,
createChangelogWriterStream,
createChangelogFromCommits
writeChangelogString,
writeChangelog,
writeChangelogStream
} from 'conventional-changelog-writer'

// Create logs Async Generator from commits
for await (let log of createChangelogAsyncGeneratorFromCommits(commits, context, options)) {
console.log(log)
}

// Create logs stream from commits
import { pipeline } from 'stream/promises'

// to write changelog from commits to string:
console.log(await writeChangelogString(commits, context, options))

// to write changelog from commits to async iterable:
await pipeline(
commits,
writeChangelog(context, options),
async function* (changelog) {
for await (const chunk of changelog) {
console.log(chunk)
}
}
)

// to write changelog from commits to stream:
commitsStream
.pipe(createChangelogWriterStream(context, options))
.pipe(writeChangelogStream(context, options))
.pipe(process.stdout)

// Create changelog string from commits
console.log(await createChangelogFromCommits(commits, context, options))
```

Commits it an async iterable of commit objects that looks like this:
Expand Down Expand Up @@ -122,19 +129,19 @@ The output log might look something like this:
## API
### createChangelogAsyncGeneratorFromCommits(commits: Iterable<Commit> | AsyncIterable<Commit>, context?: Context, options?: Options, includeDetails?: boolean): AsyncGenerator<string | Details, void>
### writeChangelog(context?: Context, options?: Options, includeDetails?: boolean)
Creates an async generator of changelog entries from commits.
Creates an async generator function to generate changelog entries from commits.
If `includeDetails` is `true`, instead of emitting strings of changelog, it emits objects containing the details the block.
### createChangelogWriterStream(context?: Context, options?: Options, includeDetails?: boolean): Transform
### writeChangelogStream(context?: Context, options?: Options, includeDetails?: boolean): Transform
Creates a transform stream which takes commits and outputs changelog entries.
If `includeDetails` is `true`, instead of emitting strings of changelog, it emits objects containing the details the block.
### createChangelogFromCommits(commits: Iterable<Commit> | AsyncIterable<Commit>, context?: Context, options?: Options): Promise<string>
### writeChangelogString(commits: Iterable<Commit> | AsyncIterable<Commit>, context?: Context, options?: Options): Promise<string>
Create a changelog from commits.
Expand Down
12 changes: 7 additions & 5 deletions packages/conventional-changelog-writer/src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#!/usr/bin/env node
import { pipeline } from 'stream/promises'
import meow from 'meow'
import type {
Context,
Options,
CommitKnownProps
} from '../index.js'
import { createChangelogAsyncGeneratorFromCommits } from '../index.js'
import { writeChangelog } from '../index.js'
import {
loadDataFile,
readCommitsFromFiles,
Expand Down Expand Up @@ -63,7 +64,6 @@ if (optionsPath) {
}

let inputStream: AsyncIterable<CommitKnownProps>
let chunk: string

try {
if (cli.input.length) {
Expand All @@ -76,9 +76,11 @@ try {
inputStream = readCommitsFromStdin()
}

for await (chunk of createChangelogAsyncGeneratorFromCommits(inputStream, context, options)) {
process.stdout.write(chunk)
}
await pipeline(
inputStream,
writeChangelog(context, options),
process.stdout
)
} catch (err) {
console.error(err)
process.exit(1)
Expand Down

0 comments on commit 7b13ec9

Please sign in to comment.