Files
test-repo/scripts/release/steps/update-changelog.js
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

84 lines
2.2 KiB
JavaScript
Raw Normal View History

2021-07-26 09:24:44 +08:00
import fs from "node:fs";
import execa from "execa";
import chalk from "chalk";
import outdent from "outdent";
import semver from "semver";
import { waitForEnter, runYarn, logPromise } from "../utils.js";
2018-06-05 16:37:52 -03:00
2021-07-26 09:24:44 +08:00
const outdentString = outdent.string;
2018-06-05 16:37:52 -03:00
function getBlogPostInfo(version) {
const date = new Date();
const year = date.getFullYear();
2019-11-17 18:36:21 +08:00
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
2018-06-05 16:37:52 -03:00
return {
file: `website/blog/${year}-${month}-${day}-${version}.md`,
path: `blog/${year}/${month}/${day}/${version}.html`,
};
}
2021-06-26 16:29:30 +09:00
function writeChangelog({ version, previousVersion, body }) {
2018-06-05 16:37:52 -03:00
const changelog = fs.readFileSync("CHANGELOG.md", "utf-8");
2020-05-11 09:05:06 +08:00
const newEntry = outdent`
2018-06-05 16:37:52 -03:00
# ${version}
2018-08-08 23:49:22 +08:00
[diff](https://github.com/prettier/prettier/compare/${previousVersion}...${version})
2018-06-05 16:37:52 -03:00
2021-06-26 16:29:30 +09:00
${body}
2018-06-05 16:37:52 -03:00
`;
fs.writeFileSync("CHANGELOG.md", newEntry + "\n\n" + changelog);
}
2021-06-26 16:29:30 +09:00
async function getChangelogForPatch({ version, previousVersion }) {
const { stdout: changelog } = await execa("node", [
"scripts/changelog-for-patch.mjs",
"--prev-version",
previousVersion,
"--new-version",
version,
]);
return changelog;
}
2021-07-26 09:24:44 +08:00
export default async function ({ version, previousVersion }) {
2018-06-05 16:37:52 -03:00
const semverDiff = semver.diff(version, previousVersion);
if (semverDiff !== "patch") {
const blogPost = getBlogPostInfo(version);
writeChangelog({
version,
previousVersion,
2021-06-26 16:29:30 +09:00
body: `🔗 [Release Notes](https://prettier.io/${blogPost.path})`,
2018-06-05 16:37:52 -03:00
});
if (fs.existsSync(blogPost.file)) {
// Everything is fine, this step is finished
return;
}
console.warn(
2020-05-11 09:05:06 +08:00
outdentString(chalk`
2019-06-06 18:48:11 -03:00
{yellow warning} The file {bold ${blogPost.file}} doesn't exist, but it will be referenced in {bold CHANGELOG.md}. Make sure to create it later.
2018-06-05 16:37:52 -03:00
Press ENTER to continue.
`)
);
} else {
2021-06-26 16:29:30 +09:00
const body = await getChangelogForPatch({
version,
previousVersion,
});
writeChangelog({
version,
previousVersion,
body,
});
console.log("Press ENTER to continue.");
2018-06-05 16:37:52 -03:00
}
await waitForEnter();
await logPromise(
"Re-running Prettier on docs",
runYarn(["lint:prettier", "--write"])
);
2021-07-26 09:24:44 +08:00
}