Skip to content

Commit 4fbc36a

Browse files
VoltrexKeyvarvagg
authored andcommitted
feat!: refactor, format, and update
- Rewrite to ESM. - Refactor the JavaScript files. - Update dependencies and developer dependencies to their latest versions. - Format files with Prettier. - Add `package-lock.json` to `.gitignore`. - Add missing backtick in `README.md`.
1 parent 5ca9a54 commit 4fbc36a

File tree

6 files changed

+247
-187
lines changed

6 files changed

+247
-187
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules
2+
package-lock.json

LICENSE.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
The MIT License (MIT)
2-
=====================
1+
# The MIT License (MIT)
32

4-
Copyright (c) 2015 Rod Vagg
5-
---------------------------
3+
## Copyright (c) 2015 Rod Vagg
64

75
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
86

README.md

+15-16
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,38 @@ Intended to be used on a standard log format (not a simplified one using one of
1616
Typical usage:
1717

1818
```js
19-
var spawn = require('child_process').spawn
20-
, split2 = require('split2')
21-
, listStream = require('list-stream')
19+
import { spawn } from 'node:child_process'
20+
import split2 from 'split2'
21+
import listStream from 'list-stream'
2222

23-
spawn('bash', [ '-c', 'git log' ])
23+
spawn('bash', ['-c', 'git log'])
2424
.stdout.pipe(split2()) // break up by newline characters
2525
.pipe(commitStream('rvagg', 'commit-stream'))
2626
.pipe(listStream.obj(onCommitList))
2727

28-
2928
function onCommitList (err, list) {
3029
// list is an array of objects
3130
}
3231
```
3332

3433
Commit object properties:
3534

36-
* `sha`: the full commit sha
37-
* `author`: an object representing the author of the commit
35+
- `sha`: the full commit sha
36+
- `author`: an object representing the author of the commit
3837
- `name`: the name of the committer
3938
- `email`: the email of the committer
40-
* `authors`: a list of such objects representing the authors of the commit, supporting multiple authors through `Co-authored-by:`
41-
* `authorDate`: a string representing the date of the original commit by the author (never change)
42-
* `commitDate`: a string representing the date of the last change of the commit
43-
* `summary`: the one-line summary of the commit
44-
* `description`: the free-form text content of the summary, minus specific metadata lines
45-
* `reviewers`: an array containing objects with `name` and `email` properties if the commit metadata contains reviewers in a `Reviewed-By: Foo Bar <baz@boom>` format.
46-
* `changes`: if `--stat` data is included in the git log, an object containing change stats:
39+
- `authors`: a list of such objects representing the authors of the commit, supporting multiple authors through `Co-authored-by:`
40+
- `authorDate`: a string representing the date of the original commit by the author (never change)
41+
- `commitDate`: a string representing the date of the last change of the commit
42+
- `summary`: the one-line summary of the commit
43+
- `description`: the free-form text content of the summary, minus specific metadata lines
44+
- `reviewers`: an array containing objects with `name` and `email` properties if the commit metadata contains reviewers in a `Reviewed-By: Foo Bar <baz@boom>` format.
45+
- `changes`: if `--stat` data is included in the git log, an object containing change stats:
4746
- `files`: the number of files changed
4847
- `insertions`: the number of new lines inserted
4948
- `deletions`: the number of old lines removed
50-
* `prUrl`: a URL pointing to a pull-request where this change was made if the commit metadata contains a `PR-URL: https://github.com/user/project/pull/XX` line. Note that whatever proceeds the `PR-URL: ` string will be collected in this property; one exception being that if a shortened `#XX` version is found and you have supplied `defaultGitHubUser` and `defaultGitHubProject` arguments to the constructor then a full GitHub pull-request will be reconstituted in its place.
51-
* `ghUser`, `ghProject, `ghIssue`: if a proper GitHub pull request is found for the `prUrl` property (including shortened `#XX` ones), these properties will be added to point to the original user, project and issue (pull-request) for this change, as extracted from the URL.
49+
- `prUrl`: a URL pointing to a pull-request where this change was made if the commit metadata contains a `PR-URL: https://github.com/user/project/pull/XX` line. Note that whatever proceeds the `PR-URL: ` string will be collected in this property; one exception being that if a shortened `#XX` version is found and you have supplied `defaultGitHubUser` and `defaultGitHubProject` arguments to the constructor then a full GitHub pull-request will be reconstituted in its place.
50+
- `ghUser`, `ghProject`, `ghIssue`: if a proper GitHub pull request is found for the `prUrl` property (including shortened `#XX` ones), these properties will be added to point to the original user, project and issue (pull-request) for this change, as extracted from the URL.
5251

5352
## License
5453

commit-stream.js

+71-50
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,116 @@
1-
const through2 = require('through2')
2-
, stripAnsi = require('strip-ansi')
1+
import through2 from 'through2'
2+
import stripAnsi from 'strip-ansi'
33

4-
5-
module.exports = commitStream
6-
7-
8-
function commitStream (ghUser, ghProject) {
9-
var commit
4+
export default function commitStream (ghUser, ghProject) {
5+
let commit
106

117
return through2.obj(onLine, onEnd)
128

139
function addLine (line) {
1410
line = stripAnsi(line)
1511

16-
if (!line)
17-
return
12+
if (line.length === 0) return
1813

19-
var old, m
14+
let old, m
2015

2116
if (/^commit \w+$/.test(line)) {
2217
old = commit
23-
commit = {
24-
sha: line.split(' ')[1]
18+
commit = { sha: line.split(' ')[1] }
19+
} else if ((m = line.match(/^CommitDate:\s+(.*)$/)) !== null) {
20+
if (commit === undefined || commit === null) {
21+
throw new Error('Something unexpected occurred')
2522
}
26-
} else if (m = line.match(/^CommitDate:\s+(.*)$/)) {
27-
if (!commit)
28-
throw new Error('wut?')
2923
commit.commitDate = m[1].trim()
30-
} else if (m = line.match(/^\s*(?:Author|Co[- ]?authored[- ]?by):?\s*([^<]+) <([^>]+)>\s*$/i)) {
31-
if (!commit)
32-
throw new Error('wut?')
33-
if (!commit.authors)
24+
} else if (
25+
(m = line.match(
26+
/^\s*(?:Author|Co[- ]?authored[- ]?by):?\s*([^<]+) <([^>]+)>\s*$/i
27+
)) !== null
28+
) {
29+
if (commit === undefined || commit === null) {
30+
throw new Error('Something unexpected occurred')
31+
}
32+
if (commit.authors === undefined || commit.authors === null) {
3433
commit.authors = []
34+
}
3535
commit.authors.push({ name: m[1], email: m[2] })
36-
} else if (m = line.match(/^AuthorDate:\s+(.*)$/)) {
37-
if (!commit)
38-
throw new Error('wut?')
39-
commit.authorDate = m[1].trim()
40-
} else if (m = line.match(/^Date:\s+(.*)$/)) {
41-
if (!commit)
42-
throw new Error('wut?')
36+
} else if ((m = line.match(/^(?:AuthorDate|Date):\s+(.*)$/)) !== null) {
37+
if (commit === undefined || commit === null) {
38+
throw new Error('Something unexpected occurred')
39+
}
4340
commit.authorDate = m[1].trim()
44-
} else if (m = line.match(/^\s+Reviewed[- ]?By:?\s*([^<]+) <([^>]+)>\s*$/i)) {
45-
if (!commit.reviewers)
41+
} else if (
42+
(m = line.match(/^\s+Reviewed[- ]?By:?\s*([^<]+) <([^>]+)>\s*$/i)) !==
43+
null
44+
) {
45+
if (commit.reviewers === undefined || commit.reviewers === null) {
4646
commit.reviewers = []
47+
}
4748
commit.reviewers.push({ name: m[1], email: m[2] })
48-
} else if (m = line.match(/^\s+PR(?:[- ]?URL)?:?\s*(.+)\s*$/)) {
49+
} else if ((m = line.match(/^\s+PR(?:[- ]?URL)?:?\s*(.+)\s*$/)) !== null) {
4950
commit.prUrl = m[1]
50-
if (ghUser && ghProject && (m = commit.prUrl.match(/^\s*#?(\d+)\s*$/))) {
51-
commit.prUrl = 'https://github.com/' + ghUser + '/' + ghProject + '/pull/' + m[1]
51+
if (
52+
typeof ghUser === 'string' &&
53+
ghUser.length !== 0 &&
54+
typeof ghProject === 'string' &&
55+
ghProject.length !== 0 &&
56+
(m = commit.prUrl.match(/^\s*#?(\d+)\s*$/)) !== null
57+
) {
58+
commit.prUrl = `https://github.com/${ghUser}/${ghProject}/pull/${m[1]}`
5259
}
53-
if (m = commit.prUrl.match(/^(https?:\/\/.+\/([^\/]+)\/([^\/]+))\/\w+\/(\d+)(\/)?$/i)) {
54-
commit.ghIssue = +m[4]
55-
commit.ghUser = m[2]
60+
if (
61+
(m = commit.prUrl.match(
62+
/^(https?:\/\/.+\/([^/]+)\/([^/]+))\/\w+\/(\d+)(\/)?$/i
63+
)) !== null
64+
) {
65+
commit.ghIssue = parseInt(m[4])
66+
commit.ghUser = m[2]
5667
commit.ghProject = m[3]
5768
}
58-
} else if (/^ /.test(line) && (line = line.trim()).length) {
59-
if (!commit.summary) {
69+
} else if (/^ {4}/.test(line) && (line = line.trim()).length !== 0) {
70+
if (commit.summary === undefined || commit.summary === null) {
6071
commit.summary = line
6172
} else {
62-
if (!commit.description)
73+
if (commit.description === undefined || commit.description === null) {
6374
commit.description = []
75+
}
6476
commit.description.push(line)
6577
}
66-
} else if (/^ [^ ]/.test(line) && (line = line.trim()).length) {
67-
if (m = line.match(/^(\d+) files? changed(?:, (\d+) insertions?\(\+\))?(?:, (\d+) deletions?\(-\))?/)) {
68-
commit.changes = {
69-
files : Number(m[1])
70-
, insertions : Number(m[2] || 0)
71-
, deletions : Number(m[3] || 0)
72-
}
78+
} else if (
79+
/^ [^ ]/.test(line) &&
80+
(line = line.trim()).length !== 0 &&
81+
(m = line.match(
82+
/^(\d+) files? changed(?:, (\d+) insertions?\(\+\))?(?:, (\d+) deletions?\(-\))?/
83+
)) !== null
84+
) {
85+
commit.changes = {
86+
files: parseInt(m[1]),
87+
insertions: parseInt(m[2] ?? 0),
88+
deletions: parseInt(m[3] ?? 0)
7389
}
7490
}
7591

7692
return old
7793
}
7894

7995
function onLine (line, _, callback) {
80-
var commit = addLine(line)
81-
if (commit && commit.authors && commit.authors.length > 0)
96+
const commit = addLine(line)
97+
98+
if (commit?.authors?.length > 0) {
8299
commit.author = commit.authors[0]
83-
if (commit)
100+
84101
this.push(commit)
102+
}
103+
85104
callback()
86105
}
87106

88107
function onEnd (callback) {
89-
if (commit && commit.authors && commit.authors.length > 0)
108+
if (commit?.authors?.length > 0) {
90109
commit.author = commit.authors[0]
91-
if (commit)
110+
92111
this.push(commit)
112+
}
113+
93114
callback()
94115
}
95116
}

package.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "1.1.0",
44
"description": "Turn a `git log` into a stream of commit objects",
55
"main": "commit-stream.js",
6+
"type": "module",
67
"scripts": {
78
"build": "true",
89
"test:ci": "npm run test",
@@ -19,13 +20,13 @@
1920
},
2021
"homepage": "https://github.com/rvagg/commit-stream",
2122
"devDependencies": {
22-
"list-stream": "~1.0.0",
23-
"split2": "~1.0.0",
23+
"list-stream": "^2.1.0",
24+
"split2": "^4.2.0",
2425
"tape": "^5.6.3"
2526
},
2627
"dependencies": {
27-
"strip-ansi": "^6.0.1",
28-
"through2": "~2.0.0"
28+
"strip-ansi": "^7.0.1",
29+
"through2": "^4.0.2"
2930
},
3031
"release": {
3132
"branches": [

0 commit comments

Comments
 (0)