-
Notifications
You must be signed in to change notification settings - Fork 0
/
ht-changelog.js
executable file
·186 lines (153 loc) · 4.98 KB
/
ht-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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env node
const cwd = process.cwd();
const path = require("path");
const _ = require("lodash");
const meow = require("meow");
const projectName = require(path.resolve(cwd, "./package.json")).name;
const fs = require("fs-extra");
const types = {
fix: {
name: "fix",
regexp: /fix:|fix\([a-zA-Z0-9\u4e00-\u9fa5-_]*\):/
},
feat: {
name: "feat",
regexp: /feat:|feat\([a-zA-Z0-9\u4e00-\u9fa5-_]*\):/
},
refactor: {
name: "refactor",
regexp: /refactor:|refactor\([a-zA-Z0-9\u4e00-\u9fa5-_]*\):/
},
style: {
name: "style",
regexp: /style:|style\([a-zA-Z0-9\u4e00-\u9fa5-_]*\):/
},
chore: {
name: "chore",
regexp: /chore:|chore\([a-zA-Z0-9\u4e00-\u9fa5-_]*\):/
},
};
let commitLogPath = "commitLog"
const cli = meow(`
Usage:
ht-changelog commit_log_path
flags:
--filter -f regexp: filter commit message by a regexp
--print -p: print the change log instead of write it into changelog.md
`, {
flags: {
filter: {
type: "string",
alias: "f"
},
print: {
type: "boolean",
alias: "p"
}
}
})
if (cli.input[0]) {
commitLogPath = cli.input[0];
}
const commitLog = require(path.resolve(cwd, commitLogPath));
function filterCommits(commits) {
const regexp = new RegExp(cli.flags.filter);
return commits.filter(commit => regexp.test(commit.message))
}
function classifyCommits(commits) {
return filterCommits(commits)
.reduce((accum, cur) => {
const { message } = cur;
const categoryMatchers = _.values(types)
for (const matcher of categoryMatchers) {
const { name, regexp } = matcher;
if (regexp.test(message)) {
accum[name].push(cur)
}
}
return accum;
}, {
[types.fix.name]: [],
[types.feat.name]: [],
[types.refactor.name]: [],
[types.style.name]: [],
[types.chore.name]: [],
})
}
function groupCommitsByVersion(commits) {
return _.groupBy(commits, "version");
}
function groupCommits(commits) {
const groupedByVersion = groupCommitsByVersion(commits);
return _.mapValues(groupedByVersion, value => classifyCommits(value));
}
function generateChangeLog(commits) {
const lines = [];
function appendLine(text) {
lines.push(text);
lines.push("\n")
}
// title
const title = projectName ? `# ${projectName}` : "# Change Log"
appendLine(title);
const groupedCommits = groupCommits(commits);
const groupedByVersion = groupCommitsByVersion(commits);
const versions = _.keys(groupedCommits);
for (const version of versions) {
//version
const versionDate = new Date(groupedByVersion[version][0].date);
const dateString = `${versionDate.getFullYear()}-${versionDate.getMonth() + 1}-${versionDate.getDate()}`
const versionText = `## v-${version} (${dateString})`;
appendLine(versionText);
// type title
const commitTypes = _.keys(groupedCommits[version]);
for (const type of commitTypes) {
const typedCommits = groupedCommits[version][type];
if (!_.isEmpty(typedCommits)) {
const typeText = `### ${type}`;
appendLine(typeText)
for (const commit of typedCommits) {
const { message, auth } = commit;
const logMessage = cleanMessage(message);
const messageText = `* ${logMessage}. (auth: ${auth})`;
appendLine(messageText)
}
lines.push("\n");
}
}
appendLine("---")
}
return lines;
}
function cleanMessage(message) {
const regexp = /^.*?((fix)|(feat)|(style)|(chore)|(refactor))\:?\s/;
const scopeRegexp = /((fix)|(feat)|(style)|(chore)|(refactor))\([a-zA-Z0-9\u4e00-\u9fa5-_]*\)/;
const withoutPrefix = message.replace(regexp, "").replace(scopeRegexp, "");
const scopeMatchResult = message.match(scopeRegexp)
if (scopeMatchResult) {
const rawScope = scopeMatchResult[0];
const scope = rawScope ? rawScope.split(/[\(\)]/)[1] : "";
return _.compact([scope, withoutPrefix]).join(" ");
}
return withoutPrefix;
}
function outputChangeLog() {
const lines = generateChangeLog(commitLog);
if (cli.flags.print) {
for (const line of lines) {
console.log(line)
}
} else {
const changelogPath = path.resolve(cwd, "changelog.md");
fs.ensureFileSync(changelogPath)
let writeStream = fs.createWriteStream(changelogPath);
for (const line of lines) {
writeStream.write(line, "utf8");
}
writeStream.on('finish', () => {
console.log('wrote all data to file');
});
writeStream.end();
}
}
outputChangeLog()