-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.ts
106 lines (99 loc) · 3.23 KB
/
utils.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
96
97
98
99
100
101
102
103
104
105
106
export const getVersion = (ver: string = '') => {
let currentVersion = ''
ver.replace(/([v|V]\d(\.\d+){0,2})/i, (str) => {
currentVersion = str
return str
})
return currentVersion
}
export const defaultTypes = {
type: '🆎',
feat: '🌟',
style: '🎨',
chore: '💄',
doc: '📖',
docs: '📖',
build: '🧯',
fix: '🐞',
test: '⛑',
refactor: '🐝',
website: '🌍',
revert: '🔙',
clean: '💊',
perf: '📈',
ci: '💢',
__unknown__: '📄'
}
export type FormatStringCommit = {
regExp?: string;
shortHash?: string;
originalMarkdown?: string;
filterAuthor?: string;
hash?: string;
login?: string;
}
export function formatStringCommit(commit = '', repoName = '', { regExp, shortHash, originalMarkdown, filterAuthor, hash, login = '' }: FormatStringCommit) {
if (filterAuthor && (new RegExp(filterAuthor)).test(login)) {
login = '';
}
if (regExp && (new RegExp(regExp).test(commit))) {
return '';
}
login = login.replace(/\[bot\]/, '-bot');
if (originalMarkdown) {
return `${commit} ${shortHash} ${login ? `@${login}`: ''}`;
}
return `${commit} [\`${shortHash}\`](https://github.com/${repoName}/commit/${hash})${login ? ` @${login}`: ''}`;
}
export function getRegExp(str = '', commit = '') {
return (new RegExp(`^(${str}\s+[\s|(|:])|(${str}[(|:])`)).test(commit.trim().toLocaleLowerCase());
}
type Options = {
types: typeof defaultTypes;
category?: Partial<Record<keyof typeof defaultTypes, string[]>>;
showEmoji: boolean;
removeType: boolean;
template: string;
}
export function getCommitLog(log: string[], options = {} as Options) {
const { types, category = {}, showEmoji, template, removeType = false } = options;
if (!Array.isArray(category['__unknown__'])) category['__unknown__'] = [];
log = log.map((commit) => {
(Object.keys(types || {}) as Array<keyof typeof defaultTypes>).forEach((name) => {
if (!category[name]) category[name] = [];
if (getRegExp(name, commit)) {
commit = showEmoji ? `- ${types[name]} ${commit}` : `- ${commit}`;
category[name]!.push(commit);
}
});
if (!/^-\s/.test(commit) && commit) {
commit = showEmoji ? `- ${types['__unknown__']} ${commit}` : `- ${commit}`;
category['__unknown__']!.push(commit);
}
if (removeType) {
commit = commit.replace(/(^-\s+?\w+(\s+)?\((.*?)\):\s+)|(^-\s+?\w+(\s+)?:\s+)/, '- ');
}
return commit
}).filter(Boolean);
let changelogContent = '';
/**
* https://github.com/jaywcjlove/changelog-generator/issues/111#issuecomment-1594085749
*/
if (template && typeof template === 'string') {
changelogContent = template.replace(/\{\{(.*?)\}\}/g, (string, replaceValue) => {
const [typeString = '', emptyString] = (replaceValue || '').split('||');
const arr = typeString.replace(/\s/g, '').split(',').map((name: keyof typeof types) => category[name] || []).flat().filter(Boolean);
if (arr.length === 0 && emptyString) return emptyString;
if (arr.length > 0) return arr.join('\n');
return string;
});
changelogContent = changelogContent.replace(/##+\s[\D]+\{\{\w+\}\}/g, '');
} else {
changelogContent = log.join('\n');
}
return {
changelog: log,
category,
changelogContent,
}
}