-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_changelog.ts
95 lines (86 loc) · 2.72 KB
/
generate_changelog.ts
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* SPDX-FileCopyrightText: Copyright 2023 Roland Csaszar
* SPDX-License-Identifier: MIT
*
* Project: vscode-scheme-repl
* File: generate_changelog.ts
* Date: 14.May.2023
*
* ==============================================================================
* Parse the latest changelog from the changelog file and save that.
* Returns an error if the version given as command line argument doesn't match
* the version of the latest changelog.
* Run with `yarn --ignore-engines ts-node generate_changelog.ts`.
*/
import { readFile, writeFile } from "fs";
/**
* The name of the file to save the latest changelog to.
*/
const outFilename = "latest_changelog.md";
/**
* The index of the version command line argument.
*/
const idxVersionArg = 2;
/**
* Regex to get a part of a changelog. The version is returned in group
* `version`.
*/
const changelogRegex =
/\n(##\s+Version\s+(?<version>[\p{N}\p{P}~]+)\s+.+?)(?:(?:\n##\s+Version)|$)/su;
/**
* Main entry point.
*/
async function main() {
const textProm = new Promise<string>((resolve, reject) => {
readFile("./CHANGELOG.md", { encoding: "utf8" }, (r, d) => {
resolve(d);
reject(r);
});
});
try {
const text = await textProm;
const match = text.match(changelogRegex);
if (match) {
const lastChangelog = processChangelog(match);
await writeChangelog(lastChangelog);
}
} catch (error) {
console.error(`Caught: ${error}`);
}
process.exit();
}
/**
* Process the changelog, write to file if version matches command line argument.
* If the version of the changelog is not the same as the command line argument,
* exit this script with an error code.
* @param match The match object to process.
* @returns The latest part of the changelog.
*/
function processChangelog(match: RegExpMatchArray) {
// eslint-disable-next-line prefer-destructuring, no-magic-numbers
const lastChangelog = match[1];
const version = match.groups ? match.groups.version : "";
if (version !== process.argv[idxVersionArg]) {
console.error(
`ERROR: version mismatch: ${version} !== ${process.argv[idxVersionArg]}`
);
// eslint-disable-next-line no-magic-numbers
process.exit(1);
}
return lastChangelog;
}
/**
* Write the changelog to the file `outFilename`.
* @param lastChangelog The changelog text to save.
*/
async function writeChangelog(lastChangelog: string) {
const writeProm = new Promise<void>((_resolve, reject) => {
writeFile(outFilename, lastChangelog, { encoding: "utf8" }, (r) => {
if (r) {
reject(r);
}
});
});
await writeProm;
}
main();