Skip to content

Commit

Permalink
feat!: reformat changelog output (#27)
Browse files Browse the repository at this point in the history
Resolves #19

## Changes

- reformat changelog output
  - the new output looks like: "#PR (USER): MESSAGE"
- remove explicit return statements on every typescript functions
  • Loading branch information
mheob committed Apr 13, 2023
1 parent 00adc68 commit 5c6eb4d
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/cuddly-carrots-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@mheob/changeset-changelog': major
---

reformat the changelog output to displzy first the PR and user
1 change: 1 addition & 0 deletions .commitlintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ module.exports = {
...defaultConfig,
prompt: {
...defaultConfig.prompt,
allowEmptyScopes: true,
},
};
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# `@mheob/changeset-changelog`

[![Release](https://github.com/mheob/changeset-changelog/actions/workflows/release.yml/badge.svg)](https://github.com/mheob/changeset-changelog/actions/workflows/release.yml) [![Check](https://github.com/mheob/changeset-changelog/actions/workflows/check.yml/badge.svg)](https://github.com/mheob/changeset-changelog/actions/workflows/check.yml) [![codecov](https://codecov.io/gh/mheob/changeset-changelog/branch/main/graph/badge.svg?token=E7RZLWHMEX)](https://codecov.io/gh/mheob/changeset-changelog)
[![Release](https://github.com/mheob/changeset-changelog/actions/workflows/release.yml/badge.svg)](https://github.com/mheob/changeset-changelog/actions/workflows/release.yml)
[![Check](https://github.com/mheob/changeset-changelog/actions/workflows/check.yml/badge.svg)](https://github.com/mheob/changeset-changelog/actions/workflows/check.yml)
[![codecov](https://codecov.io/gh/mheob/changeset-changelog/branch/main/graph/badge.svg?token=E7RZLWHMEX)](https://codecov.io/gh/mheob/changeset-changelog)

Add my own style for the changelogs generated by the awesome [changesets](https://github.com/changesets/changesets) library. The style was inspired by the original [@changesets/changelog-github](https://github.com/changesets/changesets/tree/main/packages/changelog-github) and the [@svitejs/changesets-changelog-github-compact](https://github.com/svitejs/changesets-changelog-github-compact) packages.
Add my own style for the changelogs generated by the awesome [changesets](https://github.com/changesets/changesets) library. The
style was inspired by the original
[@changesets/changelog-github](https://github.com/changesets/changesets/tree/main/packages/changelog-github) and the
[@svitejs/changesets-changelog-github-compact](https://github.com/svitejs/changesets-changelog-github-compact) packages.

## Installation

Expand Down Expand Up @@ -54,8 +59,8 @@ There are differences between this changelog output and the others:

<!-- markdownlint-enable MD024 -->

- Add nice feature to the project with a PR and commit --> ([#PR](#)) by [@user](#)
- Add nice feature to the project without a PR --> ([commit](#)) by [@user](#)
- [PR](#) ([@user](#)): Add nice feature to the project with a PR and commit
- [commit](#) ([@user](#)): Add nice feature to the project without a PR

## Additional feature: linking issues

Expand Down
5 changes: 1 addition & 4 deletions src/getDependencyReleaseLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import type { GetDependencyReleaseLine, NewChangesetWithCommit } from '@changese

import { errorMessage } from './utils';

async function getDependencyReleaseLinks(
changesets: NewChangesetWithCommit[],
repository: string,
): Promise<string> {
async function getDependencyReleaseLinks(changesets: NewChangesetWithCommit[], repository: string) {
const changesetLinks = await Promise.all(
changesets.map(async (cs) => {
// istanbul ignore next: because of our mocked get-github-info -- @preserve
Expand Down
19 changes: 9 additions & 10 deletions src/getReleaseLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async function getGitHubLinks(
commit?: string,
commitFromSummary?: string,
prFromSummary?: number,
): Promise<GithubLinks> {
) {
let githubLinks: GithubLinks = { commit: undefined, pull: undefined, user: undefined };

if (prFromSummary) {
Expand Down Expand Up @@ -82,7 +82,7 @@ async function getGitHubLinks(
return githubLinks;
}

function getUserLink(usersFromSummary: string[], user?: string): string | undefined {
function getUserLink(usersFromSummary: string[], user?: string) {
const userLink =
usersFromSummary.length > 0
? usersFromSummary
Expand All @@ -91,12 +91,12 @@ function getUserLink(usersFromSummary: string[], user?: string): string | undefi
.trim()
: user;

return userLink ? `by ${userLink}` : undefined;
return userLink ? `(${userLink})` : '';
}

// add links to issue hints (fix #123) => (fix [#123](https://....))
// thanks to https://github.com/svitejs/changesets-changelog-github-compact
function linkifyIssue(line: string, repository: string): string {
function linkifyIssue(line: string, repository: string) {
return line.replace(/(?<=\( ?(?:fix|fixes|resolves|see) )(#\d+)(?= ?\))/g, (issue) => {
return `[${issue}](https://github.com/${repository}/issues/${issue.slice(1)})`;
});
Expand All @@ -118,14 +118,13 @@ export const getReleaseLine: GetReleaseLine = async (changeset, _type, options)
.split('\n')
.map((line) => linkifyIssue(line.trimEnd(), options['repo']));

const prMessage = pull ? `(${pull})` : '';
const commitMessage = commit ? `(${commit})` : '';
const userLinkMessage = userLink ?? '';
const prMessage = pull ? `${pull}` : '';
const commitMessage = commit ? `${commit}` : '';
// istanbul ignore next: because of our mocked get-github-info -- @preserve
const printPrOrCommit = pull !== '' ? prMessage : commitMessage;
const suffix = printPrOrCommit.trim() ? ` --> ${printPrOrCommit.trim()} ${userLinkMessage}` : '';
const printPrOrCommit = (pull !== '' ? prMessage : commitMessage).trim();
const prefix = printPrOrCommit ? `${printPrOrCommit} ${userLink}: ` : '';

const futureLinesMessage = futureLines.map((line) => ` ${line}`).join('\n');

return `\n\n- ${firstLine}${suffix}\n${futureLinesMessage}`;
return `\n\n- ${prefix}${firstLine}\n${futureLinesMessage}`;
};
6 changes: 3 additions & 3 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,23 +112,23 @@ describe('getReplacedChangelog', () => {
);
const result = await getReleaseLine(changeset, 'minor', { repo: data.repo });
expect(result).toBe(
'\n\n- An awesome feature. --> ([#2](https://github.com/mheob/changeset-changelog/pull/2)) by [@mheob](https://github.com/mheob)\n',
'\n\n- [#2](https://github.com/mheob/changeset-changelog/pull/2) ([@mheob](https://github.com/mheob)): An awesome feature.\n',
);
});

it('should return the changelog in a string format with data from commit', async () => {
const changeset = getChangeset('An awesome feature.', data.commit);
const result = await getReleaseLine(changeset, 'minor', { repo: data.repo });
expect(result).toBe(
'\n\n- An awesome feature. --> ([#2](https://github.com/mheob/changeset-changelog/pull/2)) by [@mheob](https://github.com/mheob)\n',
'\n\n- [#2](https://github.com/mheob/changeset-changelog/pull/2) ([@mheob](https://github.com/mheob)): An awesome feature.\n',
);
});

it('should return the changelog in a string format with data from commit', async () => {
const changeset = getChangeset('An awesome feature, (resolves #9)', data.commit);
const result = await getReleaseLine(changeset, 'minor', { repo: data.repo });
expect(result).toBe(
'\n\n- An awesome feature, (resolves [#9](https://github.com/mheob/changeset-changelog/issues/9)) --> ([#2](https://github.com/mheob/changeset-changelog/pull/2)) by [@mheob](https://github.com/mheob)\n',
'\n\n- [#2](https://github.com/mheob/changeset-changelog/pull/2) ([@mheob](https://github.com/mheob)): An awesome feature, (resolves [#9](https://github.com/mheob/changeset-changelog/issues/9))\n',
);
});

Expand Down

0 comments on commit 5c6eb4d

Please sign in to comment.