Skip to content

Commit

Permalink
Refactor to replace @changesets/apply-release-plan patch (#7375)
Browse files Browse the repository at this point in the history
With the Changesets package updates (#7367), the patch doesn't work.
So, this change replaces the patch with a custom script processing the changelog text.
  • Loading branch information
ybiquitous committed Dec 5, 2023
1 parent 7e54fd5 commit 664683d
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 70 deletions.
40 changes: 1 addition & 39 deletions .changeset/changelog-stylelint.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,9 @@
const { getInfo, getInfoFromPullRequest } = require('@changesets/get-github-info');

/**
* @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> }}
* @type {import('@changesets/types').ChangelogFunctions}
*/
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
74 changes: 74 additions & 0 deletions .changeset/rewrite-changelog.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// --- Keep the historical changelog format. Changesets is not enough to keep it.

import { readFileSync, writeFileSync } from 'node:fs';
import process from 'node:process';

const ENTRY_PREFIXES = ['Removed', 'Changed', 'Deprecated', 'Added', 'Fixed'];
const entryPattern = new RegExp(`^- (${ENTRY_PREFIXES.join('|')}):`);

/** @type {(a: string, b: string) => number} */
const byPrefixOrder = (a, b) => {
const aPrefix = entryPattern.exec(a)?.[1];
const bPrefix = entryPattern.exec(b)?.[1];

if (aPrefix && bPrefix) return ENTRY_PREFIXES.indexOf(aPrefix) - ENTRY_PREFIXES.indexOf(bPrefix);

if (!aPrefix && bPrefix) return 1;

if (aPrefix && !bPrefix) return -1;

return 0;
};

const path = process.argv[2];

if (!path) {
throw new Error('Please provide a path to the changelog file.');
}

const content = readFileSync(path, 'utf8');
const currentLines = content.split('\n');
const newLines = [];
const entries = [];
let latestVersion = undefined;
let stoppedIndex = -1;
let subHeader = false;

for (const line of currentLines) {
stoppedIndex++;

if (line.startsWith('## ')) {
if (!latestVersion) {
latestVersion = line.replace('## ', '');
newLines.push(line);
continue;
} else {
entries.sort(byPrefixOrder);
newLines.push('', ...entries, '');
newLines.push(...currentLines.slice(stoppedIndex));
break;
}
}

if (/^### (?:Major|Minor|Patch) Changes/i.test(line)) {
subHeader = true;
continue;
} else if (subHeader) {
subHeader = false;
continue;
}

if (line.startsWith('- ')) {
entries.push(line);
continue;
}

if (line === '' && latestVersion) {
continue;
}

newLines.push(line);
}

writeFileSync(path, newLines.join('\n'), 'utf8');
console.log(`"${path}" rewritten.`); // eslint-disable-line no-console
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
"test-coverage": "npm test --ignore-scripts -- --coverage",
"test-only": "npm test --ignore-scripts",
"version": "changeset version",
"version": "changeset version && node .changeset/rewrite-changelog.mjs CHANGELOG.md",
"postversion": "git restore package.json",
"watch": "npm test --ignore-scripts -- --watch"
},
Expand Down
29 changes: 0 additions & 29 deletions patches/@changesets+apply-release-plan+6.1.4.patch

This file was deleted.

2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
"stylelint": ["./types/stylelint/index.d.ts"]
}
},
"include": ["lib/**/*.mjs", "types/**/*.ts", "rollup.config.*"],
"include": ["lib/**/*.mjs", "types/**/*.ts", "rollup.config.*", ".changeset/*.mjs"],
"exclude": ["**/__tests__/**"]
}

0 comments on commit 664683d

Please sign in to comment.