/
update-changelog.js
72 lines (65 loc) · 1.84 KB
/
update-changelog.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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,
getBlogPostInfo,
getChangelogContent,
} from "../utils.js";
const outdentString = outdent.string;
function writeChangelog(params) {
const changelog = fs.readFileSync("CHANGELOG.md", "utf-8");
const newEntry = `# ${params.version}\n\n` + getChangelogContent(params);
fs.writeFileSync("CHANGELOG.md", newEntry + "\n\n" + changelog);
}
async function getChangelogForPatch({ version, previousVersion }) {
const { stdout: changelog } = await execa("node", [
"scripts/changelog-for-patch.mjs",
"--prev-version",
previousVersion,
"--new-version",
version,
]);
return changelog;
}
export default async function updateChangelog({ version, previousVersion }) {
const semverDiff = semver.diff(version, previousVersion);
if (semverDiff !== "patch") {
const blogPost = getBlogPostInfo(version);
writeChangelog({
version,
previousVersion,
body: `🔗 [Release Notes](https://prettier.io/${blogPost.path})`,
});
if (fs.existsSync(blogPost.file)) {
// Everything is fine, this step is finished
return;
}
console.warn(
outdentString(chalk`
{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.
Press ENTER to continue.
`)
);
} else {
const body = await getChangelogForPatch({
version,
previousVersion,
});
writeChangelog({
version,
previousVersion,
body,
});
console.log("Press ENTER to continue.");
}
await waitForEnter();
await logPromise(
"Re-running Prettier on docs",
runYarn(["lint:prettier", "--write"])
);
}