-
-
Notifications
You must be signed in to change notification settings - Fork 929
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to replace
@changesets/apply-release-plan
patch (#7375)
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
1 parent
7e54fd5
commit 664683d
Showing
5 changed files
with
77 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters