Skip to content

Commit ab6e398

Browse files
committed
feat!: add types and exports; modernise tests
BREAKING CHANGE: package exports restrict imports to the package root
1 parent d67bff9 commit ab6e398

9 files changed

Lines changed: 422 additions & 328 deletions

File tree

‎.github/workflows/test-and-release.yml‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ jobs:
2525
- name: Install Dependencies
2626
run: |
2727
npm install --no-progress
28-
- name: Run tests
28+
- name: Check build is up to date
2929
run: |
30-
npm config set script-shell bash
31-
npm run test:ci
30+
npm run build
31+
git diff --exit-code || (echo "::error::Build artifacts not committed. Run 'npm run build' and commit the changes." && exit 1)
32+
- name: Run tests
33+
run: npm test
3234
release:
3335
name: Release
3436
permissions:

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
22
package-lock.json
3+
*.tsbuildinfo

‎README.md‎

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22

33
**Turn a `git log` into a stream of commit objects**
44

5-
[![npm](https://nodei.co/npm/commit-stream.png?downloads=true&downloadRank=true)](https://nodei.co/npm/commit-stream/)
6-
[![npm](https://nodei.co/npm-dl/commit-stream.png?months=6&height=3)](https://nodei.co/npm/commit-stream/)
5+
[![NPM](https://nodei.co/npm/commit-stream.svg?style=flat&data=n,v&color=blue)](https://nodei.co/npm/commit-stream/)
76

87
## API
98

109
**`commitStream([defaultGithubUser, defaultGithubProject])`**
1110

12-
Returns an object stream that you can pipe a stream of newlines from the output of `git log` to. The output of the stream is a series of objects, each representing a commit from the log.`
11+
Returns an object stream that you can pipe a stream of newlines from the output of `git log` to. The output of the stream is a series of objects, each representing a commit from the log.
1312

1413
Intended to be used on a standard log format (not a simplified one using one of the simplifying `--format` options) but it will also process change stats if you `git log --stat`.
1514

@@ -18,18 +17,20 @@ Typical usage:
1817
```js
1918
import { spawn } from 'node:child_process'
2019
import { createInterface } from 'node:readline'
21-
import { Readable } from 'node:stream'
22-
import listStream from 'list-stream'
23-
24-
const { stdout } = spawn('bash', ['-c', 'git log'])
25-
26-
Readable.from(createInterface({ input: stdout, crlfDelay: Infinity })) // break up by newline characters
27-
.pipe(commitStream('rvagg', 'commit-stream'))
28-
.pipe(listStream.obj(onCommitList))
29-
30-
function onCommitList (err, list) {
31-
// list is an array of objects
32-
}
20+
import { pipeline } from 'node:stream/promises'
21+
import commitStream from 'commit-stream'
22+
23+
const { stdout } = spawn('git', ['log'])
24+
const commits = []
25+
26+
await pipeline(
27+
createInterface({ input: stdout, crlfDelay: Infinity }), // break up by newline characters
28+
commitStream('rvagg', 'commit-stream'),
29+
async (source) => {
30+
for await (const commit of source) commits.push(commit)
31+
}
32+
)
33+
// commits is an array of objects
3334
```
3435

3536
Commit object properties:

‎commit-stream.js‎

Lines changed: 85 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,120 @@
11
import { Transform } from 'node:stream'
22
import { stripVTControlCharacters } from 'node:util'
33

4+
/**
5+
* @typedef {Object} Person
6+
* @property {string} name
7+
* @property {string} email
8+
*/
9+
10+
/**
11+
* @typedef {Object} Changes
12+
* @property {number} files
13+
* @property {number} insertions
14+
* @property {number} deletions
15+
*/
16+
17+
/**
18+
* @typedef {Object} Commit
19+
* @property {string} sha
20+
* @property {Person} [author]
21+
* @property {Person[]} [authors]
22+
* @property {string} [authorDate]
23+
* @property {string} [commitDate]
24+
* @property {string} [summary]
25+
* @property {string[]} [description]
26+
* @property {Person[]} [reviewers]
27+
* @property {Changes} [changes]
28+
* @property {string} [prUrl]
29+
* @property {string} [ghUser]
30+
* @property {string} [ghProject]
31+
* @property {number} [ghIssue]
32+
* @property {string} [cveId]
33+
*/
34+
35+
/**
36+
* Create an object stream that turns lines of `git log` output into commit objects.
37+
*
38+
* @param {string} [ghUser] - GitHub user used to expand short `#XX` PR references
39+
* @param {string} [ghProject] - GitHub project used to expand short `#XX` PR references
40+
* @returns {Transform}
41+
*/
442
export default function commitStream (ghUser, ghProject) {
43+
/** @type {Commit | undefined} */
544
let commit
645

7-
return new Transform({ objectMode: true, transform: onLine, flush: onEnd })
46+
return new Transform({
47+
objectMode: true,
48+
transform (line, _, callback) {
49+
emit(this, addLine(line))
50+
callback()
51+
},
52+
flush (callback) {
53+
emit(this, commit)
54+
callback()
55+
}
56+
})
857

58+
/**
59+
* @param {string} line
60+
* @returns {Commit | undefined} the previous commit, when `line` starts a new one
61+
*/
962
function addLine (line) {
1063
line = stripVTControlCharacters(line)
1164

1265
if (line.length === 0) return
1366

14-
let old, m
15-
1667
if (/^commit \w+$/.test(line)) {
17-
old = commit
68+
const old = commit
1869
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')
22-
}
70+
return old
71+
}
72+
73+
let m
74+
75+
if (!commit) {
76+
throw new Error('Something unexpected occurred')
77+
}
78+
79+
if ((m = line.match(/^CommitDate:\s+(.*)$/)) !== null) {
2380
commit.commitDate = m[1].trim()
2481
} else if (
2582
(m = line.match(
2683
/^\s*(?:Author|Co[- ]?authored[- ]?by):?\s*([^<]+) <([^>]+)>\s*$/i
2784
)) !== null
2885
) {
29-
if (commit === undefined || commit === null) {
30-
throw new Error('Something unexpected occurred')
31-
}
32-
if (commit.authors === undefined || commit.authors === null) {
33-
commit.authors = []
34-
}
35-
commit.authors.push({ name: m[1], email: m[2] })
86+
(commit.authors ??= []).push({ name: m[1], email: m[2] })
3687
} else if ((m = line.match(/^(?:AuthorDate|Date):\s+(.*)$/)) !== null) {
37-
if (commit === undefined || commit === null) {
38-
throw new Error('Something unexpected occurred')
39-
}
4088
commit.authorDate = m[1].trim()
4189
} else if (
4290
(m = line.match(/^\s+Reviewed[- ]?By:?\s*([^<]+) <([^>]+)>\s*$/i)) !==
4391
null
4492
) {
45-
if (commit.reviewers === undefined || commit.reviewers === null) {
46-
commit.reviewers = []
47-
}
48-
commit.reviewers.push({ name: m[1], email: m[2] })
93+
(commit.reviewers ??= []).push({ name: m[1], email: m[2] })
4994
} else if ((m = line.match(/\bCVE-ID:\s+(CVE-\d{4}-\d{4,})\b/)) !== null) {
5095
commit.cveId = m[1]
5196
} else if ((m = line.match(/^\s+PR(?:[- ]?URL)?:?\s*(.+)\s*$/) || line.match(/\(#(\d+)\)$/)) !== null) {
5297
commit.prUrl = m[1]
53-
if (
54-
typeof ghUser === 'string' &&
55-
ghUser.length !== 0 &&
56-
typeof ghProject === 'string' &&
57-
ghProject.length !== 0 &&
58-
(m = commit.prUrl.match(/^\s*#?(\d+)\s*$/)) !== null
59-
) {
98+
if (ghUser && ghProject && (m = commit.prUrl.match(/^\s*#?(\d+)\s*$/)) !== null) {
6099
commit.prUrl = `https://github.com/${ghUser}/${ghProject}/pull/${m[1]}`
61100
}
62101
if (
63102
(m = commit.prUrl.match(
64103
/^(https?:\/\/.+\/([^/]+)\/([^/]+))\/\w+\/(\d+)(\/)?$/i
65104
)) !== null
66105
) {
67-
commit.ghIssue = parseInt(m[4])
106+
commit.ghIssue = Number(m[4])
68107
commit.ghUser = m[2]
69108
commit.ghProject = m[3]
70109
}
71110
if ((m = line.match(/^ {4}(.*)\s\(#\d+\)$/)) && !commit.summary) {
72111
commit.summary = m[1]
73112
}
74113
} else if (/^ {4}/.test(line) && (line = line.trim()).length !== 0) {
75-
if (commit.summary === undefined || commit.summary === null) {
114+
if (commit.summary === undefined) {
76115
commit.summary = line
77116
} else {
78-
if (commit.description === undefined || commit.description === null) {
79-
commit.description = []
80-
}
81-
commit.description.push(line)
117+
(commit.description ??= []).push(line)
82118
}
83119
} else if (
84120
/^ [^ ]/.test(line) &&
@@ -88,34 +124,23 @@ export default function commitStream (ghUser, ghProject) {
88124
)) !== null
89125
) {
90126
commit.changes = {
91-
files: parseInt(m[1]),
92-
insertions: parseInt(m[2] ?? 0),
93-
deletions: parseInt(m[3] ?? 0)
127+
files: Number(m[1]),
128+
insertions: Number(m[2] ?? 0),
129+
deletions: Number(m[3] ?? 0)
94130
}
95131
}
96-
97-
return old
98-
}
99-
100-
function onLine (line, _, callback) {
101-
const commit = addLine(line)
102-
103-
if (commit?.authors?.length > 0) {
104-
commit.author = commit.authors[0]
105-
106-
this.push(commit)
107-
}
108-
109-
callback()
110132
}
133+
}
111134

112-
function onEnd (callback) {
113-
if (commit?.authors?.length > 0) {
114-
commit.author = commit.authors[0]
115-
116-
this.push(commit)
117-
}
118-
119-
callback()
135+
/**
136+
* Push a completed commit, skipping any without an author.
137+
*
138+
* @param {Transform} stream
139+
* @param {Commit | undefined} commit
140+
*/
141+
function emit (stream, commit) {
142+
if (commit?.authors?.length) {
143+
commit.author = commit.authors[0]
144+
stream.push(commit)
120145
}
121146
}

‎package.json‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,21 @@
33
"version": "2.2.6",
44
"description": "Turn a `git log` into a stream of commit objects",
55
"main": "commit-stream.js",
6+
"types": "types/commit-stream.d.ts",
7+
"exports": {
8+
".": {
9+
"types": "./types/commit-stream.d.ts",
10+
"default": "./commit-stream.js"
11+
}
12+
},
613
"type": "module",
714
"scripts": {
8-
"build": "true",
9-
"test:ci": "npm run test",
10-
"test": "node --test test.js"
15+
"lint": "standard",
16+
"build": "npm run build:types",
17+
"build:types": "tsc --build",
18+
"prepublishOnly": "npm run build",
19+
"test:unit": "node --test test.js",
20+
"test": "npm run lint && npm run test:unit"
1121
},
1222
"repository": {
1323
"type": "git",
@@ -22,9 +32,11 @@
2232
"devDependencies": {
2333
"@semantic-release/changelog": "^7.0.0",
2434
"@semantic-release/git": "^11.0.0",
35+
"@types/node": "^24.0.0",
2536
"conventional-changelog-conventionalcommits": "^9.3.1",
26-
"list-stream": "^2.1.0",
27-
"semantic-release": "^25.0.3"
37+
"semantic-release": "^25.0.3",
38+
"standard": "^17.1.2",
39+
"typescript": "^5.9.3"
2840
},
2941
"release": {
3042
"branches": [

0 commit comments

Comments
 (0)