Skip to content

Commit 603d3d0

Browse files
committed
chore: lint with standard (#23)
1 parent 17d363e commit 603d3d0

File tree

3 files changed

+117
-78
lines changed

3 files changed

+117
-78
lines changed

.github/workflows/lint.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Lint
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
11+
jobs:
12+
build:
13+
runs-on: macOS-latest
14+
steps:
15+
- uses: actions/checkout@master
16+
- name: Use Node.js 12.x
17+
uses: actions/setup-node@v1
18+
with:
19+
version: 12.x
20+
- name: Lint
21+
run: |
22+
npm i
23+
npm run lint

branch-diff.js

Lines changed: 86 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,31 @@
22

33
'use strict'
44

5-
const fs = require('fs')
6-
, path = require('path')
7-
, commitStream = require('commit-stream')
8-
, split2 = require('split2')
9-
, listStream = require('list-stream')
10-
, pkgtoId = require('pkg-to-id')
11-
, stripAnsi = require('strip-ansi')
12-
, map = require('map-async')
13-
, { commitToOutput } = require('changelog-maker/commit-to-output')
14-
, collectCommitLabels = require('changelog-maker/collect-commit-labels')
15-
, groupCommits = require('changelog-maker/group-commits')
16-
, { isReleaseCommit, toGroups } = require('changelog-maker/groups')
17-
, gitexec = require('gitexec')
18-
19-
, pkgFile = path.join(process.cwd(), 'package.json')
20-
, pkgData = fs.existsSync(pkgFile) ? require(pkgFile) : {}
21-
, pkgId = pkgtoId(pkgData)
22-
, refcmd = 'git rev-list --max-count=1 {{ref}}'
23-
, commitdatecmd = '$(git show -s --format=%cd `{{refcmd}}`)'
24-
, gitcmd = 'git log {{startCommit}}..{{branch}} --until="{{untilcmd}}"'
25-
, ghId = {
26-
user: pkgId.user || 'nodejs'
27-
, repo: pkgId.name || 'node'
28-
}
29-
, defaultCommitUrl = 'https://github.com/{ghUser}/{ghRepo}/commit/{ref}'
5+
const fs = require('fs')
6+
const path = require('path')
7+
const commitStream = require('commit-stream')
8+
const split2 = require('split2')
9+
const listStream = require('list-stream')
10+
const pkgtoId = require('pkg-to-id')
11+
const stripAnsi = require('strip-ansi')
12+
const map = require('map-async')
13+
const { commitToOutput } = require('changelog-maker/commit-to-output')
14+
const collectCommitLabels = require('changelog-maker/collect-commit-labels')
15+
const groupCommits = require('changelog-maker/group-commits')
16+
const { isReleaseCommit, toGroups } = require('changelog-maker/groups')
17+
const gitexec = require('gitexec')
18+
19+
const pkgFile = path.join(process.cwd(), 'package.json')
20+
const pkgData = fs.existsSync(pkgFile) ? require(pkgFile) : {}
21+
const pkgId = pkgtoId(pkgData)
22+
const refcmd = 'git rev-list --max-count=1 {{ref}}'
23+
const commitdatecmd = '$(git show -s --format=%cd `{{refcmd}}`)'
24+
const gitcmd = 'git log {{startCommit}}..{{branch}} --until="{{untilcmd}}"'
25+
const ghId = {
26+
user: pkgId.user || 'nodejs',
27+
repo: pkgId.name || 'node'
28+
}
29+
const defaultCommitUrl = 'https://github.com/{ghUser}/{ghRepo}/commit/{ref}'
3030

3131
const formatType = {
3232
PLAINTEXT: 'plaintext',
@@ -43,58 +43,56 @@ const getFormat = (argv) => {
4343
}
4444
return formatType.MARKDOWN
4545
}
46-
46+
4747
function replace (s, m) {
4848
Object.keys(m).forEach(function (k) {
4949
s = s.replace(new RegExp('\\{\\{' + k + '\\}\\}', 'g'), m[k])
5050
})
5151
return s
5252
}
5353

54-
5554
function branchDiff (branch1, branch2, options, callback) {
56-
if (!branch1 || !branch2)
55+
if (!branch1 || !branch2) {
5756
return callback(new Error('Must supply two branch names to compare'))
57+
}
5858

59-
let repoPath = options.repoPath || process.cwd()
59+
const repoPath = options.repoPath || process.cwd()
6060

6161
findMergeBase(repoPath, branch1, branch2, (err, commit) => {
62-
if (err)
63-
return callback(err)
62+
if (err) { return callback(err) }
6463
map(
65-
[ branch1, branch2 ], (branch, callback) => {
66-
collect(repoPath, branch, commit, branch == branch2 && options.endRef).pipe(listStream.obj(callback))
67-
}
64+
[branch1, branch2], (branch, callback) => {
65+
collect(repoPath, branch, commit, branch === branch2 && options.endRef).pipe(listStream.obj(callback))
66+
}
6867
, (err, branchCommits) => err ? callback(err) : diffCollected(options, branchCommits, callback)
6968
)
7069
})
7170
}
7271

73-
7472
function findMergeBase (repoPath, branch1, branch2, callback) {
75-
let gitcmd = `git merge-base ${branch1} ${branch2}`
73+
const gitcmd = `git merge-base ${branch1} ${branch2}`
7674

7775
gitexec.execCollect(repoPath, gitcmd, (err, data) => {
78-
if (err)
76+
if (err) {
7977
return callback(err)
78+
}
8079

8180
callback(null, data.substr(0, 10))
8281
})
8382
}
8483

85-
8684
function diffCollected (options, branchCommits, callback) {
8785
function isInList (commit) {
8886
return branchCommits[0].some((c) => {
89-
if (commit.sha === c.sha)
90-
return true
87+
if (commit.sha === c.sha) { return true }
9188
if (commit.summary === c.summary) {
9289
if (commit.prUrl && c.prUrl) {
93-
return commit.prUrl === c.prUrl
94-
} else if (commit.author.name === c.author.name
95-
&& commit.author.email === c.author.email) {
96-
if (process.stderr.isTTY)
90+
return commit.prUrl === c.prUrl
91+
} else if (commit.author.name === c.author.name &&
92+
commit.author.email === c.author.email) {
93+
if (process.stderr.isTTY) {
9794
console.error(`Note: Commit fell back to author checking: "${commit.summary}" -`, commit.author)
95+
}
9896
return true
9997
}
10098
}
@@ -105,8 +103,9 @@ function diffCollected (options, branchCommits, callback) {
105103
let list = branchCommits[1].filter((commit) => !isInList(commit))
106104

107105
collectCommitLabels(list, (err) => {
108-
if (err)
106+
if (err) {
109107
return callback(err)
108+
}
110109

111110
if (options.excludeLabels.length > 0) {
112111
list = list.filter((commit) => {
@@ -124,8 +123,9 @@ function diffCollected (options, branchCommits, callback) {
124123
})
125124
}
126125

127-
if (options.group)
126+
if (options.group) {
128127
list = groupCommits(list)
128+
}
129129

130130
callback(null, list)
131131
})
@@ -152,83 +152,92 @@ function printCommits (list, format, reverse, commitUrl) {
152152
}
153153
list = formatted
154154
} else {
155-
list = list.map((commit) => commitToOutput(commit, formatType.MARKDOWN, ghId, commitUrl))
155+
list = list.map((commit) => {
156+
return commitToOutput(commit, formatType.MARKDOWN, ghId, commitUrl)
157+
})
156158
}
157159

158-
if (reverse)
160+
if (reverse) {
159161
list = list.reverse()
162+
}
160163

161164
let out = list.join('\n') + '\n'
162165

163-
if (!process.stdout.isTTY)
166+
if (!process.stdout.isTTY) {
164167
out = stripAnsi(out)
168+
}
165169

166170
process.stdout.write(out)
167171
}
168172

169-
170173
function collect (repoPath, branch, startCommit, endRef) {
171-
let endrefcmd = endRef && replace(refcmd, { ref: endRef })
172-
, untilcmd = endRef ? replace(commitdatecmd, { refcmd: endrefcmd }) : ''
173-
, _gitcmd = replace(gitcmd, { branch, startCommit, untilcmd })
174+
const endrefcmd = endRef && replace(refcmd, { ref: endRef })
175+
const untilcmd = endRef ? replace(commitdatecmd, { refcmd: endrefcmd }) : ''
176+
const _gitcmd = replace(gitcmd, { branch, startCommit, untilcmd })
174177

175178
return gitexec.exec(repoPath, _gitcmd)
176179
.pipe(split2())
177180
.pipe(commitStream(ghId.user, ghId.repo))
178181
}
179182

180-
181183
module.exports = branchDiff
182184

183-
if (require.main === module) {
184-
let minimistConfig = {
185-
boolean: [ 'version', 'group', 'patch-only', 'simple', 'filter-release', 'reverse' ]
186-
}
187-
, argv = require('minimist')(process.argv.slice(2), minimistConfig)
188-
, branch1 = argv._[0]
189-
, branch2 = argv._[1]
190-
, reverse = argv.reverse
191-
, group = argv.group || argv.g
192-
, endRef = argv['end-ref']
193-
, commitUrl = argv['commit-url'] || defaultCommitUrl
194-
, excludeLabels = []
195-
, requireLabels = []
196-
, options
185+
function main () {
186+
const minimistConfig = {
187+
boolean: ['version', 'group', 'patch-only', 'simple', 'filter-release', 'reverse']
188+
}
189+
const argv = require('minimist')(process.argv.slice(2), minimistConfig)
190+
const branch1 = argv._[0]
191+
const branch2 = argv._[1]
192+
const reverse = argv.reverse
193+
const group = argv.group || argv.g
194+
const endRef = argv['end-ref']
195+
const commitUrl = argv['commit-url'] || defaultCommitUrl
196+
let excludeLabels = []
197+
let requireLabels = []
197198

198199
const format = getFormat(argv)
199200

200-
if (argv.version || argv.v)
201+
if (argv.version || argv.v) {
201202
return console.log(`v ${require('./package.json').version}`)
203+
}
202204

203-
if (argv['patch-only'])
204-
excludeLabels = [ 'semver-minor', 'semver-major' ]
205+
if (argv['patch-only']) {
206+
excludeLabels = ['semver-minor', 'semver-major']
207+
}
205208

206209
if (argv['exclude-label']) {
207-
if (!Array.isArray(argv['exclude-label']))
210+
if (!Array.isArray(argv['exclude-label'])) {
208211
argv['exclude-label'] = argv['exclude-label'].split(',')
212+
}
209213
excludeLabels = excludeLabels.concat(argv['exclude-label'])
210214
}
211215

212216
if (argv['require-label']) {
213-
if (!Array.isArray(argv['require-label']))
217+
if (!Array.isArray(argv['require-label'])) {
214218
argv['require-label'] = argv['require-label'].split(',')
219+
}
215220
requireLabels = requireLabels.concat(argv['require-label'])
216221
}
217222

218-
options = {
223+
const options = {
219224
group,
220225
excludeLabels,
221226
requireLabels,
222227
endRef
223228
}
224229

225230
branchDiff(branch1, branch2, options, (err, list) => {
226-
if (err)
227-
throw err
231+
if (err) { throw err }
228232

229-
if (argv['filter-release'])
233+
if (argv['filter-release']) {
230234
list = list.filter((commit) => !isReleaseCommit(commit.summary))
235+
}
231236

232237
printCommits(list, format, reverse, commitUrl)
233238
})
234239
}
240+
241+
if (require.main === module) {
242+
main()
243+
}

package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
"bin": {
77
"branch-diff": "./branch-diff.js"
88
},
9+
"scripts": {
10+
"lint": "standard",
11+
"format": "standard --fix"
12+
},
913
"author": "Rod <[email protected]> (http://r.va.gg/)",
1014
"license": "MIT",
1115
"dependencies": {
@@ -26,5 +30,8 @@
2630
"type": "git",
2731
"url": "https://github.com/rvagg/branch-diff.git"
2832
},
29-
"preferGlobal": true
33+
"preferGlobal": true,
34+
"devDependencies": {
35+
"standard": "^14.3.3"
36+
}
3037
}

0 commit comments

Comments
 (0)