This repository has been archived by the owner on May 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
release.ts
206 lines (179 loc) Β· 4.4 KB
/
release.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import { BlockContent, Content, ListItem, PhrasingContent, Root } from 'mdast'
import parse from 'remark-parse'
import { unified } from 'unified'
enum Section {
BreakingChanges = 'breakingChanges',
Added = 'added',
Changed = 'changed',
Deprecated = 'deprecated',
Removed = 'removed',
Fixed = 'fixed',
Security = 'security',
Internal = 'internal',
}
export class Release<Scope> {
constructor(
private release: SerializedRelease<Scope>,
private repository: RepositoryConfig
) {}
public get name(): string {
return this.release.name || this.release.tagName || 'Unreleased'
}
public get tagName(): string {
return this.release.tagName || 'HEAD'
}
public get date(): string {
if (!this.release.date) {
return ''
}
return new Date(this.release.date).toLocaleDateString('en', {
year: 'numeric',
day: 'numeric',
month: 'long',
})
}
public toMarkdown(previous?: Release<Scope>): Content[] {
return [...this.header(previous), ...this.body()]
}
private header(previous?: Release<Scope>): Content[] {
return [
{
type: 'heading',
depth: 2,
children: [
{
type: 'link',
url: this.compareUrl(previous),
children: [
{
type: 'text',
value: this.name,
},
],
},
...date(this.date),
...yanked(this.release.yanked),
],
},
]
function date(date?: string): PhrasingContent[] {
if (!date) {
return []
}
return [
{
type: 'text',
value: ` - ${date}`,
},
]
}
function yanked(yanked?: boolean): PhrasingContent[] {
if (!yanked) {
return []
}
return [
{
type: 'text',
value: ' [YANKED]',
},
]
}
}
private body(): Content[] {
return [
...this.description(),
...([] as Content[]).concat(...Release.sections.map(this.section)),
]
}
private description(): Content[] {
if (!this.release.description) {
return []
}
return [
{
type: 'paragraph',
children: [{ type: 'text', value: this.release.description }],
},
]
}
private section = (section: Section): Content[] => {
const entries: Entry<Scope>[] = this.release[section] || []
if (entries.length === 0) {
return []
}
return [
{
type: 'heading',
depth: 3,
children: [
{
type: 'text',
value: Release.sectionTitle(section),
},
],
},
{
type: 'list',
children: entries.map<ListItem>((entry) => {
const isString = typeof entry === 'string'
const description = isString ? entry : `**${entry[0]}**. ${entry[1]}`
const children = (unified().use(parse).parse(description) as Root)
.children as BlockContent[]
return {
type: 'listItem',
children,
}
}),
},
]
}
private compareUrl(previous?: Release<Scope>): string {
const from = previous ? previous.tagName : this.repository.firstCommit
const to = this.tagName
return `https://github.com/${this.repository.owner}/${this.repository.repo}/compare/${from}..${to}`
}
static sections: Section[] = [
Section.BreakingChanges,
Section.Added,
Section.Changed,
Section.Deprecated,
Section.Removed,
Section.Fixed,
Section.Security,
Section.Internal,
]
static sectionTitle(section: Section): string {
switch (section) {
case Section.BreakingChanges:
return 'Breaking Changes'
case Section.Added:
return 'Added'
case Section.Changed:
return 'Changed'
case Section.Deprecated:
return 'Deprecated'
case Section.Removed:
return 'Removed'
case Section.Fixed:
return 'Fixed'
case Section.Security:
return 'Security'
case Section.Internal:
return 'Internal'
}
}
}
export type SerializedRelease<Scope> = {
name?: string
tagName?: string
date?: string
prerelease?: boolean
yanked?: boolean
description?: string
} & Partial<Record<Section, Entry<Scope>[]>>
export interface RepositoryConfig {
firstCommit: string
owner: string
repo: string
}
type Entry<Scope> = string | [Scope, string]