-
Notifications
You must be signed in to change notification settings - Fork 3
/
cli.js
executable file
·126 lines (107 loc) · 3.16 KB
/
cli.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env node
import meow from "meow";
import GitHub from "./src/github.js";
import { makeChangelog, makeGroups, addPrefixes } from "./src/parse.js";
async function run() {
const cli = meow(
`
Usage
$ changelog-generator
Options:
--milestone, -m Milestone title.
--pat, -p Personal access token.
--repo Repository address.
--style, -s Changelog style comma separated: plain (default), wordpress, grouped
--prefixes PR prefixes. Default: Added,Changed,Deprecated,Removed,Fixed,Security
--quiet Disable debug output
`,
{
importMeta: import.meta,
flags: {
milestone: {
type: "string",
alias: "m",
},
pat: {
type: "string",
alias: "p",
},
repo: {
type: "string",
},
style: {
type: "string",
alias: "s",
default: "plain",
},
prefixes: {
type: "string",
default: "Added,Changed,Deprecated,Removed,Fixed,Security",
},
quiet: {
type: "boolean",
defalut: false,
},
},
}
);
let prefixesPattern =
"^(" + cli.flags.prefixes.replaceAll(",", "|") + "|Other) - (.*?)$";
addPrefixes(prefixesPattern);
if (!cli.flags.milestone) {
cli.showHelp();
}
const styles = cli.flags.style.split(",");
styles.forEach((style) => {
if (!["plain", "grouped", "wordpress"].includes(style)) {
console.log(`\n\nWrong "style" option: ${style}`);
cli.showHelp();
}
});
!cli.flags.quiet &&
console.log(`Reading issues from Milestone "${cli.flags.milestone}"`);
const github = new GitHub({
...(cli.flags.pat ? { auth: cli.flags.pat } : {}),
});
await github.init(cli.flags.repo);
const prs = await github.getIssues(cli.flags.milestone);
!cli.flags.quiet && console.log(`Found PRs: ${prs.length}`);
let entries = [];
for (let pr of prs) {
!cli.flags.quiet && console.log(`PR "${pr.title}"`);
const participants = await github.getParticipants(pr);
!cli.flags.quiet &&
console.log(`Found ${participants.length} participants`);
const lines = makeChangelog(pr, participants);
!cli.flags.quiet &&
console.log(`Extracted ${lines.length} changelog records`);
entries = entries.concat(lines);
}
entries = entries.sort();
const groups = makeGroups(entries);
styles.forEach((style) => {
switch (style) {
case "wordpress":
const date = new Date().toISOString().substring(0, 10);
console.log(`== Changelog ==\n\n= ${cli.flags.milestone} - ${date} =`);
for (const [key, records] of Object.entries(groups)) {
records.forEach((record) => {
console.log(`* **${key}** ${record}`);
});
}
break;
case "grouped":
console.log("\n\n## Changelog:");
for (const [key, records] of Object.entries(groups)) {
console.log(`\n### ${key}\n- ` + records.join("\n- "));
}
break;
case "plain":
default:
console.log("\n\n## Changelog:");
console.log("\n- " + entries.flat().sort().join("\n- "));
break;
}
});
}
run();