New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a script that generates changelog for patch release #11052
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#!/usr/bin/env node | ||
|
||
import path from "node:path"; | ||
import minimist from "minimist"; | ||
import semver from "semver"; | ||
import { | ||
changelogUnreleasedDirPath, | ||
changelogUnreleasedDirs, | ||
getEntries, | ||
printEntries, | ||
replaceVersions, | ||
} from "./utils/changelog.mjs"; | ||
|
||
const { previousVersion, newVersion } = parseArgv(); | ||
|
||
const entries = changelogUnreleasedDirs.flatMap((dir) => { | ||
const dirPath = path.join(changelogUnreleasedDirPath, dir.name); | ||
return getEntries(dirPath); | ||
}); | ||
|
||
console.log( | ||
replaceVersions( | ||
printEntries(entries).join("\n\n"), | ||
previousVersion, | ||
newVersion, | ||
/** isPatch */ true | ||
) | ||
); | ||
|
||
function parseArgv() { | ||
const argv = minimist(process.argv.slice(2)); | ||
const previousVersion = argv["prev-version"]; | ||
const newVersion = argv["new-version"]; | ||
if ( | ||
!previousVersion || | ||
!newVersion || | ||
semver.compare(previousVersion, newVersion) !== -1 | ||
) { | ||
throw new Error( | ||
`Invalid argv, prev-version: ${previousVersion}, new-version: ${newVersion}` | ||
); | ||
} | ||
return { previousVersion, newVersion }; | ||
} |
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
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,91 @@ | ||
import fs from "node:fs"; | ||
import path from "node:path"; | ||
import createEsmUtils from "esm-utils"; | ||
import semver from "semver"; | ||
|
||
const { __dirname } = createEsmUtils(import.meta); | ||
|
||
export const changelogUnreleasedDirPath = path.join( | ||
__dirname, | ||
"../../changelog_unreleased" | ||
); | ||
|
||
export const changelogUnreleasedDirs = fs | ||
.readdirSync(changelogUnreleasedDirPath, { | ||
withFileTypes: true, | ||
}) | ||
.filter((entry) => entry.isDirectory()); | ||
|
||
export function getEntries(dirPath) { | ||
const fileNames = fs | ||
.readdirSync(dirPath) | ||
.filter((fileName) => path.extname(fileName) === ".md"); | ||
const entries = fileNames.map((fileName) => { | ||
const [title, ...rest] = fs | ||
.readFileSync(path.join(dirPath, fileName), "utf8") | ||
.trim() | ||
.split("\n"); | ||
|
||
const improvement = title.match(/\[IMPROVEMENT(:(\d+))?]/); | ||
|
||
const section = title.includes("[HIGHLIGHT]") | ||
? "highlight" | ||
: title.includes("[BREAKING]") | ||
? "breaking" | ||
: improvement | ||
? "improvement" | ||
: undefined; | ||
|
||
const order = | ||
section === "improvement" && improvement[2] !== undefined | ||
? Number(improvement[2]) | ||
: undefined; | ||
|
||
const content = [processTitle(title), ...rest].join("\n"); | ||
|
||
return { fileName, section, order, content }; | ||
}); | ||
return entries; | ||
} | ||
|
||
export function printEntries(entries) { | ||
const result = []; | ||
if (entries.length > 0) { | ||
entries.sort((a, b) => { | ||
if (a.order !== undefined) { | ||
return b.order === undefined ? 1 : a.order - b.order; | ||
} | ||
return a.fileName.localeCompare(b.fileName, "en", { numeric: true }); | ||
}); | ||
result.push(...entries.map((entry) => entry.content)); | ||
} | ||
return result; | ||
} | ||
|
||
export function replaceVersions(data, prevVer, newVer, isPatch = false) { | ||
return data | ||
.replace( | ||
/prettier stable/gi, | ||
`Prettier ${isPatch ? prevVer : formatVersion(prevVer)}` | ||
) | ||
.replace( | ||
/prettier main/gi, | ||
`Prettier ${isPatch ? newVer : formatVersion(newVer)}` | ||
); | ||
} | ||
|
||
function formatVersion(version) { | ||
return `${semver.major(version)}.${semver.minor(version)}`; | ||
} | ||
|
||
function processTitle(title) { | ||
return title | ||
.replace(/\[(BREAKING|HIGHLIGHT|IMPROVEMENT(:\d+)?)]/g, "") | ||
.replace(/\s+/g, " ") | ||
.replace(/^#{4} [a-z]/, (s) => s.toUpperCase()) | ||
.replace(/(?<![[`])@([\w-]+)/g, "[@$1](https://github.com/$1)") | ||
.replace( | ||
/(?<![[`])#(\d{4,})/g, | ||
"[#$1](https://github.com/prettier/prettier/pull/$1)" | ||
); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't previous version always
require("prettier").version
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that's correct in most cases. However, the release script actually get the previous version in a different way(ref:
prettier/scripts/release/release.js
Lines 20 to 24 in acf9242
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fisker What do you think? I want to merge this for next patch version.