Skip to content

Commit

Permalink
Fix changelog generation (stylelint#6299)
Browse files Browse the repository at this point in the history
Co-authored-by: Masafumi Koba <473530+ybiquitous@users.noreply.github.com>
  • Loading branch information
JounQin and ybiquitous committed Aug 31, 2022
1 parent 0967b9c commit 72c63ea
Show file tree
Hide file tree
Showing 5 changed files with 483 additions and 73 deletions.
41 changes: 39 additions & 2 deletions .changeset/changelog-stylelint.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,44 @@
const { getInfo, getInfoFromPullRequest } = require('@changesets/get-github-info');

/**
* @type {import('@changesets/types').ChangelogFunctions}
* @type {Array<string | undefined>}
*/
const CHANGESET_SECTIONS = ['Removed', 'Changed', 'Deprecated', 'Added', 'Fixed'];

const changesetSectionReg = new RegExp(`^- (${CHANGESET_SECTIONS.join('|')}): `);

/**
* @typedef { 'major' | 'minor' | 'patch' } ReleaseType
* @typedef { Record<ReleaseType, Array<Promise<string>>> } ReleaseLines
* @typedef { Record<ReleaseType, string[]> | string[] } ResolvedReleaseLines
* @type {import('@changesets/types').ChangelogFunctions & { reorderReleaseLines(releaseLines: ReleaseLines): Promise<ResolvedReleaseLines> }}
*/
const changelogFunctions = {
async reorderReleaseLines(releaseLines) {
const resolved = {
major: await Promise.all(releaseLines.major),
minor: await Promise.all(releaseLines.minor),
patch: await Promise.all(releaseLines.patch),
};

return Object.entries(resolved)
.reduce((acc, [_type, lines]) => {
const type = /** @type {ReleaseType} */ (_type);
lines.forEach((line) => {
if (line) {
acc.push(`${line}${type === 'major' ? ' (BREAKING)' : ''}`);
}
});
return acc;
}, /** @type {string[]} */ ([]))
.sort((a, b) => {
const aSection = changesetSectionReg.exec(a)?.[1];
const bSection = changesetSectionReg.exec(b)?.[1];
return aSection === bSection
? a.localeCompare(b)
: CHANGESET_SECTIONS.indexOf(aSection) - CHANGESET_SECTIONS.indexOf(bSection);
});
},
async getReleaseLine(changeset, _type, options) {
if (!options || !options.repo) {
throw new Error(
Expand Down Expand Up @@ -86,7 +121,9 @@ const changelogFunctions = {
users === null ? '' : ` (${users})`,
].join('');

return `\n\n- ${firstLine}${futureLines.map((l) => ` ${l}`).join('\n')}${suffix}.\n`;
const line = `${firstLine}${futureLines.map((l) => ` ${l}`).join('\n')}${suffix}`;

return line ? `- ${line}.` : '';
},
async getDependencyReleaseLine() {
return '';
Expand Down
7 changes: 5 additions & 2 deletions .github/workflows/releasing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ jobs:
- name: Create release pull request
uses: changesets/action@v1
with:
commit: 'Prepare release'
title: 'Prepare release'
commit: Prepare release
title: Prepare release
# this expects you to have a npm script called version that runs some logic and then calls `changeset version`.
# We're also restoring `package.json` because we're using `np` for publishing
version: npm run version
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 comments on commit 72c63ea

Please sign in to comment.