-
Notifications
You must be signed in to change notification settings - Fork 0
/
changelog.ts
135 lines (118 loc) · 3.19 KB
/
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
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
import type { Repo } from "./repo.ts";
import type { Commit } from "./commits.ts";
import type { Tag } from "./tags.ts";
export interface Filter {
type: string;
title: string;
}
export const filters: Filter[] = [
{
type: "breaking",
title: "Breaking",
},
{
type: "feat",
title: "Features",
},
{
type: "fix",
title: "Bug Fixes",
},
{
type: "docs",
title: "Docs",
},
{
type: "core",
title: "Core",
},
];
export interface Document {
sections: string[];
links: string[];
}
// links definition for markdown (they are not inline)
export function fmtLink(name: string, to: string): string {
return `[${name}]: ${to}`;
}
export function pushHeader(doc: Document): void {
doc.sections.push(`# Changelog
All notable changes to this project will be documented in this file.`);
}
export function pushChanges(
doc: Document,
repo: Repo,
title: string,
commits: Commit[],
style: "github" | "md",
): void {
doc.sections.push(`### ${title}`);
const list: string[] = [];
for (const commit of commits) {
const { hash } = commit;
const { header } = commit.cc;
const shortid = hash.slice(0, 7);
if (repo.remote && repo.remote.github && style === "md") {
const { user, name } = repo.remote.github;
let url = `https://github.com/${user}/${name}/`;
url = `${url}commit/${hash}`;
list.push(`- ${header} ([${shortid}])`);
doc.links.push(fmtLink(shortid, url));
} else {
// on github release we do not need to use url
list.push(`- ${header} (${shortid})`);
}
}
doc.sections.push(list.join("\n"));
}
export function pushTag(
doc: Document,
repo: Repo,
commits: Commit[],
filters: Filter[],
tag: Tag,
style: "github" | "md",
parent?: Tag,
): void {
const year = tag.date.getUTCFullYear();
const month = String(tag.date.getUTCMonth() + 1).padStart(2, "0");
const day = String(tag.date.getUTCDate()).padStart(2, "0");
if (repo.remote && repo.remote.github && style === "md") {
const { user, name } = repo.remote.github;
let url = `https://github.com/${user}/${name}/`;
url = parent
? `${url}compare/${parent.version}...${tag.version}`
: `${url}compare/${tag.version}`;
doc.links.push(fmtLink(tag.version, url));
doc.sections.push(`## [${tag.version}] - ${year}-${month}-${day}`);
} else {
doc.sections.push(`## ${tag.version} - ${year}-${month}-${day}`);
}
for (const filter of filters) {
const filtered = commits.filter((_) => _.cc.type === filter.type);
if (filtered.length > 0) {
pushChanges(doc, repo, filter.title, filtered, style);
}
}
}
export function render(doc: Document): string {
const sections = doc.sections.join("\n\n");
const links = doc.links.join("\n");
const full = [sections, links];
return `${full.join("\n\n").trim()}\n`;
}
export function polyfillVersion(repo: Repo, to: string): [Tag[], Commit[]] {
const newtag: Tag = {
tag: to,
version: to,
date: new Date(),
hash: "",
};
const tags = [newtag, ...repo.tags];
const commits = [...repo.commits];
for (const commit of commits) {
if (commit.belongs !== null) break;
commit.belongs = newtag;
}
return [tags, commits];
}