-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter-opts.js
201 lines (164 loc) · 6.47 KB
/
writer-opts.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
'use strict'
const resolve = require(`path`).resolve
const availableTypes = require('@zazen/commit-types').types
const compareFunc = require(`compare-func`)
const capitalize = require('lodash.capitalize')
const Q = require(`q`)
const readFile = Q.denodeify(require(`fs`).readFile)
const typeGroups = [
'Removed',
'Deprecated',
'Changed',
'Added',
'Fixed',
'Security',
]
function buildCompareUrl(context) {
let { repository, repoUrl, previousTag, currentTag } = context
let host = context.host ? `${context.host}/` : ''
let owner = context.owner ? `${context.owner}/` : ''
let urlBase = repository ? `${host}${owner}${repository}` : repoUrl
let compare = `/compare/${previousTag}...${currentTag}`
return `[${context.version}](${urlBase}${compare})`
}
function buildCommitUrl(commit, context) {
let { hash } = commit
let { repository, repoUrl } = context
let host = context.host ? `${context.host}/` : ''
let owner = context.owner ? `${context.owner}/` : ''
let urlBase = repository ? `${host}${owner}${repository}` : repoUrl
let tail = `/${context.commit}/${hash}`
return `[[\`${hash}\`](${urlBase}${tail})]`
}
function buildCommitRefs(commit, context) {
let { references } = commit
let { repository, repoUrl, linkReferences } = context
let host = context.host ? `${context.host}/` : ''
let owner = context.owner ? `${context.owner}/` : ''
let commitRefs = references
.map((ref) => {
let { issue } = ref
let refRepo = ref.repository || ''
let refOwner = ref.owner ? `${ref.owner}/` : ''
let refIssue = `${refOwner}${refRepo}#${issue}`
let urlBase = repository ? `${host}` : repoUrl
let refUrl = refRepo
? `${refOwner}${refRepo}`
: `${owner}${repository}`
return linkReferences
? `[${refIssue}](${urlBase}${refUrl}/${context.issue}/${issue})`
: refIssue
})
.join(' ')
return `, closes ${commitRefs}`
}
function getWriterOpts() {
return {
/**
* Prepare template strings here so there isn't a bunch of
* conditional blocks inside the Markdown template.
*/
finalizeContext: (context) => {
let compareUrl = buildCompareUrl(context)
let headingVersion = context.linkCompare
? `${compareUrl}`
: `${context.version}`
let headingTitle = context.title ? ` “${context.title}”` : ''
let headingDate = context.date ? ` — ${context.date}` : ''
context.releaseTitle = `${
context.isPatch ? '###' : '##'
} ${headingVersion}${headingTitle}${headingDate}`
return context
},
transform: (commit, context) => {
let currentEmoji = commit.emoji
let issues = []
let emojiLength
if (!commit.emoji || typeof commit.emoji !== `string`) {
return
}
commit.notes.forEach((note) => {
note.title = `BREAKING CHANGES`
})
if (availableTypes[currentEmoji]) {
commit.type = capitalize(availableTypes[currentEmoji].group)
} else {
commit.type = 'Other updates'
}
if (commit.scope === `*`) {
commit.scope = ``
}
commit.emoji = commit.emoji.substring(0, 72)
emojiLength = commit.emoji.length
if (typeof commit.hash === `string`) {
commit.hash = commit.hash.substring(0, 7)
}
if (typeof commit.message === `string`) {
commit.message = commit.message.substring(0, 72 - emojiLength)
let url = context.repository
? `${context.host}/${context.owner}/${context.repository}`
: context.repoUrl
if (url) {
url = `${url}/issues/`
// Issue URLs.
commit.message = commit.message.replace(
/#([0-9]+)/g,
(_, issue) => {
issues.push(issue)
return `[#${issue}](${url}${issue})`
},
)
}
if (context.host) {
// User URLs.
commit.message = commit.message.replace(
/\B@([a-z0-9](?:-?[a-z0-9]){0,38})/g,
`[@$1](${context.host}/$1)`,
)
}
}
// Remove references that already appear in the message.
commit.references = commit.references.filter((reference) => {
if (issues.indexOf(reference.issue) === -1) {
return true
}
return false
})
let commitScope = commit.scope ? ` **${commit.scope}:**` : ''
let commitMsg = commit.message || commit.header
let commitHash = context.linkReferences
? buildCommitUrl(commit, context)
: commit.hash
let commitRefs = commit.references.length
? buildCommitRefs(commit, context)
: ''
/**
* Add two spaces before each line of the message body
* to maintain Markdown indentation.
*/
let commitBody = commit.body
? ` \\\n${commit.body.replace(/^.*/gm, ` $&`)}`
: ''
commit.logString = `- ${currentEmoji}${commitScope} ${commitMsg} ${commitHash}${commitRefs}${commitBody}`
return commit
},
groupBy: `type`,
commitGroupsSort: typeGroups,
commitsSort: [`scope`, `hash`, `message`],
noteGroupsSort: `emoji`,
notesSort: compareFunc,
}
}
module.exports = Q.all([
readFile(resolve(__dirname, `./templates/template.hbs`), `utf-8`),
readFile(resolve(__dirname, `./templates/header.hbs`), `utf-8`),
readFile(resolve(__dirname, `./templates/commit.hbs`), `utf-8`),
readFile(resolve(__dirname, `./templates/footer.hbs`), `utf-8`),
]).spread((template, header, commit, footer) => {
let writerOpts = getWriterOpts()
writerOpts.mainTemplate = template
writerOpts.headerPartial = header
writerOpts.commitPartial = commit
writerOpts.footerPartial = footer
return writerOpts
})