forked from nodejs/changelog-maker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchangelog-maker.js
More file actions
executable file
·178 lines (146 loc) · 4.59 KB
/
changelog-maker.js
File metadata and controls
executable file
·178 lines (146 loc) · 4.59 KB
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
#!/usr/bin/env node
'use strict'
const fs = require('fs')
const path = require('path')
const split2 = require('split2')
const list = require('list-stream')
const stripAnsi = require('strip-ansi')
const pkgtoId = require('pkg-to-id')
const commitStream = require('commit-stream')
const gitexec = require('gitexec')
const { commitToOutput, formatType } = require('./commit-to-output')
const groupCommits = require('./group-commits')
const collectCommitLabels = require('./collect-commit-labels')
const { isReleaseCommit, toGroups } = require('./groups')
const pkg = require('./package.json')
const debug = require('debug')(pkg.name)
const argv = require('minimist')(process.argv.slice(2))
const quiet = argv.quiet || argv.q
const help = argv.h || argv.help
const commitUrl = argv['commit-url'] || 'https://github.com/{ghUser}/{ghRepo}/commit/{ref}'
const pkgFile = path.join(process.cwd(), 'package.json')
const pkgData = fs.existsSync(pkgFile) ? require(pkgFile) : {}
const pkgId = pkgtoId(pkgData)
const getFormat = () => {
if (argv.simple || argv.s) {
return formatType.SIMPLE
} else if (argv.plaintext || argv.p) {
return formatType.PLAINTEXT
}
return formatType.MARKDOWN
}
const ghId = {
user: argv._[0] || pkgId.user || 'nodejs',
repo: argv._[1] || (pkgId.name && stripScope(pkgId.name)) || 'node'
}
const gitcmd = 'git log --pretty=full --since="{{sincecmd}}" --until="{{untilcmd}}"'
const commitdatecmd = '$(git show -s --format=%cd `{{refcmd}}`)'
const untilcmd = ''
const refcmd = argv.a || argv.all ? 'git rev-list --max-parents=0 HEAD' : 'git rev-list --max-count=1 {{ref}}'
const defaultRef = '--tags=v*.*.* 2> /dev/null ' +
'|| git rev-list --max-count=1 --tags=*.*.* 2> /dev/null ' +
'|| git rev-list --max-count=1 HEAD'
debug(ghId)
if (help) {
showUsage()
process.exit(0)
}
function showUsage () {
let usage = fs.readFileSync(path.join(__dirname, 'README.md'), 'utf8')
.replace(/[\s\S]+(## Usage\n[\s\S]*)\n## [\s\S]+/m, '$1')
if (process.stdout.isTTY) {
usage = usage
.replace(/## Usage\n[\s]*/m, '')
.replace(/\*\*/g, '')
.replace(/`/g, '')
}
process.stdout.write(usage)
}
function stripScope (name) {
return name[0] === '@' && name.indexOf('/') > 0 ? name.split('/')[1] : name
}
function replace (s, m) {
Object.keys(m).forEach((k) => {
s = s.replace(new RegExp(`\\{\\{${k}\\}\\}`, 'g'), m[k])
})
return s
}
function organiseCommits (list) {
if (argv['start-ref'] || argv.a || argv.all) {
if (argv['filter-release']) {
list = list.filter((commit) => !isReleaseCommit(commit.summary))
}
return list
}
// filter commits to those _before_ 'working on ...'
let started = false
return list.filter((commit) => {
if (started) {
return false
}
if (isReleaseCommit(commit.summary)) {
started = true
}
return !started
})
}
function printCommits (list) {
let out = `${list.join('\n')}\n`
if (!process.stdout.isTTY) {
out = stripAnsi(out)
}
process.stdout.write(out)
}
function onCommitList (err, list) {
if (err) {
return fatal(err)
}
list = organiseCommits(list)
collectCommitLabels(list, (err) => {
if (err) {
return fatal(err)
}
if (argv.group) {
list = groupCommits(list)
}
const format = getFormat()
if (format === formatType.PLAINTEXT) {
const formatted = []
let currentGroup
for (const commit of list) {
const commitGroup = toGroups(commit.summary)
if (currentGroup !== commitGroup) {
formatted.push(`${commitGroup}:`)
currentGroup = commitGroup
}
formatted.push(commitToOutput(commit, formatType.PLAINTEXT, ghId, commitUrl))
}
list = formatted
} else {
list = list.map((commit) => {
return commitToOutput(commit, format, ghId, commitUrl)
})
}
if (!quiet) {
printCommits(list)
}
})
}
function fatal (err) {
console.error(`Fatal error: ${err.message}`)
process.exit(1)
}
const _startrefcmd = replace(refcmd, { ref: argv['start-ref'] || defaultRef })
const _endrefcmd = argv['end-ref'] && replace(refcmd, { ref: argv['end-ref'] })
const _sincecmd = replace(commitdatecmd, { refcmd: _startrefcmd })
const _untilcmd = argv['end-ref'] ? replace(commitdatecmd, { refcmd: _endrefcmd }) : untilcmd
const _gitcmd = replace(gitcmd, { sincecmd: _sincecmd, untilcmd: _untilcmd })
debug('%s', _startrefcmd)
debug('%s', _endrefcmd)
debug('%s', _sincecmd)
debug('%s', _untilcmd)
debug('%s', _gitcmd)
gitexec.exec(process.cwd(), _gitcmd)
.pipe(split2())
.pipe(commitStream(ghId.user, ghId.repo))
.pipe(list.obj(onCommitList))