@hamzaahmadaslam/commit-changelog 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +233 -2
- package/bin/commit-changelog.mjs +4 -0
- package/package.json +40 -4
- package/src/changelog.mjs +156 -0
- package/src/classify.mjs +257 -0
- package/src/cli.mjs +212 -0
- package/src/conventional.mjs +40 -0
- package/src/errors.mjs +7 -0
- package/src/git.mjs +184 -0
- package/src/jev.mjs +92 -0
- package/src/report.mjs +220 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Hamza Ahmad Aslam
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,234 @@
|
|
|
1
|
-
#
|
|
1
|
+
# commit-changelog
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Turns the commits since your last tag into a [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) section, with
|
|
4
|
+
each commit's own first line under Added, Changed, Deprecated, Removed, Fixed or Security; for maintainers whose
|
|
5
|
+
commit messages do not follow a convention.
|
|
6
|
+
|
|
7
|
+
conventional-changelog, release-please and semantic-release sort commits by a prefix such as `feat:` or `fix:` and
|
|
8
|
+
leave out commits without one. Most repositories have plain messages ("Stop the CSV export from dropping the last
|
|
9
|
+
row"), so their changelogs are still written by hand. Matching words like "fix" or "add" with regular expressions
|
|
10
|
+
misplaces a message like that one. A text-generating model can sort it, but it rewrites the lines, can drop or
|
|
11
|
+
invent entries, and gives no measure of how sure it is. commit-changelog asks two narrow questions per commit and
|
|
12
|
+
gets probabilities back: code puts each commit's first line, unchanged, in a section, and the commits Jev is unsure
|
|
13
|
+
of go to a review list instead of the changelog.
|
|
14
|
+
|
|
15
|
+
## How it uses Jev
|
|
16
|
+
|
|
17
|
+
Jev is TypeSafe AI's System One model: it answers typed questions with probabilities and writes no text.
|
|
18
|
+
|
|
19
|
+
Commits with a conventional-commit prefix are placed by the prefix, in code, and never sent:
|
|
20
|
+
|
|
21
|
+
| Prefix | Section |
|
|
22
|
+
| ---------------------------------------------------------------------------------- | ------------------------------------ |
|
|
23
|
+
| `feat` | Added |
|
|
24
|
+
| `fix` | Fixed |
|
|
25
|
+
| `perf` | Changed |
|
|
26
|
+
| `docs`, `style`, `refactor`, `test`, `build`, `ci`, `chore` | internal, not listed |
|
|
27
|
+
| one of those internal types marked breaking (`refactor!:` or a `BREAKING CHANGE:` footer) | Changed |
|
|
28
|
+
| `revert` and any other type | asked, like a commit without a prefix |
|
|
29
|
+
|
|
30
|
+
Every other commit is sent to Jev, up to 20 commits per request (`--batch`), as a state like this:
|
|
31
|
+
|
|
32
|
+
```json
|
|
33
|
+
{
|
|
34
|
+
"commits": [
|
|
35
|
+
{
|
|
36
|
+
"subject": "Stop the CSV export from dropping the last row",
|
|
37
|
+
"body": "The writer closed the stream before the final row was flushed, so every export lost its last invoice.",
|
|
38
|
+
"changed_files": ["src/csv.js", "test/csv.test.js"]
|
|
39
|
+
}
|
|
40
|
+
]
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
For each commit it asks two questions, which point at the commit by its position:
|
|
45
|
+
|
|
46
|
+
| Question | Type | What it decides |
|
|
47
|
+
| --------------------------------------------------------------------------------------------------------- | ------ | -------------------------------------------------------------------- |
|
|
48
|
+
| `commits[3]` is one commit from the history of a software project [...]. In which section of the project's changelog does this change belong? | choice | added, changed, deprecated, removed, fixed, security or internal |
|
|
49
|
+
| Does the change in `commits[3]` affect the people who use the software, rather than only the people who develop it? | noul | whether users notice it; it has to agree with the section |
|
|
50
|
+
|
|
51
|
+
The full wording, with a description of every section, is in `src/classify.mjs`, and `--dry-run --json` prints every
|
|
52
|
+
request body. Code places each commit with one threshold, `--threshold` (default 0.8):
|
|
53
|
+
|
|
54
|
+
| Outcome | When |
|
|
55
|
+
| -------- | ------------------------------------------------------------------------------------------------------------- |
|
|
56
|
+
| listed | the section's confidence is at least the threshold, the section is not internal, and the users answer is at least the threshold |
|
|
57
|
+
| internal | the section is internal with a confidence of at least the threshold, and the users answer is at most 1 minus the threshold |
|
|
58
|
+
| review | everything else: a section Jev is unsure of, a users answer between the two limits, or answers that disagree |
|
|
59
|
+
|
|
60
|
+
Every entry in the section is a commit's first line exactly as it was written, prefix included. Jev only picks
|
|
61
|
+
where it goes. The report prints the probabilities next to every commit in review.
|
|
62
|
+
|
|
63
|
+
## Install
|
|
64
|
+
|
|
65
|
+
Needs Node.js 20 or later and git.
|
|
66
|
+
|
|
67
|
+
```sh
|
|
68
|
+
npm install -g @hamzaahmadaslam/commit-changelog
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Usage
|
|
72
|
+
|
|
73
|
+
```sh
|
|
74
|
+
export TYPESAFE_API_KEY=<your-key>
|
|
75
|
+
commit-changelog
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
That prints the section for the commits since the last tag of the repository in the current folder. In PowerShell,
|
|
79
|
+
set the key with `$env:TYPESAFE_API_KEY = "<your-key>"`.
|
|
80
|
+
|
|
81
|
+
```sh
|
|
82
|
+
commit-changelog --dry-run # how each commit would be placed, the questions, a token estimate; sends nothing
|
|
83
|
+
commit-changelog ../my-plugin --from v1.2.0 --to v1.3.0 # another repository and an explicit range
|
|
84
|
+
commit-changelog --version-label 1.3.0 --write CHANGELOG.md # add the section to the changelog file
|
|
85
|
+
commit-changelog --json > changelog.json
|
|
86
|
+
commit-changelog --threshold 0.9 # stricter: more commits go to review
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
| Option | Default | What it does |
|
|
90
|
+
| ------------------------ | ---------------- | ---------------------------------------------------------------------------------------------- |
|
|
91
|
+
| `[repository]` | `.` | A folder inside the git repository to read. |
|
|
92
|
+
| `--from <ref>` | the last tag | Start after this tag, branch or commit. |
|
|
93
|
+
| `--to <ref>` | `HEAD` | End at this tag, branch or commit. |
|
|
94
|
+
| `--version-label <text>` | `Unreleased` | The version in the heading: `## [1.3.0] - 2026-10-01`, or `## [Unreleased]` without a date. |
|
|
95
|
+
| `--date <YYYY-MM-DD>` | today | The release date in the heading. Needs `--version-label`. |
|
|
96
|
+
| `--include-internal` | off | Also list internal commits, under an Internal heading after Security. |
|
|
97
|
+
| `--write <file>` | off | Add the section to this changelog file (see below). |
|
|
98
|
+
| `--threshold <p>` | `0.8` | Confidence needed to place a commit. Above 0.5, at most 1. |
|
|
99
|
+
| `--batch <n>` | `20` | Commits per request, 1 to 50. |
|
|
100
|
+
| `--timeout <seconds>` | `10` | Time limit for each request. Rate limits (429) and overload (529) are retried three times. |
|
|
101
|
+
| `--json` | off | Print JSON: the range, the section, and every commit with its outcome and probabilities. |
|
|
102
|
+
| `--dry-run` | off | Print how each commit would be placed, one commit's questions and the token estimate. Needs no key and writes nothing. |
|
|
103
|
+
|
|
104
|
+
Without `--from`, the range starts after the nearest tag reachable from `--to`. When `--to` itself carries that tag
|
|
105
|
+
(you tagged the release already), it starts after the tag before, so the range is that release. Without any tag it
|
|
106
|
+
covers every commit up to `--to`. Merge commits are skipped.
|
|
107
|
+
|
|
108
|
+
Environment: `TYPESAFE_API_KEY` (needed only when a commit has no conventional-commit prefix, and not for
|
|
109
|
+
`--dry-run`) and `TYPESAFE_MODEL` (default `jev-latest`).
|
|
110
|
+
|
|
111
|
+
Exit codes: `0` when every commit was placed, `1` when at least one is in review, so a release job can stop for a
|
|
112
|
+
person, and `2` on an error.
|
|
113
|
+
|
|
114
|
+
### Writing to a changelog file
|
|
115
|
+
|
|
116
|
+
`--write` adds the section to the file and changes nothing else in it. The section goes above the newest release
|
|
117
|
+
heading, below an `[Unreleased]` section at the top of the file, or at the end of a file without release headings.
|
|
118
|
+
Line endings are kept. A missing file is created with a `# Changelog` title and one line about the format. When the
|
|
119
|
+
file already has a section for the same version, the tool stops with exit code 2 before anything is sent to
|
|
120
|
+
TypeSafe, and nothing is written. Commits in review are not written; add them by hand if they belong.
|
|
121
|
+
|
|
122
|
+
## Example
|
|
123
|
+
|
|
124
|
+
`examples/history.json` describes a made-up history of a small invoice export tool, written for this example: 16
|
|
125
|
+
commits, the second one tagged v1.2.0. `examples/run.mjs` builds it as a throwaway git repository in a temporary
|
|
126
|
+
folder and runs `commit-changelog --version-label 1.3.0 --date 2026-10-01` on it. The probabilities come from
|
|
127
|
+
`examples/fixture-answers.json`: they were written by hand for the tests, not recorded from TypeSafe, and show the
|
|
128
|
+
report format. Your numbers will differ. `node examples/run.mjs` prints this report without a key or a network call.
|
|
129
|
+
|
|
130
|
+
```text
|
|
131
|
+
commit-changelog: 14 commits in v1.2.0..HEAD (merge commits skipped)
|
|
132
|
+
Model jev-1.13.0, 1 request, 4,413 input tokens, threshold 0.8
|
|
133
|
+
|
|
134
|
+
listed 8 internal 4 review 2 (4 placed by their conventional-commit prefix, 10 asked)
|
|
135
|
+
|
|
136
|
+
## [1.3.0] - 2026-10-01
|
|
137
|
+
|
|
138
|
+
### Added
|
|
139
|
+
|
|
140
|
+
- feat(export): add a --since option
|
|
141
|
+
- Read settings from invoice-export.json when it exists
|
|
142
|
+
|
|
143
|
+
### Changed
|
|
144
|
+
|
|
145
|
+
- Stream rows while exporting instead of loading every invoice first
|
|
146
|
+
|
|
147
|
+
### Deprecated
|
|
148
|
+
|
|
149
|
+
- Deprecate the --format flag in favour of --output-format
|
|
150
|
+
|
|
151
|
+
### Removed
|
|
152
|
+
|
|
153
|
+
- Drop support for Node 18
|
|
154
|
+
|
|
155
|
+
### Fixed
|
|
156
|
+
|
|
157
|
+
- Stop the CSV export from dropping the last row
|
|
158
|
+
- fix: show a clear error when the output folder is missing
|
|
159
|
+
|
|
160
|
+
### Security
|
|
161
|
+
|
|
162
|
+
- Escape formula characters in CSV cells
|
|
163
|
+
|
|
164
|
+
Review: these commits were not placed. Add them to the section by hand if they belong.
|
|
165
|
+
fbe5098 Tweak the cache
|
|
166
|
+
changed 0.46, fixed 0.29, internal 0.19 (confidence 0.37) | affects users 0.58
|
|
167
|
+
unclear which section, unclear if users notice it
|
|
168
|
+
6eafc7c Handle invoices without line items
|
|
169
|
+
fixed 0.62, added 0.30, changed 0.08 (confidence 0.56) | affects users 0.90
|
|
170
|
+
unclear which section
|
|
171
|
+
|
|
172
|
+
4 internal commits not listed. --include-internal lists them under Internal.
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
The four commits with a prefix were placed without Jev. "Tweak the cache" says too little to place, and "Handle
|
|
176
|
+
invoices without line items" could be a fix or a feature, so both went to review. "Refactor the date helpers" and
|
|
177
|
+
"Update test fixtures" came back internal. The same run with `--json` is in `examples/report.json`, and the dry run
|
|
178
|
+
in `examples/dry-run.txt`.
|
|
179
|
+
|
|
180
|
+
## What leaves your machine
|
|
181
|
+
|
|
182
|
+
Only when you run it with a key and without `--dry-run`, only for commits without a conventional-commit prefix, and
|
|
183
|
+
only to `https://api.typesafe.ai/v1/systemone`:
|
|
184
|
+
|
|
185
|
+
- each such commit's first line, and the rest of its message up to 1,200 characters, with trailer lines that carry
|
|
186
|
+
an email address (`Signed-off-by:` and similar) removed and any other email address replaced by `[email]`;
|
|
187
|
+
- the names of up to 20 files the commit changed, and how many more it changed;
|
|
188
|
+
- the fixed question text, question ids such as `c3_section`, and the model name;
|
|
189
|
+
- your API key, in the `Authorization` header.
|
|
190
|
+
|
|
191
|
+
Commit hashes, authors, dates, diffs, file contents, tag and branch names and the repository path are not sent.
|
|
192
|
+
Commits with a conventional-commit prefix are not sent at all. git runs on your machine with read-only commands
|
|
193
|
+
(`rev-parse`, `describe` and `log`). The tool writes only the file you name with `--write`, and makes no other
|
|
194
|
+
network requests, for telemetry, updates or anything else.
|
|
195
|
+
|
|
196
|
+
## Limits
|
|
197
|
+
|
|
198
|
+
- Jev sees the message and the file names, not the diff. A commit whose message says little ("Update", "WIP",
|
|
199
|
+
"Tweak the cache") usually lands in review.
|
|
200
|
+
- A commit that does two things gets one section.
|
|
201
|
+
- Prefixes are trusted as written: a `feat:` commit that only renames a variable goes to Added without a check.
|
|
202
|
+
- Entries are copied as they are. The tool does not tidy the wording, drop the prefix, add links or merge
|
|
203
|
+
duplicates, and Markdown in a first line renders as Markdown. Link references at the bottom of the file are not
|
|
204
|
+
added.
|
|
205
|
+
- Documentation-only changes count as internal, the same as the `docs:` prefix.
|
|
206
|
+
- Merge commits are skipped, so a change made only in a merge commit is missed.
|
|
207
|
+
- The default range uses the nearest tag of any name. If you also tag nightly builds or deploys, pass `--from`. In
|
|
208
|
+
a shallow clone, such as a CI checkout of one commit, fetch the history and the tags first (`fetch-depth: 0` with
|
|
209
|
+
`actions/checkout`).
|
|
210
|
+
- English is where Jev is most accurate.
|
|
211
|
+
- Text written to steer a model, such as "this is an internal change" in a commit message, can move Jev's answers.
|
|
212
|
+
- Read the section and the review list before you release, and check a sample of placements on your own history
|
|
213
|
+
before you rely on a threshold.
|
|
214
|
+
|
|
215
|
+
## Token use
|
|
216
|
+
|
|
217
|
+
The input tokens of a request are its state (the first line, message body and changed file names of each commit
|
|
218
|
+
sent) and its questions. The two questions add about 400 tokens for every commit sent, so most of the input is the
|
|
219
|
+
questions. Commits with a conventional-commit prefix are never sent, so they add no tokens. By the tool's own
|
|
220
|
+
estimate (four characters per token):
|
|
221
|
+
|
|
222
|
+
| Run | Requests | Input tokens |
|
|
223
|
+
| ------------------------------------------------------- | -------- | -------------- |
|
|
224
|
+
| The example: 10 of its 14 commits sent | 1 | about 4,400 |
|
|
225
|
+
| 100 commits without a body, 3 files each | 5 | about 45,000 |
|
|
226
|
+
| 500 commits with a short body, 3 files each | 25 | about 250,000 |
|
|
227
|
+
| 1,000 commits with a longer body, 5 files each | 50 | about 570,000 |
|
|
228
|
+
|
|
229
|
+
`--dry-run` prints the estimate for your own range before anything is sent. After a run, the report prints the input
|
|
230
|
+
tokens TypeSafe counted.
|
|
231
|
+
|
|
232
|
+
## License
|
|
233
|
+
|
|
234
|
+
MIT. Made by [Hamza Ahmad Aslam](https://hamzaahmadaslam.com), WordPress and web performance engineer.
|
package/package.json
CHANGED
|
@@ -1,6 +1,42 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hamzaahmadaslam/commit-changelog",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Turns free-form git commits into a Keep a Changelog section: each commit's own first line, placed by its conventional-commit prefix or by TypeSafe's Jev model, with unsure commits listed for review.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"commit-changelog": "bin/commit-changelog.mjs"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"bin",
|
|
11
|
+
"src"
|
|
12
|
+
],
|
|
13
|
+
"engines": {
|
|
14
|
+
"node": ">=20"
|
|
15
|
+
},
|
|
16
|
+
"scripts": {
|
|
17
|
+
"test": "node --test"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"changelog",
|
|
21
|
+
"keep-a-changelog",
|
|
22
|
+
"git",
|
|
23
|
+
"commits",
|
|
24
|
+
"release-notes",
|
|
25
|
+
"cli",
|
|
26
|
+
"typesafe",
|
|
27
|
+
"jev"
|
|
28
|
+
],
|
|
29
|
+
"author": "Hamza Ahmad Aslam (https://hamzaahmadaslam.com)",
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "git+https://github.com/hamzaahmadaslam/commit-changelog.git"
|
|
34
|
+
},
|
|
35
|
+
"homepage": "https://hamzaahmadaslam.com/open-source/commit-changelog",
|
|
36
|
+
"bugs": {
|
|
37
|
+
"url": "https://github.com/hamzaahmadaslam/commit-changelog/issues"
|
|
38
|
+
},
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
// Assembles a Keep a Changelog 1.1.0 section from the placed commits and adds it to a changelog file. Every entry
|
|
2
|
+
// is a commit's own first line, copied as it is. Adding the section to a file changes nothing else in the file.
|
|
3
|
+
import { readFileSync, statSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { SECTIONS } from "./classify.mjs";
|
|
5
|
+
import { UserError } from "./errors.mjs";
|
|
6
|
+
|
|
7
|
+
export const UNRELEASED = "Unreleased";
|
|
8
|
+
|
|
9
|
+
export const SECTION_TITLES = {
|
|
10
|
+
added: "Added",
|
|
11
|
+
changed: "Changed",
|
|
12
|
+
deprecated: "Deprecated",
|
|
13
|
+
removed: "Removed",
|
|
14
|
+
fixed: "Fixed",
|
|
15
|
+
security: "Security",
|
|
16
|
+
internal: "Internal",
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/** The first lines of a new changelog file, when --write names a file that does not exist yet. */
|
|
20
|
+
export const NEW_FILE_HEADER =
|
|
21
|
+
"# Changelog\n\nAll notable changes to this project are documented in this file. The format is based on " +
|
|
22
|
+
"[Keep a Changelog](https://keepachangelog.com/en/1.1.0/).\n\n";
|
|
23
|
+
|
|
24
|
+
export const isUnreleased = (label) => label.toLowerCase() === UNRELEASED.toLowerCase();
|
|
25
|
+
|
|
26
|
+
/** The section heading: "## [1.3.0] - 2026-10-01", or "## [Unreleased]" without a date. */
|
|
27
|
+
export function heading(label, date) {
|
|
28
|
+
return isUnreleased(label) ? `## [${UNRELEASED}]` : `## [${label}] - ${date}`;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The section as Markdown, or null when no commit is listed. Listed commits go under their section in Keep a
|
|
33
|
+
* Changelog order, in history order within a section; internal commits go under Internal only with
|
|
34
|
+
* `includeInternal`. Returns { text, entries }.
|
|
35
|
+
*/
|
|
36
|
+
export function buildSection(entries, { label = UNRELEASED, date, includeInternal = false } = {}) {
|
|
37
|
+
const groups = Object.fromEntries(SECTIONS.map((section) => [section, []]));
|
|
38
|
+
for (const entry of entries) {
|
|
39
|
+
if (entry.outcome === "listed" || (entry.outcome === "internal" && includeInternal)) {
|
|
40
|
+
groups[entry.section].push(entry.commit.firstLine);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const lines = [heading(label, date)];
|
|
44
|
+
let count = 0;
|
|
45
|
+
for (const section of SECTIONS) {
|
|
46
|
+
if (!groups[section].length) continue;
|
|
47
|
+
lines.push("", `### ${SECTION_TITLES[section]}`, "", ...groups[section].map((line) => `- ${line}`));
|
|
48
|
+
count += groups[section].length;
|
|
49
|
+
}
|
|
50
|
+
return { text: count ? `${lines.join("\n")}\n` : null, entries: count };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* What a "## ..." heading names, for comparing: { key, release }. The key drops brackets, a leading v before a digit
|
|
55
|
+
* and anything after the name. A release heading is bracketed, starts with a digit, or is Unreleased.
|
|
56
|
+
*/
|
|
57
|
+
function readHeading(text) {
|
|
58
|
+
const bracketed = /^\[([^\]]*)\]/.exec(text);
|
|
59
|
+
const name = bracketed ? bracketed[1] : (text.split(/\s/)[0] ?? "");
|
|
60
|
+
const key = name.trim().replace(/^v(?=\d)/i, "").toLowerCase();
|
|
61
|
+
return { key, release: Boolean(bracketed) || /^\d/.test(key) || key === UNRELEASED.toLowerCase() };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const BOM = String.fromCharCode(0xfeff);
|
|
65
|
+
const FENCE = /^ {0,3}(`{3,}|~{3,})(.*)$/;
|
|
66
|
+
const LEVEL_TWO = /^ {0,3}##[ \t]+(.*?)[ \t#]*$/;
|
|
67
|
+
|
|
68
|
+
/** The release headings of a changelog, outside fenced code blocks, as { index, key } with the offset of each line. */
|
|
69
|
+
function releaseHeadings(body) {
|
|
70
|
+
const headings = [];
|
|
71
|
+
let fence = null;
|
|
72
|
+
// In multiline mode `.` stops before \r as well as \n, so CRLF files give an extra empty line that matches nothing.
|
|
73
|
+
for (const match of body.matchAll(/^.*$/gm)) {
|
|
74
|
+
const line = match[0];
|
|
75
|
+
const opener = FENCE.exec(line);
|
|
76
|
+
if (fence) {
|
|
77
|
+
if (opener && opener[1][0] === fence[0] && opener[1].length >= fence.length && !opener[2].trim()) fence = null;
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
if (opener) {
|
|
81
|
+
fence = opener[1];
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
const found = LEVEL_TWO.exec(line);
|
|
85
|
+
if (!found) continue;
|
|
86
|
+
const { key, release } = readHeading(found[1]);
|
|
87
|
+
if (release) headings.push({ index: match.index, key });
|
|
88
|
+
}
|
|
89
|
+
return headings;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Adds `section` to the changelog `text` and returns the new text. The section goes above the first release
|
|
94
|
+
* heading, so the newest release comes first; a version goes below an [Unreleased] section that sits at the top.
|
|
95
|
+
* Without a release heading it goes at the end. Line endings and a byte order mark are kept. Throws a UserError when
|
|
96
|
+
* the file already has a section with the same label, and changes nothing.
|
|
97
|
+
*/
|
|
98
|
+
export function insertSection(text, section, label) {
|
|
99
|
+
const bom = text.startsWith(BOM) ? BOM : "";
|
|
100
|
+
const body = text.slice(bom.length);
|
|
101
|
+
const eol = body.includes("\r\n") ? "\r\n" : "\n";
|
|
102
|
+
const { key } = readHeading(`[${label}]`);
|
|
103
|
+
const headings = releaseHeadings(body);
|
|
104
|
+
|
|
105
|
+
if (headings.some((h) => h.key === key)) {
|
|
106
|
+
const hint = isUnreleased(label) ? " Pass --version-label to add a release section, or edit that section by hand." : "";
|
|
107
|
+
throw new UserError(`The changelog already has a section for ${label}. Nothing was written.${hint}`);
|
|
108
|
+
}
|
|
109
|
+
const unreleasedFirst = headings[0]?.key === UNRELEASED.toLowerCase();
|
|
110
|
+
const at = unreleasedFirst && !isUnreleased(label) ? headings[1]?.index : headings[0]?.index;
|
|
111
|
+
const block = section.replace(/\n/g, eol);
|
|
112
|
+
|
|
113
|
+
if (at === undefined) {
|
|
114
|
+
// No release heading after the insertion point: add the section at the end, after a blank line.
|
|
115
|
+
let before = body;
|
|
116
|
+
if (before && !before.endsWith(eol)) before += eol;
|
|
117
|
+
if (before && !before.endsWith(eol + eol)) before += eol;
|
|
118
|
+
return bom + before + block;
|
|
119
|
+
}
|
|
120
|
+
// A heading starts a line, so `before` is empty or ends with a line break; add one more unless a blank line is there.
|
|
121
|
+
const before = body.slice(0, at);
|
|
122
|
+
const gap = before.trim() && !before.endsWith(eol + eol) ? eol : "";
|
|
123
|
+
return bom + before + gap + block + eol + body.slice(at);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** The text of the changelog at `file`, or null when there is no such file. */
|
|
127
|
+
function readChangelog(file) {
|
|
128
|
+
try {
|
|
129
|
+
if (statSync(file, { throwIfNoEntry: false })?.isDirectory()) throw new Error("it is a folder");
|
|
130
|
+
return readFileSync(file, "utf8");
|
|
131
|
+
} catch (error) {
|
|
132
|
+
if (error?.code === "ENOENT") return null;
|
|
133
|
+
throw new UserError(`Cannot read ${file}: ${error.message}`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Throws the UserError --write would end with, before anything is sent: an unreadable file, or the label already there. */
|
|
138
|
+
export function checkChangelog(file, label) {
|
|
139
|
+
const existing = readChangelog(file);
|
|
140
|
+
if (existing !== null) insertSection(existing, "", label);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Adds the section to the changelog at `file`, or creates the file with a short header when it does not exist.
|
|
145
|
+
* Returns { created }. Reading or writing errors become a UserError.
|
|
146
|
+
*/
|
|
147
|
+
export function writeChangelog(file, section, label) {
|
|
148
|
+
const existing = readChangelog(file);
|
|
149
|
+
const next = existing === null ? NEW_FILE_HEADER + section : insertSection(existing, section, label);
|
|
150
|
+
try {
|
|
151
|
+
writeFileSync(file, next);
|
|
152
|
+
} catch (error) {
|
|
153
|
+
throw new UserError(`Cannot write ${file}: ${error.message}`);
|
|
154
|
+
}
|
|
155
|
+
return { created: existing === null };
|
|
156
|
+
}
|