-
Notifications
You must be signed in to change notification settings - Fork 709
/
writer-opts.js
196 lines (172 loc) · 6.34 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
'use strict'
const addBangNotes = require('./add-bang-notes')
const compareFunc = require(`compare-func`)
const Q = require(`q`)
const readFile = Q.denodeify(require(`fs`).readFile)
const resolve = require(`path`).resolve
/**
* Handlebar partials for various property substitutions based on commit context.
*/
const owner = '{{#if this.owner}}{{~this.owner}}{{else}}{{~@root.owner}}{{/if}}'
const host = '{{~@root.host}}'
const repository = '{{#if this.repository}}{{~this.repository}}{{else}}{{~@root.repository}}{{/if}}'
module.exports = function (config) {
config = defaultConfig(config)
const commitUrlFormat = expandTemplate(config.commitUrlFormat, {
host,
owner,
repository
})
const compareUrlFormat = expandTemplate(config.compareUrlFormat, {
host,
owner,
repository
})
const issueUrlFormat = expandTemplate(config.issueUrlFormat, {
host,
owner,
repository,
id: '{{this.issue}}',
prefix: '{{this.prefix}}'
})
return 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) => {
const writerOpts = getWriterOpts(config)
writerOpts.mainTemplate = template
writerOpts.headerPartial = header
.replace(/{{compareUrlFormat}}/g, compareUrlFormat)
writerOpts.commitPartial = commit
.replace(/{{commitUrlFormat}}/g, commitUrlFormat)
.replace(/{{issueUrlFormat}}/g, issueUrlFormat)
writerOpts.footerPartial = footer
return writerOpts
})
}
function getWriterOpts (config) {
config = defaultConfig(config)
const typesLookup = {}
config.types.forEach(type => {
typesLookup[type.type] = type
})
return {
transform: (commit, context) => {
let discard = true
const issues = []
const typeKey = (commit.type || '').toLowerCase()
// adds additional breaking change notes
// for the special case, test(system)!: hello world, where there is
// a '!' but no 'BREAKING CHANGE' in body:
addBangNotes(commit)
commit.notes.forEach(note => {
note.title = `BREAKING CHANGES`
discard = false
})
// breaking changes attached to any type are still displayed.
if (discard && (typesLookup[typeKey] === undefined ||
typesLookup[typeKey].hidden)) return
if (typesLookup[typeKey]) commit.type = typesLookup[typeKey].section
if (commit.scope === `*`) {
commit.scope = ``
}
if (typeof commit.hash === `string`) {
commit.hash = commit.hash.substring(0, 7)
}
if (typeof commit.subject === `string`) {
// Issue URLs.
config.issuePrefixes.join('|')
let issueRegEx = '(' + config.issuePrefixes.join('|') + ')' + '([0-9]+)'
let re = new RegExp(issueRegEx, 'g')
commit.subject = commit.subject.replace(re, (_, prefix, issue) => {
issues.push(prefix + issue)
const url = expandTemplate(config.issueUrlFormat, {
host: context.host,
owner: context.owner,
repository: context.repository,
id: issue,
prefix: prefix
})
return `[${prefix}${issue}](${url})`
})
// User URLs.
commit.subject = commit.subject.replace(/\B@([a-z0-9](?:-?[a-z0-9/]){0,38})/g, (_, user) => {
// TODO: investigate why this code exists.
if (user.includes('/')) {
return `@${user}`
}
const usernameUrl = expandTemplate(config.userUrlFormat, {
host: context.host,
owner: context.owner,
repository: context.repository,
user: user
})
return `[@${user}](${usernameUrl})`
})
}
// remove references that already appear in the subject
commit.references = commit.references.filter(reference => {
if (issues.indexOf(reference.prefix + reference.issue) === -1) {
return true
}
return false
})
return commit
},
groupBy: `type`,
// the groupings of commit messages, e.g., Features vs., Bug Fixes, are
// sorted based on their probable importance:
commitGroupsSort: (a, b) => {
const commitGroupOrder = ['Reverts', 'Performance Improvements', 'Bug Fixes', 'Features']
const gRankA = commitGroupOrder.indexOf(a.title)
const gRankB = commitGroupOrder.indexOf(b.title)
if (gRankA >= gRankB) {
return -1
} else {
return 1
}
},
commitsSort: [`scope`, `subject`],
noteGroupsSort: `title`,
notesSort: compareFunc
}
}
// merge user set configuration with default configuration.
function defaultConfig (config) {
config = config || {}
config.types = config.types || [
{ type: 'feat', section: 'Features' },
{ type: 'fix', section: 'Bug Fixes' },
{ type: 'perf', section: 'Performance Improvements' },
{ type: 'revert', section: 'Reverts' },
{ type: 'docs', section: 'Documentation', hidden: true },
{ type: 'style', section: 'Styles', hidden: true },
{ type: 'chore', section: 'Miscellaneous Chores', hidden: true },
{ type: 'refactor', section: 'Code Refactoring', hidden: true },
{ type: 'test', section: 'Tests', hidden: true },
{ type: 'build', section: 'Build System', hidden: true },
{ type: 'ci', section: 'Continuous Integration', hidden: true }
]
config.issueUrlFormat = config.issueUrlFormat ||
'{{host}}/{{owner}}/{{repository}}/issues/{{id}}'
config.commitUrlFormat = config.commitUrlFormat ||
'{{host}}/{{owner}}/{{repository}}/commit/{{hash}}'
config.compareUrlFormat = config.compareUrlFormat ||
'{{host}}/{{owner}}/{{repository}}/compare/{{previousTag}}...{{currentTag}}'
config.userUrlFormat = config.userUrlFormat ||
'{{host}}/{{user}}'
config.issuePrefixes = config.issuePrefixes || ['#']
return config
}
// expand on the simple mustache-style templates supported in
// configuration (we may eventually want to use handlebars for this).
function expandTemplate (template, context) {
let expanded = template
Object.keys(context).forEach(key => {
expanded = expanded.replace(new RegExp(`{{${key}}}`, 'g'), context[key])
})
return expanded
}