-
Notifications
You must be signed in to change notification settings - Fork 8
/
bump-changelog.js
49 lines (38 loc) · 1.47 KB
/
bump-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
/**
* @overview Sets the current version in the manifest as the latest release in
* the CHANGELOG.
* @license MIT
*/
import fs from "node:fs";
import path from "node:path";
import { common } from "../_.js";
const STR_UNRELEASED = "## [Unreleased]";
const STR_NO_CHANGES = "- _No changes yet_";
const manifestFile = path.resolve(common.projectRoot, "package.json");
const changelogFile = path.resolve(common.projectRoot, "CHANGELOG.md");
const manifestRaw = fs.readFileSync(manifestFile).toString();
const manifest = JSON.parse(manifestRaw);
const version = manifest.version;
const changelog = fs.readFileSync(changelogFile).toString();
if (changelog.includes(`## [${version}]`)) {
throw new Error(`${version} already in CHANGELOG`);
}
const unreleasedTitleIndex = changelog.indexOf(STR_UNRELEASED);
if (unreleasedTitleIndex === -1) {
throw new Error("The CHANGELOG is invalid");
}
if (changelog.includes(STR_NO_CHANGES)) {
throw new Error("No changes to release in the CHANGELOG");
}
const date = new Date();
const year = date.getFullYear();
const _month = date.getMonth() + 1;
const month = _month < 10 ? `0${_month}` : _month;
const _day = date.getDate();
const day = _day < 10 ? `0${_day}` : _day;
const newChangelog =
changelog.slice(0, unreleasedTitleIndex + STR_UNRELEASED.length) +
`\n\n${STR_NO_CHANGES}` +
`\n\n## [${version}] - ${year}-${month}-${day}` +
changelog.slice(unreleasedTitleIndex + STR_UNRELEASED.length);
fs.writeFileSync(changelogFile, newChangelog);