@jjloneman/kit-changesets-changelog 0.0.0-stage → 0.0.1-stage
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 +61 -2
- package/dist/index.d.ts +159 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +279 -0
- package/dist/index.js.map +1 -0
- package/package.json +59 -3
- package/src/index.ts +499 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 JJ Loneman
|
|
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,62 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @jjloneman/kit-changesets-changelog
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
- A [Changesets](https://github.com/changesets/changesets) changelog formatter
|
|
4
|
+
that prefixes each line with the gitmoji and scope of the commit that added
|
|
5
|
+
the changeset.
|
|
6
|
+
- After a squash merge, that commit's subject is the PR title, so a repo whose
|
|
7
|
+
PR titles follow `<type>(<scope>): <gitmoji> <subject>` gets a changelog that
|
|
8
|
+
reads like its history.
|
|
9
|
+
- Part of [jj-kit](https://github.com/jjloneman/jj-kit), my shared repo kit.
|
|
10
|
+
|
|
11
|
+
## 📦 Install
|
|
12
|
+
|
|
13
|
+
- Node 26 or later.
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
pnpm add --save-dev --save-exact @jjloneman/kit-changesets-changelog
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
- Then name it in `.changeset/config.json`:
|
|
20
|
+
|
|
21
|
+
```json
|
|
22
|
+
{ "changelog": "@jjloneman/kit-changesets-changelog" }
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## 📝 Lines
|
|
26
|
+
|
|
27
|
+
- **Each changeset's line** is
|
|
28
|
+
`- <gitmoji> **<scope>:** <summary> (<short hash>)`.
|
|
29
|
+
- A `!` after the type adds `**Breaking:**` after the scope.
|
|
30
|
+
- A subject that isn't a conventional commit just drops the prefix.
|
|
31
|
+
- A commit git has no record of, such as a hash from before a squash, gives
|
|
32
|
+
the bare summary with no hash.
|
|
33
|
+
- A commit git fails to read for another reason keeps its hash, without a
|
|
34
|
+
gitmoji or scope, and prints a warning.
|
|
35
|
+
- A scope's Markdown characters are escaped, so `fix(*)` stays bold.
|
|
36
|
+
- A summary's later lines are indented under the bullet.
|
|
37
|
+
- **Dependency bumps** get one `Updated dependencies` line naming the
|
|
38
|
+
changesets' commits, then one sub-item per bumped package.
|
|
39
|
+
- **git runs from the current directory**, since Changesets passes the formatter
|
|
40
|
+
none; run `changeset version` from the repo root, as it expects.
|
|
41
|
+
- **A sample `CHANGELOG.md`**, from a PR titled
|
|
42
|
+
`feat(eslint-config): ✨ add the react preset (#12)`:
|
|
43
|
+
|
|
44
|
+
```md
|
|
45
|
+
## 1.1.0
|
|
46
|
+
|
|
47
|
+
### Minor Changes
|
|
48
|
+
|
|
49
|
+
- ✨ **eslint-config:** Add the react preset. (b4fc42d)
|
|
50
|
+
|
|
51
|
+
### Patch Changes
|
|
52
|
+
|
|
53
|
+
- Updated dependencies [0a1b2c3]
|
|
54
|
+
- @jjloneman/kit-eslint-plugin@1.2.0
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## 🧩 API
|
|
58
|
+
|
|
59
|
+
- The default export is Changesets'
|
|
60
|
+
`{ getReleaseLine, getDependencyReleaseLine }`.
|
|
61
|
+
- `formatReleaseLine`, `parseConventionalSubject` and `readCommitSubject` are
|
|
62
|
+
also exported, with their types, for reuse.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The two functions changesets calls.
|
|
3
|
+
*
|
|
4
|
+
* - Narrower than changesets' own `ChangelogFunctions`: each returns a plain
|
|
5
|
+
* string and leaves out the parameters it doesn't use, which changesets'
|
|
6
|
+
* type still accepts.
|
|
7
|
+
*/
|
|
8
|
+
export type ChangelogFunctions = {
|
|
9
|
+
/** Build the lines for a release that only bumps its dependencies. */
|
|
10
|
+
getDependencyReleaseLine: typeof getDependencyReleaseLine;
|
|
11
|
+
/** Build one changeset's line. */
|
|
12
|
+
getReleaseLine: typeof getReleaseLine;
|
|
13
|
+
};
|
|
14
|
+
/** A changeset as changesets passes it to the formatter. */
|
|
15
|
+
export type ChangesetWithCommit = {
|
|
16
|
+
/**
|
|
17
|
+
* The hash of the commit that added the changeset, when git found one.
|
|
18
|
+
*
|
|
19
|
+
* @example "b4fc42d0d5a0e1f2c3b4a5968778695a4b3c2d1e"
|
|
20
|
+
*/
|
|
21
|
+
commit?: string;
|
|
22
|
+
/**
|
|
23
|
+
* The changeset's markdown body, which may span several lines.
|
|
24
|
+
*
|
|
25
|
+
* @example "Add `getDependencyReleaseLine`, which lists bumped packages."
|
|
26
|
+
*/
|
|
27
|
+
summary: string;
|
|
28
|
+
};
|
|
29
|
+
/** What reading a commit's subject found. */
|
|
30
|
+
export type CommitLookup = {
|
|
31
|
+
/**
|
|
32
|
+
* git has no such commit, such as a hash from before a squash, or git
|
|
33
|
+
* failed for another reason, so the subject is unknown.
|
|
34
|
+
*
|
|
35
|
+
* @example "missing"
|
|
36
|
+
*/
|
|
37
|
+
status: "missing" | "unreadable";
|
|
38
|
+
} | {
|
|
39
|
+
/** git read the commit. @example "found" */
|
|
40
|
+
status: "found";
|
|
41
|
+
/**
|
|
42
|
+
* The commit's subject line.
|
|
43
|
+
*
|
|
44
|
+
* @example "feat(eslint-config): ✨ add the react preset (#12)"
|
|
45
|
+
*/
|
|
46
|
+
subject: string;
|
|
47
|
+
};
|
|
48
|
+
/** The parts of a `<type>(<scope>)!: <gitmoji> <subject>` commit subject. */
|
|
49
|
+
export type ConventionalSubject = {
|
|
50
|
+
/** The gitmoji after the colon, when there is one. @example "✨" */
|
|
51
|
+
gitmoji?: string;
|
|
52
|
+
/** Whether the type carries the breaking-change `!`. @example true */
|
|
53
|
+
isBreaking: boolean;
|
|
54
|
+
/** The scope in parentheses, when there is one. @example "eslint-config" */
|
|
55
|
+
scope?: string;
|
|
56
|
+
};
|
|
57
|
+
/** A dependency bumped in the same release, as changesets passes it. */
|
|
58
|
+
export type DependencyUpdate = {
|
|
59
|
+
/**
|
|
60
|
+
* The dependency's package name.
|
|
61
|
+
*
|
|
62
|
+
* @example `"@jjloneman/kit-eslint-plugin"`
|
|
63
|
+
*/
|
|
64
|
+
name: string;
|
|
65
|
+
/**
|
|
66
|
+
* The version it was bumped to; absent for a `none` release, which
|
|
67
|
+
* changesets can pass though nothing was bumped.
|
|
68
|
+
*
|
|
69
|
+
* @example "1.2.0"
|
|
70
|
+
*/
|
|
71
|
+
newVersion?: string | undefined;
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* Build one changelog line from a changeset and what reading its commit
|
|
75
|
+
* found.
|
|
76
|
+
*
|
|
77
|
+
* - A commit git has no record of, such as a hash from before a squash, gives
|
|
78
|
+
* the bare summary, without a hash to a commit that isn't on `main`.
|
|
79
|
+
*
|
|
80
|
+
* - A commit git couldn't read for another reason keeps its hash, with no
|
|
81
|
+
* gitmoji or scope, since the hash came from the repo's own history.
|
|
82
|
+
*
|
|
83
|
+
* - A summary's later lines are indented under the bullet, as changesets'
|
|
84
|
+
* default formatter does.
|
|
85
|
+
*
|
|
86
|
+
* @returns the markdown list item.
|
|
87
|
+
*/
|
|
88
|
+
export declare function formatReleaseLine({ changeset, lookup }: {
|
|
89
|
+
/** The changeset the line describes. */
|
|
90
|
+
changeset: ChangesetWithCommit;
|
|
91
|
+
/**
|
|
92
|
+
* What reading the commit that added it found, or `undefined` when it has
|
|
93
|
+
* no commit.
|
|
94
|
+
*/
|
|
95
|
+
lookup: CommitLookup | undefined;
|
|
96
|
+
}): string;
|
|
97
|
+
/**
|
|
98
|
+
* The changesets entry point for a release's dependency bumps.
|
|
99
|
+
*
|
|
100
|
+
* - Positional parameters, since changesets calls it as
|
|
101
|
+
* `(changesets, dependenciesUpdated, options)`; the unused `options` is
|
|
102
|
+
* left out.
|
|
103
|
+
*
|
|
104
|
+
* - One `Updated dependencies` line with each changeset's short hash, then
|
|
105
|
+
* one sub-item per bumped dependency, the shape changesets' default
|
|
106
|
+
* formatter uses, without repeating the line per changeset.
|
|
107
|
+
*
|
|
108
|
+
* - A dependency with no new version, a `none` release, is left out rather
|
|
109
|
+
* than printed as `name@undefined`.
|
|
110
|
+
*
|
|
111
|
+
* @param changesets - The changesets in the release, each with the hash of
|
|
112
|
+
* the commit that added it.
|
|
113
|
+
* @param dependenciesUpdated - The dependencies bumped in the same release,
|
|
114
|
+
* including any `none` release with no new version.
|
|
115
|
+
* @returns the lines, or an empty string, which changesets leaves out, when
|
|
116
|
+
* no dependency was bumped.
|
|
117
|
+
*/
|
|
118
|
+
export declare function getDependencyReleaseLine(changesets: readonly ChangesetWithCommit[], dependenciesUpdated: readonly DependencyUpdate[]): string;
|
|
119
|
+
/**
|
|
120
|
+
* The changesets entry point for one changeset's line.
|
|
121
|
+
*
|
|
122
|
+
* - Reads git from the process's directory, since changesets passes the
|
|
123
|
+
* formatter no directory and runs from the repo root; once installed, this
|
|
124
|
+
* file lives in `node_modules`, not the repo.
|
|
125
|
+
*
|
|
126
|
+
* - changesets also passes the bump type and options, which the line doesn't
|
|
127
|
+
* use, so they're left out.
|
|
128
|
+
*
|
|
129
|
+
* @returns the markdown list item.
|
|
130
|
+
*/
|
|
131
|
+
export declare function getReleaseLine(changeset: ChangesetWithCommit): string;
|
|
132
|
+
/**
|
|
133
|
+
* Read the gitmoji, scope, and breaking-change marker from a
|
|
134
|
+
* conventional-commit subject.
|
|
135
|
+
*
|
|
136
|
+
* @returns the parts found; none are set when the subject doesn't parse.
|
|
137
|
+
*/
|
|
138
|
+
export declare function parseConventionalSubject(subject: string): ConventionalSubject;
|
|
139
|
+
/**
|
|
140
|
+
* Read a commit's subject line, once per repo and hash.
|
|
141
|
+
*
|
|
142
|
+
* @returns the subject, or whether git had no such commit or couldn't read
|
|
143
|
+
* it.
|
|
144
|
+
*/
|
|
145
|
+
export declare function readCommitSubject({ cwd, hash }: {
|
|
146
|
+
/** The repo to read from. @example "/Users/me/jj-kit" */
|
|
147
|
+
cwd: string;
|
|
148
|
+
/**
|
|
149
|
+
* The commit to read; never read as an option, even when it starts with
|
|
150
|
+
* `-`.
|
|
151
|
+
*
|
|
152
|
+
* @example "b4fc42d"
|
|
153
|
+
*/
|
|
154
|
+
hash: string;
|
|
155
|
+
}): CommitLookup;
|
|
156
|
+
/** The formatter, in the shape changesets loads from `changelog`. */
|
|
157
|
+
declare const CHANGELOG_FUNCTIONS: ChangelogFunctions;
|
|
158
|
+
export { CHANGELOG_FUNCTIONS as default };
|
|
159
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/index.ts"],"mappings":";;;;;;;YAqBY;;EAEV,iCAAiC;;EAGjC,uBAAuB;;;YAIb;;;;;;EAMV;;;;;;EAOA;;;YAIU;;;;;;;EAQN;;;EAIA;;;;;;EAOA;;;YAIM;;EAEV;;EAGA;;EAGA;;;YAIU;;;;;;EAMV;;;;;;;EAQA;;;;;;;;;;;;;;;;;wBA6Ec,oBACd,WACA;;EAGA,WAAW;;;;;EAMX,QAAQ;;;;;;;;;;;;;;;;;;;;;;;wBA+CM,yBACd,qBAAqB,uBACrB,8BAA8B;;;;;;;;;;;;;wBAsChB,eAAe,WAAW;;;;;;;wBA8B1B,yBAAyB,kBAAkB;;;;;;;wBAgB3C,oBACd,KACA;;EAGA;;;;;;;EAQA;IACE;;cAkKE,qBAAqB"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
// src/index.ts
|
|
3
|
+
/**
|
|
4
|
+
* One pictograph, with an optional skin tone and variation selector.
|
|
5
|
+
*
|
|
6
|
+
* - Escapes rather than the literal characters, since the variation selector
|
|
7
|
+
* is invisible in an editor.
|
|
8
|
+
*/
|
|
9
|
+
const PICTOGRAPH_SOURCE = String.raw`\p{Extended_Pictographic}` + String.raw`\p{Emoji_Modifier}?\u{FE0F}?`;
|
|
10
|
+
/**
|
|
11
|
+
* One gitmoji: a pictograph, optionally joined to more of the same.
|
|
12
|
+
*
|
|
13
|
+
* - `🏗️` and `⬆️` match whole, and a plain first word never does.
|
|
14
|
+
*
|
|
15
|
+
* - An escape rather than the literal joiner, which is invisible in an
|
|
16
|
+
* editor.
|
|
17
|
+
*/
|
|
18
|
+
const GITMOJI_SOURCE = PICTOGRAPH_SOURCE + String.raw`(?:\u{200D}${PICTOGRAPH_SOURCE})*`;
|
|
19
|
+
/**
|
|
20
|
+
* The exit status `git rev-parse --verify --quiet` gives a revision that
|
|
21
|
+
* names no commit, as opposed to 128 for git failing outright.
|
|
22
|
+
*/
|
|
23
|
+
const NO_SUCH_COMMIT_STATUS = 1;
|
|
24
|
+
/**
|
|
25
|
+
* How long one git call may run before it's abandoned.
|
|
26
|
+
*
|
|
27
|
+
* - Lazy fetches and prompts are off, so only a stuck process reaches it;
|
|
28
|
+
* the line then keeps its hash, as for any other failure.
|
|
29
|
+
*/
|
|
30
|
+
const GIT_TIMEOUT_MS = 1e4;
|
|
31
|
+
/** Characters Markdown would read as formatting inside a bold scope. */
|
|
32
|
+
const MARKDOWN_SPECIAL_PATTERN = /[!#*<>[\\\]_`|~]/gu;
|
|
33
|
+
/**
|
|
34
|
+
* Lookups already made, keyed by repo and hash.
|
|
35
|
+
*
|
|
36
|
+
* - changesets asks for one line at a time, so the calls can't be batched;
|
|
37
|
+
* a commit that added several changesets is still read only once.
|
|
38
|
+
*/
|
|
39
|
+
const LOOKUP_CACHE = /* @__PURE__ */ new Map();
|
|
40
|
+
/** The length a hash is shortened to in a line, as changesets' own does. */
|
|
41
|
+
const SHORT_HASH_LENGTH = 7;
|
|
42
|
+
/**
|
|
43
|
+
* A conventional-commit subject: the type, an optional scope and `!`, then
|
|
44
|
+
* an optional gitmoji before the description.
|
|
45
|
+
*/
|
|
46
|
+
const SUBJECT_PATTERN = new RegExp(String.raw`^\w+(?:\((?<scope>[^()]+)\))?(?<breaking>!)?: ` + String.raw`(?:(?<gitmoji>${GITMOJI_SOURCE})\s+)?\S`, "u");
|
|
47
|
+
/**
|
|
48
|
+
* Build one changelog line from a changeset and what reading its commit
|
|
49
|
+
* found.
|
|
50
|
+
*
|
|
51
|
+
* - A commit git has no record of, such as a hash from before a squash, gives
|
|
52
|
+
* the bare summary, without a hash to a commit that isn't on `main`.
|
|
53
|
+
*
|
|
54
|
+
* - A commit git couldn't read for another reason keeps its hash, with no
|
|
55
|
+
* gitmoji or scope, since the hash came from the repo's own history.
|
|
56
|
+
*
|
|
57
|
+
* - A summary's later lines are indented under the bullet, as changesets'
|
|
58
|
+
* default formatter does.
|
|
59
|
+
*
|
|
60
|
+
* @returns the markdown list item.
|
|
61
|
+
*/
|
|
62
|
+
function formatReleaseLine({ changeset, lookup }) {
|
|
63
|
+
const [firstLine, ...laterLines] = changeset.summary.trim().split("\n").map((line) => line.trimEnd());
|
|
64
|
+
const { gitmoji, isBreaking, scope } = lookup?.status === "found" ? parseConventionalSubject(lookup.subject) : {};
|
|
65
|
+
return [`- ${[
|
|
66
|
+
gitmoji,
|
|
67
|
+
scope === void 0 ? void 0 : `**${escapeMarkdown(scope)}:**`,
|
|
68
|
+
isBreaking === true ? "**Breaking:**" : void 0,
|
|
69
|
+
firstLine,
|
|
70
|
+
changeset.commit === void 0 || lookup?.status === "missing" ? void 0 : `(${shortenHash(changeset.commit)})`
|
|
71
|
+
].filter((part) => part !== void 0 && part !== "").join(" ")}`, ...laterLines.map((line) => line === "" ? line : ` ${line}`)].join("\n");
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* The changesets entry point for a release's dependency bumps.
|
|
75
|
+
*
|
|
76
|
+
* - Positional parameters, since changesets calls it as
|
|
77
|
+
* `(changesets, dependenciesUpdated, options)`; the unused `options` is
|
|
78
|
+
* left out.
|
|
79
|
+
*
|
|
80
|
+
* - One `Updated dependencies` line with each changeset's short hash, then
|
|
81
|
+
* one sub-item per bumped dependency, the shape changesets' default
|
|
82
|
+
* formatter uses, without repeating the line per changeset.
|
|
83
|
+
*
|
|
84
|
+
* - A dependency with no new version, a `none` release, is left out rather
|
|
85
|
+
* than printed as `name@undefined`.
|
|
86
|
+
*
|
|
87
|
+
* @param changesets - The changesets in the release, each with the hash of
|
|
88
|
+
* the commit that added it.
|
|
89
|
+
* @param dependenciesUpdated - The dependencies bumped in the same release,
|
|
90
|
+
* including any `none` release with no new version.
|
|
91
|
+
* @returns the lines, or an empty string, which changesets leaves out, when
|
|
92
|
+
* no dependency was bumped.
|
|
93
|
+
*/
|
|
94
|
+
function getDependencyReleaseLine(changesets, dependenciesUpdated) {
|
|
95
|
+
const bumped = dependenciesUpdated.flatMap(({ name, newVersion }) => newVersion === void 0 ? [] : [` - ${name}@${newVersion}`]);
|
|
96
|
+
if (bumped.length === 0) return "";
|
|
97
|
+
const hashes = [...new Set(changesets.flatMap(({ commit }) => commit === void 0 ? [] : [shortenHash(commit)]))];
|
|
98
|
+
return [hashes.length === 0 ? "- Updated dependencies" : `- Updated dependencies [${hashes.join(", ")}]`, ...bumped].join("\n");
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* The changesets entry point for one changeset's line.
|
|
102
|
+
*
|
|
103
|
+
* - Reads git from the process's directory, since changesets passes the
|
|
104
|
+
* formatter no directory and runs from the repo root; once installed, this
|
|
105
|
+
* file lives in `node_modules`, not the repo.
|
|
106
|
+
*
|
|
107
|
+
* - changesets also passes the bump type and options, which the line doesn't
|
|
108
|
+
* use, so they're left out.
|
|
109
|
+
*
|
|
110
|
+
* @returns the markdown list item.
|
|
111
|
+
*/
|
|
112
|
+
function getReleaseLine(changeset) {
|
|
113
|
+
if (changeset.commit === void 0) return formatReleaseLine({
|
|
114
|
+
changeset,
|
|
115
|
+
lookup: void 0
|
|
116
|
+
});
|
|
117
|
+
return formatReleaseLine({
|
|
118
|
+
changeset,
|
|
119
|
+
lookup: readCommitSubject({
|
|
120
|
+
cwd: process.cwd(),
|
|
121
|
+
hash: changeset.commit
|
|
122
|
+
})
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Read the gitmoji, scope, and breaking-change marker from a
|
|
127
|
+
* conventional-commit subject.
|
|
128
|
+
*
|
|
129
|
+
* @returns the parts found; none are set when the subject doesn't parse.
|
|
130
|
+
*/
|
|
131
|
+
function parseConventionalSubject(subject) {
|
|
132
|
+
const groups = SUBJECT_PATTERN.exec(subject)?.groups;
|
|
133
|
+
return {
|
|
134
|
+
...groups?.gitmoji === void 0 ? {} : { gitmoji: groups.gitmoji },
|
|
135
|
+
isBreaking: groups?.breaking !== void 0,
|
|
136
|
+
...groups?.scope === void 0 ? {} : { scope: groups.scope }
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Read a commit's subject line, once per repo and hash.
|
|
141
|
+
*
|
|
142
|
+
* @returns the subject, or whether git had no such commit or couldn't read
|
|
143
|
+
* it.
|
|
144
|
+
*/
|
|
145
|
+
function readCommitSubject({ cwd, hash }) {
|
|
146
|
+
const key = `${cwd}\0${hash}`;
|
|
147
|
+
const cached = LOOKUP_CACHE.get(key);
|
|
148
|
+
if (cached !== void 0) return cached;
|
|
149
|
+
const lookup = readUncachedSubject({
|
|
150
|
+
cwd,
|
|
151
|
+
hash
|
|
152
|
+
});
|
|
153
|
+
LOOKUP_CACHE.set(key, lookup);
|
|
154
|
+
return lookup;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Say why a call failed, from whatever it threw.
|
|
158
|
+
*
|
|
159
|
+
* @returns the error's message, or the thrown value as a string.
|
|
160
|
+
*/
|
|
161
|
+
function describeError(error) {
|
|
162
|
+
return error instanceof Error ? error.message : String(error);
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Escape the characters Markdown would read as formatting.
|
|
166
|
+
*
|
|
167
|
+
* @returns the text with each one backslash-escaped.
|
|
168
|
+
*/
|
|
169
|
+
function escapeMarkdown(text) {
|
|
170
|
+
return text.replaceAll(MARKDOWN_SPECIAL_PATTERN, String.raw`\$&`);
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Whether git exited with the status meaning "no such commit".
|
|
174
|
+
*
|
|
175
|
+
* @returns `true` for that status alone.
|
|
176
|
+
*/
|
|
177
|
+
function isNoSuchCommit(error) {
|
|
178
|
+
return error instanceof Error && "status" in error && error.status === NO_SUCH_COMMIT_STATUS;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Ask git for a commit's subject line.
|
|
182
|
+
*
|
|
183
|
+
* - `rev-parse` first, whose exit status tells a missing commit apart from
|
|
184
|
+
* git failing, in any locale and without reading its messages.
|
|
185
|
+
*
|
|
186
|
+
* @returns the subject, or whether git had no such commit or couldn't read
|
|
187
|
+
* it.
|
|
188
|
+
*/
|
|
189
|
+
function readUncachedSubject({ cwd, hash }) {
|
|
190
|
+
let fullHash;
|
|
191
|
+
try {
|
|
192
|
+
fullHash = runGit({
|
|
193
|
+
args: [
|
|
194
|
+
"rev-parse",
|
|
195
|
+
"--verify",
|
|
196
|
+
"--quiet",
|
|
197
|
+
"--end-of-options",
|
|
198
|
+
`${hash}^{commit}`
|
|
199
|
+
],
|
|
200
|
+
cwd
|
|
201
|
+
}).trim();
|
|
202
|
+
} catch (error) {
|
|
203
|
+
return isNoSuchCommit(error) ? { status: "missing" } : reportUnreadable({
|
|
204
|
+
error,
|
|
205
|
+
hash
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
try {
|
|
209
|
+
return {
|
|
210
|
+
status: "found",
|
|
211
|
+
subject: runGit({
|
|
212
|
+
args: [
|
|
213
|
+
"log",
|
|
214
|
+
"-1",
|
|
215
|
+
"--format=%s",
|
|
216
|
+
"--no-show-signature",
|
|
217
|
+
fullHash,
|
|
218
|
+
"--"
|
|
219
|
+
],
|
|
220
|
+
cwd
|
|
221
|
+
}).trim()
|
|
222
|
+
};
|
|
223
|
+
} catch (error) {
|
|
224
|
+
return reportUnreadable({
|
|
225
|
+
error,
|
|
226
|
+
hash
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Warn that a commit couldn't be read.
|
|
232
|
+
*
|
|
233
|
+
* @returns the `unreadable` lookup.
|
|
234
|
+
*/
|
|
235
|
+
function reportUnreadable({ error, hash }) {
|
|
236
|
+
console.warn(`[kit-changesets-changelog] couldn't read commit ${hash}, so its line has no gitmoji or scope: ${describeError(error).trim()}`);
|
|
237
|
+
return { status: "unreadable" };
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Run git in a directory, with stdin ignored and its output captured.
|
|
241
|
+
*
|
|
242
|
+
* - Lazy fetches and credential prompts are off, so a partial clone missing
|
|
243
|
+
* a commit fails at once rather than waiting on the network or a prompt.
|
|
244
|
+
*
|
|
245
|
+
* @returns what git printed to stdout.
|
|
246
|
+
*/
|
|
247
|
+
function runGit({ args, cwd }) {
|
|
248
|
+
return execFileSync("git", args, {
|
|
249
|
+
cwd,
|
|
250
|
+
encoding: "utf8",
|
|
251
|
+
env: {
|
|
252
|
+
...process.env,
|
|
253
|
+
GIT_NO_LAZY_FETCH: "1",
|
|
254
|
+
GIT_TERMINAL_PROMPT: "0"
|
|
255
|
+
},
|
|
256
|
+
stdio: [
|
|
257
|
+
"ignore",
|
|
258
|
+
"pipe",
|
|
259
|
+
"pipe"
|
|
260
|
+
],
|
|
261
|
+
timeout: GIT_TIMEOUT_MS
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Shorten a commit hash for a changelog line.
|
|
266
|
+
*
|
|
267
|
+
* @returns its first seven characters.
|
|
268
|
+
*/
|
|
269
|
+
function shortenHash(hash) {
|
|
270
|
+
return hash.slice(0, SHORT_HASH_LENGTH);
|
|
271
|
+
}
|
|
272
|
+
/** The formatter, in the shape changesets loads from `changelog`. */
|
|
273
|
+
const CHANGELOG_FUNCTIONS = {
|
|
274
|
+
getDependencyReleaseLine,
|
|
275
|
+
getReleaseLine
|
|
276
|
+
};
|
|
277
|
+
export { CHANGELOG_FUNCTIONS as default, formatReleaseLine, getDependencyReleaseLine, getReleaseLine, parseConventionalSubject, readCommitSubject };
|
|
278
|
+
|
|
279
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["/*\n * A changesets changelog formatter that reuses each commit's gitmoji and\n * scope, named by `changelog` in a repo's `.changeset/config.json`.\n *\n * - Each line takes the gitmoji and scope from the subject of the commit that\n * added the changeset, which after a squash merge is the PR title:\n * `- ✨ **scope:** <summary> (<short hash>)`.\n *\n * - jj-kit loads this file itself under Node's type stripping, so it uses\n * only erasable syntax and `.ts` import specifiers; consumers load the\n * built `dist/`, since Node won't strip types under `node_modules`.\n */\nimport { execFileSync } from \"node:child_process\";\n\n/**\n * The two functions changesets calls.\n *\n * - Narrower than changesets' own `ChangelogFunctions`: each returns a plain\n * string and leaves out the parameters it doesn't use, which changesets'\n * type still accepts.\n */\nexport type ChangelogFunctions = {\n /** Build the lines for a release that only bumps its dependencies. */\n getDependencyReleaseLine: typeof getDependencyReleaseLine;\n\n /** Build one changeset's line. */\n getReleaseLine: typeof getReleaseLine;\n};\n\n/** A changeset as changesets passes it to the formatter. */\nexport type ChangesetWithCommit = {\n /**\n * The hash of the commit that added the changeset, when git found one.\n *\n * @example \"b4fc42d0d5a0e1f2c3b4a5968778695a4b3c2d1e\"\n */\n commit?: string;\n\n /**\n * The changeset's markdown body, which may span several lines.\n *\n * @example \"Add `getDependencyReleaseLine`, which lists bumped packages.\"\n */\n summary: string;\n};\n\n/** What reading a commit's subject found. */\nexport type CommitLookup =\n | {\n /**\n * git has no such commit, such as a hash from before a squash, or git\n * failed for another reason, so the subject is unknown.\n *\n * @example \"missing\"\n */\n status: \"missing\" | \"unreadable\";\n }\n | {\n /** git read the commit. @example \"found\" */\n status: \"found\";\n\n /**\n * The commit's subject line.\n *\n * @example \"feat(eslint-config): ✨ add the react preset (#12)\"\n */\n subject: string;\n };\n\n/** The parts of a `<type>(<scope>)!: <gitmoji> <subject>` commit subject. */\nexport type ConventionalSubject = {\n /** The gitmoji after the colon, when there is one. @example \"✨\" */\n gitmoji?: string;\n\n /** Whether the type carries the breaking-change `!`. @example true */\n isBreaking: boolean;\n\n /** The scope in parentheses, when there is one. @example \"eslint-config\" */\n scope?: string;\n};\n\n/** A dependency bumped in the same release, as changesets passes it. */\nexport type DependencyUpdate = {\n /**\n * The dependency's package name.\n *\n * @example `\"@jjloneman/kit-eslint-plugin\"`\n */\n name: string;\n\n /**\n * The version it was bumped to; absent for a `none` release, which\n * changesets can pass though nothing was bumped.\n *\n * @example \"1.2.0\"\n */\n newVersion?: string | undefined;\n};\n\n/**\n * One pictograph, with an optional skin tone and variation selector.\n *\n * - Escapes rather than the literal characters, since the variation selector\n * is invisible in an editor.\n */\nconst PICTOGRAPH_SOURCE =\n String.raw`\\p{Extended_Pictographic}` +\n String.raw`\\p{Emoji_Modifier}?\\u{FE0F}?`;\n\n/**\n * One gitmoji: a pictograph, optionally joined to more of the same.\n *\n * - `🏗️` and `⬆️` match whole, and a plain first word never does.\n *\n * - An escape rather than the literal joiner, which is invisible in an\n * editor.\n */\nconst GITMOJI_SOURCE =\n PICTOGRAPH_SOURCE + String.raw`(?:\\u{200D}${PICTOGRAPH_SOURCE})*`;\n\n/**\n * The exit status `git rev-parse --verify --quiet` gives a revision that\n * names no commit, as opposed to 128 for git failing outright.\n */\nconst NO_SUCH_COMMIT_STATUS = 1;\n\n/**\n * How long one git call may run before it's abandoned.\n *\n * - Lazy fetches and prompts are off, so only a stuck process reaches it;\n * the line then keeps its hash, as for any other failure.\n */\nconst GIT_TIMEOUT_MS = 10_000;\n\n/** Characters Markdown would read as formatting inside a bold scope. */\nconst MARKDOWN_SPECIAL_PATTERN = /[!#*<>[\\\\\\]_`|~]/gu;\n\n/**\n * Lookups already made, keyed by repo and hash.\n *\n * - changesets asks for one line at a time, so the calls can't be batched;\n * a commit that added several changesets is still read only once.\n */\nconst LOOKUP_CACHE = new Map<string, CommitLookup>();\n\n/** The length a hash is shortened to in a line, as changesets' own does. */\nconst SHORT_HASH_LENGTH = 7;\n\n/**\n * A conventional-commit subject: the type, an optional scope and `!`, then\n * an optional gitmoji before the description.\n */\nconst SUBJECT_PATTERN = new RegExp(\n String.raw`^\\w+(?:\\((?<scope>[^()]+)\\))?(?<breaking>!)?: ` +\n String.raw`(?:(?<gitmoji>${GITMOJI_SOURCE})\\s+)?\\S`,\n \"u\"\n);\n\n/**\n * Build one changelog line from a changeset and what reading its commit\n * found.\n *\n * - A commit git has no record of, such as a hash from before a squash, gives\n * the bare summary, without a hash to a commit that isn't on `main`.\n *\n * - A commit git couldn't read for another reason keeps its hash, with no\n * gitmoji or scope, since the hash came from the repo's own history.\n *\n * - A summary's later lines are indented under the bullet, as changesets'\n * default formatter does.\n *\n * @returns the markdown list item.\n */\nexport function formatReleaseLine({\n changeset,\n lookup,\n}: {\n /** The changeset the line describes. */\n changeset: ChangesetWithCommit;\n\n /**\n * What reading the commit that added it found, or `undefined` when it has\n * no commit.\n */\n lookup: CommitLookup | undefined;\n}): string {\n const [firstLine, ...laterLines] = changeset.summary\n .trim()\n .split(\"\\n\")\n .map((line) => line.trimEnd());\n\n const { gitmoji, isBreaking, scope }: Partial<ConventionalSubject> =\n lookup?.status === \"found\" ? parseConventionalSubject(lookup.subject) : {};\n\n const parts = [\n gitmoji,\n scope === undefined ? undefined : `**${escapeMarkdown(scope)}:**`,\n isBreaking === true ? \"**Breaking:**\" : undefined,\n firstLine,\n changeset.commit === undefined || lookup?.status === \"missing\"\n ? undefined\n : `(${shortenHash(changeset.commit)})`,\n ].filter((part) => part !== undefined && part !== \"\");\n\n return [\n `- ${parts.join(\" \")}`,\n ...laterLines.map((line) => (line === \"\" ? line : ` ${line}`)),\n ].join(\"\\n\");\n}\n\n/**\n * The changesets entry point for a release's dependency bumps.\n *\n * - Positional parameters, since changesets calls it as\n * `(changesets, dependenciesUpdated, options)`; the unused `options` is\n * left out.\n *\n * - One `Updated dependencies` line with each changeset's short hash, then\n * one sub-item per bumped dependency, the shape changesets' default\n * formatter uses, without repeating the line per changeset.\n *\n * - A dependency with no new version, a `none` release, is left out rather\n * than printed as `name@undefined`.\n *\n * @param changesets - The changesets in the release, each with the hash of\n * the commit that added it.\n * @param dependenciesUpdated - The dependencies bumped in the same release,\n * including any `none` release with no new version.\n * @returns the lines, or an empty string, which changesets leaves out, when\n * no dependency was bumped.\n */\nexport function getDependencyReleaseLine(\n changesets: readonly ChangesetWithCommit[],\n dependenciesUpdated: readonly DependencyUpdate[]\n): string {\n const bumped = dependenciesUpdated.flatMap(({ name, newVersion }) =>\n newVersion === undefined ? [] : [` - ${name}@${newVersion}`]\n );\n\n if (bumped.length === 0) {\n return \"\";\n }\n\n const hashes = [\n ...new Set(\n changesets.flatMap(({ commit }) =>\n commit === undefined ? [] : [shortenHash(commit)]\n )\n ),\n ];\n\n return [\n hashes.length === 0\n ? \"- Updated dependencies\"\n : `- Updated dependencies [${hashes.join(\", \")}]`,\n ...bumped,\n ].join(\"\\n\");\n}\n\n/**\n * The changesets entry point for one changeset's line.\n *\n * - Reads git from the process's directory, since changesets passes the\n * formatter no directory and runs from the repo root; once installed, this\n * file lives in `node_modules`, not the repo.\n *\n * - changesets also passes the bump type and options, which the line doesn't\n * use, so they're left out.\n *\n * @returns the markdown list item.\n */\nexport function getReleaseLine(changeset: ChangesetWithCommit): string {\n if (changeset.commit === undefined) {\n return formatReleaseLine({ changeset, lookup: undefined });\n }\n\n /*\n * The process's directory, not the `cwd` changesets found the commits\n * from, which it doesn't pass on.\n *\n * - The two match whenever changesets runs from its CLI or its GitHub\n * Action, both of which run from the repo root.\n *\n * - They can differ only through changesets' programmatic API called from\n * another directory; each commit then reads as missing, and its line\n * falls back to the bare summary.\n */\n const cwd = process.cwd();\n\n return formatReleaseLine({\n changeset,\n lookup: readCommitSubject({ cwd, hash: changeset.commit }),\n });\n}\n\n/**\n * Read the gitmoji, scope, and breaking-change marker from a\n * conventional-commit subject.\n *\n * @returns the parts found; none are set when the subject doesn't parse.\n */\nexport function parseConventionalSubject(subject: string): ConventionalSubject {\n const groups = SUBJECT_PATTERN.exec(subject)?.groups;\n\n return {\n ...(groups?.gitmoji === undefined ? {} : { gitmoji: groups.gitmoji }),\n isBreaking: groups?.breaking !== undefined,\n ...(groups?.scope === undefined ? {} : { scope: groups.scope }),\n };\n}\n\n/**\n * Read a commit's subject line, once per repo and hash.\n *\n * @returns the subject, or whether git had no such commit or couldn't read\n * it.\n */\nexport function readCommitSubject({\n cwd,\n hash,\n}: {\n /** The repo to read from. @example \"/Users/me/jj-kit\" */\n cwd: string;\n\n /**\n * The commit to read; never read as an option, even when it starts with\n * `-`.\n *\n * @example \"b4fc42d\"\n */\n hash: string;\n}): CommitLookup {\n const key = `${cwd}\\0${hash}`;\n const cached = LOOKUP_CACHE.get(key);\n\n if (cached !== undefined) {\n return cached;\n }\n\n const lookup = readUncachedSubject({ cwd, hash });\n LOOKUP_CACHE.set(key, lookup);\n\n return lookup;\n}\n\n/**\n * Say why a call failed, from whatever it threw.\n *\n * @returns the error's message, or the thrown value as a string.\n */\nfunction describeError(error: unknown): string {\n return error instanceof Error ? error.message : String(error);\n}\n\n/**\n * Escape the characters Markdown would read as formatting.\n *\n * @returns the text with each one backslash-escaped.\n */\nfunction escapeMarkdown(text: string): string {\n return text.replaceAll(MARKDOWN_SPECIAL_PATTERN, String.raw`\\$&`);\n}\n\n/**\n * Whether git exited with the status meaning \"no such commit\".\n *\n * @returns `true` for that status alone.\n */\nfunction isNoSuchCommit(error: unknown): boolean {\n return (\n error instanceof Error &&\n \"status\" in error &&\n error.status === NO_SUCH_COMMIT_STATUS\n );\n}\n\n/**\n * Ask git for a commit's subject line.\n *\n * - `rev-parse` first, whose exit status tells a missing commit apart from\n * git failing, in any locale and without reading its messages.\n *\n * @returns the subject, or whether git had no such commit or couldn't read\n * it.\n */\nfunction readUncachedSubject({\n cwd,\n hash,\n}: {\n /** The repo to read from. */\n cwd: string;\n\n /** The commit to read. */\n hash: string;\n}): CommitLookup {\n let fullHash: string;\n\n try {\n fullHash = runGit({\n args: [\n \"rev-parse\",\n \"--verify\",\n \"--quiet\",\n \"--end-of-options\",\n `${hash}^{commit}`,\n ],\n cwd,\n }).trim();\n } catch (error: unknown) {\n // A hash from before a squash is expected; anything else is worth a look.\n return isNoSuchCommit(error)\n ? { status: \"missing\" }\n : reportUnreadable({ error, hash });\n }\n\n try {\n // `--no-show-signature`, or `log.showSignature` would print gpg's check\n // ahead of the subject.\n const subject = runGit({\n args: [\"log\", \"-1\", \"--format=%s\", \"--no-show-signature\", fullHash, \"--\"],\n cwd,\n }).trim();\n\n return { status: \"found\", subject };\n } catch (error: unknown) {\n return reportUnreadable({ error, hash });\n }\n}\n\n/**\n * Warn that a commit couldn't be read.\n *\n * @returns the `unreadable` lookup.\n */\nfunction reportUnreadable({\n error,\n hash,\n}: {\n /** What git threw; its message carries git's stderr. */\n error: unknown;\n\n /** The commit that couldn't be read. */\n hash: string;\n}): CommitLookup {\n console.warn(\n `[kit-changesets-changelog] couldn't read commit ${hash}, so its ` +\n `line has no gitmoji or scope: ${describeError(error).trim()}`\n );\n\n return { status: \"unreadable\" };\n}\n\n/**\n * Run git in a directory, with stdin ignored and its output captured.\n *\n * - Lazy fetches and credential prompts are off, so a partial clone missing\n * a commit fails at once rather than waiting on the network or a prompt.\n *\n * @returns what git printed to stdout.\n */\nfunction runGit({\n args,\n cwd,\n}: {\n /**\n * The arguments after `git`.\n *\n * @example `[\"log\", \"-1\", \"--format=%s\", \"HEAD\"]`\n */\n args: readonly string[];\n\n /** The directory to run it in. */\n cwd: string;\n}): string {\n return execFileSync(\"git\", args, {\n cwd,\n encoding: \"utf8\",\n env: { ...process.env, GIT_NO_LAZY_FETCH: \"1\", GIT_TERMINAL_PROMPT: \"0\" },\n stdio: [\"ignore\", \"pipe\", \"pipe\"],\n timeout: GIT_TIMEOUT_MS,\n });\n}\n\n/**\n * Shorten a commit hash for a changelog line.\n *\n * @returns its first seven characters.\n */\nfunction shortenHash(hash: string): string {\n return hash.slice(0, SHORT_HASH_LENGTH);\n}\n\n/** The formatter, in the shape changesets loads from `changelog`. */\nconst CHANGELOG_FUNCTIONS: ChangelogFunctions = {\n getDependencyReleaseLine,\n getReleaseLine,\n};\n\nexport default CHANGELOG_FUNCTIONS;\n"],"mappings":"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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,62 @@
|
|
|
1
1
|
{
|
|
2
|
+
"author": {
|
|
3
|
+
"name": "JJ Loneman",
|
|
4
|
+
"url": "https://github.com/jjloneman"
|
|
5
|
+
},
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/jjloneman/jj-kit/issues"
|
|
8
|
+
},
|
|
9
|
+
"description": "🦋 A Changesets changelog formatter that prefixes each line with the gitmoji and scope of the commit that added it.",
|
|
10
|
+
"devDependencies": {
|
|
11
|
+
"@arethetypeswrong/cli": "0.18.5",
|
|
12
|
+
"@changesets/types": "7.0.0",
|
|
13
|
+
"@types/node": "26.6.6",
|
|
14
|
+
"publint": "0.3.25",
|
|
15
|
+
"tsdown": "0.23.7",
|
|
16
|
+
"typescript": "6.0.3",
|
|
17
|
+
"vitest": "5.1.0"
|
|
18
|
+
},
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=26"
|
|
21
|
+
},
|
|
22
|
+
"exports": {
|
|
23
|
+
".": "./dist/index.js",
|
|
24
|
+
"./package.json": "./package.json"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"CHANGELOG.md",
|
|
28
|
+
"dist",
|
|
29
|
+
"src",
|
|
30
|
+
"!src/**/*.test.ts"
|
|
31
|
+
],
|
|
32
|
+
"homepage": "https://github.com/jjloneman/jj-kit/tree/main/packages/changesets-changelog#readme",
|
|
33
|
+
"keywords": [
|
|
34
|
+
"changelog",
|
|
35
|
+
"changesets",
|
|
36
|
+
"conventional-commits",
|
|
37
|
+
"gitmoji"
|
|
38
|
+
],
|
|
39
|
+
"license": "MIT",
|
|
2
40
|
"name": "@jjloneman/kit-changesets-changelog",
|
|
3
|
-
"
|
|
4
|
-
|
|
5
|
-
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"@changesets/cli": "^3.0.0"
|
|
43
|
+
},
|
|
44
|
+
"publishConfig": {
|
|
45
|
+
"access": "public",
|
|
46
|
+
"provenance": true
|
|
47
|
+
},
|
|
48
|
+
"repository": {
|
|
49
|
+
"directory": "packages/changesets-changelog",
|
|
50
|
+
"type": "git",
|
|
51
|
+
"url": "git+https://github.com/jjloneman/jj-kit.git"
|
|
52
|
+
},
|
|
53
|
+
"sideEffects": false,
|
|
54
|
+
"type": "module",
|
|
55
|
+
"types": "./dist/index.d.ts",
|
|
56
|
+
"version": "0.0.1-stage",
|
|
57
|
+
"scripts": {
|
|
58
|
+
"build": "tsdown",
|
|
59
|
+
"lint:package": "pnpm build && publint && attw --pack --profile esm-only",
|
|
60
|
+
"typecheck": "tsc"
|
|
61
|
+
}
|
|
6
62
|
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,499 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* A changesets changelog formatter that reuses each commit's gitmoji and
|
|
3
|
+
* scope, named by `changelog` in a repo's `.changeset/config.json`.
|
|
4
|
+
*
|
|
5
|
+
* - Each line takes the gitmoji and scope from the subject of the commit that
|
|
6
|
+
* added the changeset, which after a squash merge is the PR title:
|
|
7
|
+
* `- ✨ **scope:** <summary> (<short hash>)`.
|
|
8
|
+
*
|
|
9
|
+
* - jj-kit loads this file itself under Node's type stripping, so it uses
|
|
10
|
+
* only erasable syntax and `.ts` import specifiers; consumers load the
|
|
11
|
+
* built `dist/`, since Node won't strip types under `node_modules`.
|
|
12
|
+
*/
|
|
13
|
+
import { execFileSync } from "node:child_process";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The two functions changesets calls.
|
|
17
|
+
*
|
|
18
|
+
* - Narrower than changesets' own `ChangelogFunctions`: each returns a plain
|
|
19
|
+
* string and leaves out the parameters it doesn't use, which changesets'
|
|
20
|
+
* type still accepts.
|
|
21
|
+
*/
|
|
22
|
+
export type ChangelogFunctions = {
|
|
23
|
+
/** Build the lines for a release that only bumps its dependencies. */
|
|
24
|
+
getDependencyReleaseLine: typeof getDependencyReleaseLine;
|
|
25
|
+
|
|
26
|
+
/** Build one changeset's line. */
|
|
27
|
+
getReleaseLine: typeof getReleaseLine;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/** A changeset as changesets passes it to the formatter. */
|
|
31
|
+
export type ChangesetWithCommit = {
|
|
32
|
+
/**
|
|
33
|
+
* The hash of the commit that added the changeset, when git found one.
|
|
34
|
+
*
|
|
35
|
+
* @example "b4fc42d0d5a0e1f2c3b4a5968778695a4b3c2d1e"
|
|
36
|
+
*/
|
|
37
|
+
commit?: string;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The changeset's markdown body, which may span several lines.
|
|
41
|
+
*
|
|
42
|
+
* @example "Add `getDependencyReleaseLine`, which lists bumped packages."
|
|
43
|
+
*/
|
|
44
|
+
summary: string;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/** What reading a commit's subject found. */
|
|
48
|
+
export type CommitLookup =
|
|
49
|
+
| {
|
|
50
|
+
/**
|
|
51
|
+
* git has no such commit, such as a hash from before a squash, or git
|
|
52
|
+
* failed for another reason, so the subject is unknown.
|
|
53
|
+
*
|
|
54
|
+
* @example "missing"
|
|
55
|
+
*/
|
|
56
|
+
status: "missing" | "unreadable";
|
|
57
|
+
}
|
|
58
|
+
| {
|
|
59
|
+
/** git read the commit. @example "found" */
|
|
60
|
+
status: "found";
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The commit's subject line.
|
|
64
|
+
*
|
|
65
|
+
* @example "feat(eslint-config): ✨ add the react preset (#12)"
|
|
66
|
+
*/
|
|
67
|
+
subject: string;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
/** The parts of a `<type>(<scope>)!: <gitmoji> <subject>` commit subject. */
|
|
71
|
+
export type ConventionalSubject = {
|
|
72
|
+
/** The gitmoji after the colon, when there is one. @example "✨" */
|
|
73
|
+
gitmoji?: string;
|
|
74
|
+
|
|
75
|
+
/** Whether the type carries the breaking-change `!`. @example true */
|
|
76
|
+
isBreaking: boolean;
|
|
77
|
+
|
|
78
|
+
/** The scope in parentheses, when there is one. @example "eslint-config" */
|
|
79
|
+
scope?: string;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
/** A dependency bumped in the same release, as changesets passes it. */
|
|
83
|
+
export type DependencyUpdate = {
|
|
84
|
+
/**
|
|
85
|
+
* The dependency's package name.
|
|
86
|
+
*
|
|
87
|
+
* @example `"@jjloneman/kit-eslint-plugin"`
|
|
88
|
+
*/
|
|
89
|
+
name: string;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* The version it was bumped to; absent for a `none` release, which
|
|
93
|
+
* changesets can pass though nothing was bumped.
|
|
94
|
+
*
|
|
95
|
+
* @example "1.2.0"
|
|
96
|
+
*/
|
|
97
|
+
newVersion?: string | undefined;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* One pictograph, with an optional skin tone and variation selector.
|
|
102
|
+
*
|
|
103
|
+
* - Escapes rather than the literal characters, since the variation selector
|
|
104
|
+
* is invisible in an editor.
|
|
105
|
+
*/
|
|
106
|
+
const PICTOGRAPH_SOURCE =
|
|
107
|
+
String.raw`\p{Extended_Pictographic}` +
|
|
108
|
+
String.raw`\p{Emoji_Modifier}?\u{FE0F}?`;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* One gitmoji: a pictograph, optionally joined to more of the same.
|
|
112
|
+
*
|
|
113
|
+
* - `🏗️` and `⬆️` match whole, and a plain first word never does.
|
|
114
|
+
*
|
|
115
|
+
* - An escape rather than the literal joiner, which is invisible in an
|
|
116
|
+
* editor.
|
|
117
|
+
*/
|
|
118
|
+
const GITMOJI_SOURCE =
|
|
119
|
+
PICTOGRAPH_SOURCE + String.raw`(?:\u{200D}${PICTOGRAPH_SOURCE})*`;
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* The exit status `git rev-parse --verify --quiet` gives a revision that
|
|
123
|
+
* names no commit, as opposed to 128 for git failing outright.
|
|
124
|
+
*/
|
|
125
|
+
const NO_SUCH_COMMIT_STATUS = 1;
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* How long one git call may run before it's abandoned.
|
|
129
|
+
*
|
|
130
|
+
* - Lazy fetches and prompts are off, so only a stuck process reaches it;
|
|
131
|
+
* the line then keeps its hash, as for any other failure.
|
|
132
|
+
*/
|
|
133
|
+
const GIT_TIMEOUT_MS = 10_000;
|
|
134
|
+
|
|
135
|
+
/** Characters Markdown would read as formatting inside a bold scope. */
|
|
136
|
+
const MARKDOWN_SPECIAL_PATTERN = /[!#*<>[\\\]_`|~]/gu;
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Lookups already made, keyed by repo and hash.
|
|
140
|
+
*
|
|
141
|
+
* - changesets asks for one line at a time, so the calls can't be batched;
|
|
142
|
+
* a commit that added several changesets is still read only once.
|
|
143
|
+
*/
|
|
144
|
+
const LOOKUP_CACHE = new Map<string, CommitLookup>();
|
|
145
|
+
|
|
146
|
+
/** The length a hash is shortened to in a line, as changesets' own does. */
|
|
147
|
+
const SHORT_HASH_LENGTH = 7;
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* A conventional-commit subject: the type, an optional scope and `!`, then
|
|
151
|
+
* an optional gitmoji before the description.
|
|
152
|
+
*/
|
|
153
|
+
const SUBJECT_PATTERN = new RegExp(
|
|
154
|
+
String.raw`^\w+(?:\((?<scope>[^()]+)\))?(?<breaking>!)?: ` +
|
|
155
|
+
String.raw`(?:(?<gitmoji>${GITMOJI_SOURCE})\s+)?\S`,
|
|
156
|
+
"u"
|
|
157
|
+
);
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Build one changelog line from a changeset and what reading its commit
|
|
161
|
+
* found.
|
|
162
|
+
*
|
|
163
|
+
* - A commit git has no record of, such as a hash from before a squash, gives
|
|
164
|
+
* the bare summary, without a hash to a commit that isn't on `main`.
|
|
165
|
+
*
|
|
166
|
+
* - A commit git couldn't read for another reason keeps its hash, with no
|
|
167
|
+
* gitmoji or scope, since the hash came from the repo's own history.
|
|
168
|
+
*
|
|
169
|
+
* - A summary's later lines are indented under the bullet, as changesets'
|
|
170
|
+
* default formatter does.
|
|
171
|
+
*
|
|
172
|
+
* @returns the markdown list item.
|
|
173
|
+
*/
|
|
174
|
+
export function formatReleaseLine({
|
|
175
|
+
changeset,
|
|
176
|
+
lookup,
|
|
177
|
+
}: {
|
|
178
|
+
/** The changeset the line describes. */
|
|
179
|
+
changeset: ChangesetWithCommit;
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* What reading the commit that added it found, or `undefined` when it has
|
|
183
|
+
* no commit.
|
|
184
|
+
*/
|
|
185
|
+
lookup: CommitLookup | undefined;
|
|
186
|
+
}): string {
|
|
187
|
+
const [firstLine, ...laterLines] = changeset.summary
|
|
188
|
+
.trim()
|
|
189
|
+
.split("\n")
|
|
190
|
+
.map((line) => line.trimEnd());
|
|
191
|
+
|
|
192
|
+
const { gitmoji, isBreaking, scope }: Partial<ConventionalSubject> =
|
|
193
|
+
lookup?.status === "found" ? parseConventionalSubject(lookup.subject) : {};
|
|
194
|
+
|
|
195
|
+
const parts = [
|
|
196
|
+
gitmoji,
|
|
197
|
+
scope === undefined ? undefined : `**${escapeMarkdown(scope)}:**`,
|
|
198
|
+
isBreaking === true ? "**Breaking:**" : undefined,
|
|
199
|
+
firstLine,
|
|
200
|
+
changeset.commit === undefined || lookup?.status === "missing"
|
|
201
|
+
? undefined
|
|
202
|
+
: `(${shortenHash(changeset.commit)})`,
|
|
203
|
+
].filter((part) => part !== undefined && part !== "");
|
|
204
|
+
|
|
205
|
+
return [
|
|
206
|
+
`- ${parts.join(" ")}`,
|
|
207
|
+
...laterLines.map((line) => (line === "" ? line : ` ${line}`)),
|
|
208
|
+
].join("\n");
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* The changesets entry point for a release's dependency bumps.
|
|
213
|
+
*
|
|
214
|
+
* - Positional parameters, since changesets calls it as
|
|
215
|
+
* `(changesets, dependenciesUpdated, options)`; the unused `options` is
|
|
216
|
+
* left out.
|
|
217
|
+
*
|
|
218
|
+
* - One `Updated dependencies` line with each changeset's short hash, then
|
|
219
|
+
* one sub-item per bumped dependency, the shape changesets' default
|
|
220
|
+
* formatter uses, without repeating the line per changeset.
|
|
221
|
+
*
|
|
222
|
+
* - A dependency with no new version, a `none` release, is left out rather
|
|
223
|
+
* than printed as `name@undefined`.
|
|
224
|
+
*
|
|
225
|
+
* @param changesets - The changesets in the release, each with the hash of
|
|
226
|
+
* the commit that added it.
|
|
227
|
+
* @param dependenciesUpdated - The dependencies bumped in the same release,
|
|
228
|
+
* including any `none` release with no new version.
|
|
229
|
+
* @returns the lines, or an empty string, which changesets leaves out, when
|
|
230
|
+
* no dependency was bumped.
|
|
231
|
+
*/
|
|
232
|
+
export function getDependencyReleaseLine(
|
|
233
|
+
changesets: readonly ChangesetWithCommit[],
|
|
234
|
+
dependenciesUpdated: readonly DependencyUpdate[]
|
|
235
|
+
): string {
|
|
236
|
+
const bumped = dependenciesUpdated.flatMap(({ name, newVersion }) =>
|
|
237
|
+
newVersion === undefined ? [] : [` - ${name}@${newVersion}`]
|
|
238
|
+
);
|
|
239
|
+
|
|
240
|
+
if (bumped.length === 0) {
|
|
241
|
+
return "";
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const hashes = [
|
|
245
|
+
...new Set(
|
|
246
|
+
changesets.flatMap(({ commit }) =>
|
|
247
|
+
commit === undefined ? [] : [shortenHash(commit)]
|
|
248
|
+
)
|
|
249
|
+
),
|
|
250
|
+
];
|
|
251
|
+
|
|
252
|
+
return [
|
|
253
|
+
hashes.length === 0
|
|
254
|
+
? "- Updated dependencies"
|
|
255
|
+
: `- Updated dependencies [${hashes.join(", ")}]`,
|
|
256
|
+
...bumped,
|
|
257
|
+
].join("\n");
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* The changesets entry point for one changeset's line.
|
|
262
|
+
*
|
|
263
|
+
* - Reads git from the process's directory, since changesets passes the
|
|
264
|
+
* formatter no directory and runs from the repo root; once installed, this
|
|
265
|
+
* file lives in `node_modules`, not the repo.
|
|
266
|
+
*
|
|
267
|
+
* - changesets also passes the bump type and options, which the line doesn't
|
|
268
|
+
* use, so they're left out.
|
|
269
|
+
*
|
|
270
|
+
* @returns the markdown list item.
|
|
271
|
+
*/
|
|
272
|
+
export function getReleaseLine(changeset: ChangesetWithCommit): string {
|
|
273
|
+
if (changeset.commit === undefined) {
|
|
274
|
+
return formatReleaseLine({ changeset, lookup: undefined });
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/*
|
|
278
|
+
* The process's directory, not the `cwd` changesets found the commits
|
|
279
|
+
* from, which it doesn't pass on.
|
|
280
|
+
*
|
|
281
|
+
* - The two match whenever changesets runs from its CLI or its GitHub
|
|
282
|
+
* Action, both of which run from the repo root.
|
|
283
|
+
*
|
|
284
|
+
* - They can differ only through changesets' programmatic API called from
|
|
285
|
+
* another directory; each commit then reads as missing, and its line
|
|
286
|
+
* falls back to the bare summary.
|
|
287
|
+
*/
|
|
288
|
+
const cwd = process.cwd();
|
|
289
|
+
|
|
290
|
+
return formatReleaseLine({
|
|
291
|
+
changeset,
|
|
292
|
+
lookup: readCommitSubject({ cwd, hash: changeset.commit }),
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Read the gitmoji, scope, and breaking-change marker from a
|
|
298
|
+
* conventional-commit subject.
|
|
299
|
+
*
|
|
300
|
+
* @returns the parts found; none are set when the subject doesn't parse.
|
|
301
|
+
*/
|
|
302
|
+
export function parseConventionalSubject(subject: string): ConventionalSubject {
|
|
303
|
+
const groups = SUBJECT_PATTERN.exec(subject)?.groups;
|
|
304
|
+
|
|
305
|
+
return {
|
|
306
|
+
...(groups?.gitmoji === undefined ? {} : { gitmoji: groups.gitmoji }),
|
|
307
|
+
isBreaking: groups?.breaking !== undefined,
|
|
308
|
+
...(groups?.scope === undefined ? {} : { scope: groups.scope }),
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Read a commit's subject line, once per repo and hash.
|
|
314
|
+
*
|
|
315
|
+
* @returns the subject, or whether git had no such commit or couldn't read
|
|
316
|
+
* it.
|
|
317
|
+
*/
|
|
318
|
+
export function readCommitSubject({
|
|
319
|
+
cwd,
|
|
320
|
+
hash,
|
|
321
|
+
}: {
|
|
322
|
+
/** The repo to read from. @example "/Users/me/jj-kit" */
|
|
323
|
+
cwd: string;
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* The commit to read; never read as an option, even when it starts with
|
|
327
|
+
* `-`.
|
|
328
|
+
*
|
|
329
|
+
* @example "b4fc42d"
|
|
330
|
+
*/
|
|
331
|
+
hash: string;
|
|
332
|
+
}): CommitLookup {
|
|
333
|
+
const key = `${cwd}\0${hash}`;
|
|
334
|
+
const cached = LOOKUP_CACHE.get(key);
|
|
335
|
+
|
|
336
|
+
if (cached !== undefined) {
|
|
337
|
+
return cached;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
const lookup = readUncachedSubject({ cwd, hash });
|
|
341
|
+
LOOKUP_CACHE.set(key, lookup);
|
|
342
|
+
|
|
343
|
+
return lookup;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Say why a call failed, from whatever it threw.
|
|
348
|
+
*
|
|
349
|
+
* @returns the error's message, or the thrown value as a string.
|
|
350
|
+
*/
|
|
351
|
+
function describeError(error: unknown): string {
|
|
352
|
+
return error instanceof Error ? error.message : String(error);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* Escape the characters Markdown would read as formatting.
|
|
357
|
+
*
|
|
358
|
+
* @returns the text with each one backslash-escaped.
|
|
359
|
+
*/
|
|
360
|
+
function escapeMarkdown(text: string): string {
|
|
361
|
+
return text.replaceAll(MARKDOWN_SPECIAL_PATTERN, String.raw`\$&`);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Whether git exited with the status meaning "no such commit".
|
|
366
|
+
*
|
|
367
|
+
* @returns `true` for that status alone.
|
|
368
|
+
*/
|
|
369
|
+
function isNoSuchCommit(error: unknown): boolean {
|
|
370
|
+
return (
|
|
371
|
+
error instanceof Error &&
|
|
372
|
+
"status" in error &&
|
|
373
|
+
error.status === NO_SUCH_COMMIT_STATUS
|
|
374
|
+
);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Ask git for a commit's subject line.
|
|
379
|
+
*
|
|
380
|
+
* - `rev-parse` first, whose exit status tells a missing commit apart from
|
|
381
|
+
* git failing, in any locale and without reading its messages.
|
|
382
|
+
*
|
|
383
|
+
* @returns the subject, or whether git had no such commit or couldn't read
|
|
384
|
+
* it.
|
|
385
|
+
*/
|
|
386
|
+
function readUncachedSubject({
|
|
387
|
+
cwd,
|
|
388
|
+
hash,
|
|
389
|
+
}: {
|
|
390
|
+
/** The repo to read from. */
|
|
391
|
+
cwd: string;
|
|
392
|
+
|
|
393
|
+
/** The commit to read. */
|
|
394
|
+
hash: string;
|
|
395
|
+
}): CommitLookup {
|
|
396
|
+
let fullHash: string;
|
|
397
|
+
|
|
398
|
+
try {
|
|
399
|
+
fullHash = runGit({
|
|
400
|
+
args: [
|
|
401
|
+
"rev-parse",
|
|
402
|
+
"--verify",
|
|
403
|
+
"--quiet",
|
|
404
|
+
"--end-of-options",
|
|
405
|
+
`${hash}^{commit}`,
|
|
406
|
+
],
|
|
407
|
+
cwd,
|
|
408
|
+
}).trim();
|
|
409
|
+
} catch (error: unknown) {
|
|
410
|
+
// A hash from before a squash is expected; anything else is worth a look.
|
|
411
|
+
return isNoSuchCommit(error)
|
|
412
|
+
? { status: "missing" }
|
|
413
|
+
: reportUnreadable({ error, hash });
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
try {
|
|
417
|
+
// `--no-show-signature`, or `log.showSignature` would print gpg's check
|
|
418
|
+
// ahead of the subject.
|
|
419
|
+
const subject = runGit({
|
|
420
|
+
args: ["log", "-1", "--format=%s", "--no-show-signature", fullHash, "--"],
|
|
421
|
+
cwd,
|
|
422
|
+
}).trim();
|
|
423
|
+
|
|
424
|
+
return { status: "found", subject };
|
|
425
|
+
} catch (error: unknown) {
|
|
426
|
+
return reportUnreadable({ error, hash });
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* Warn that a commit couldn't be read.
|
|
432
|
+
*
|
|
433
|
+
* @returns the `unreadable` lookup.
|
|
434
|
+
*/
|
|
435
|
+
function reportUnreadable({
|
|
436
|
+
error,
|
|
437
|
+
hash,
|
|
438
|
+
}: {
|
|
439
|
+
/** What git threw; its message carries git's stderr. */
|
|
440
|
+
error: unknown;
|
|
441
|
+
|
|
442
|
+
/** The commit that couldn't be read. */
|
|
443
|
+
hash: string;
|
|
444
|
+
}): CommitLookup {
|
|
445
|
+
console.warn(
|
|
446
|
+
`[kit-changesets-changelog] couldn't read commit ${hash}, so its ` +
|
|
447
|
+
`line has no gitmoji or scope: ${describeError(error).trim()}`
|
|
448
|
+
);
|
|
449
|
+
|
|
450
|
+
return { status: "unreadable" };
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* Run git in a directory, with stdin ignored and its output captured.
|
|
455
|
+
*
|
|
456
|
+
* - Lazy fetches and credential prompts are off, so a partial clone missing
|
|
457
|
+
* a commit fails at once rather than waiting on the network or a prompt.
|
|
458
|
+
*
|
|
459
|
+
* @returns what git printed to stdout.
|
|
460
|
+
*/
|
|
461
|
+
function runGit({
|
|
462
|
+
args,
|
|
463
|
+
cwd,
|
|
464
|
+
}: {
|
|
465
|
+
/**
|
|
466
|
+
* The arguments after `git`.
|
|
467
|
+
*
|
|
468
|
+
* @example `["log", "-1", "--format=%s", "HEAD"]`
|
|
469
|
+
*/
|
|
470
|
+
args: readonly string[];
|
|
471
|
+
|
|
472
|
+
/** The directory to run it in. */
|
|
473
|
+
cwd: string;
|
|
474
|
+
}): string {
|
|
475
|
+
return execFileSync("git", args, {
|
|
476
|
+
cwd,
|
|
477
|
+
encoding: "utf8",
|
|
478
|
+
env: { ...process.env, GIT_NO_LAZY_FETCH: "1", GIT_TERMINAL_PROMPT: "0" },
|
|
479
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
480
|
+
timeout: GIT_TIMEOUT_MS,
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
/**
|
|
485
|
+
* Shorten a commit hash for a changelog line.
|
|
486
|
+
*
|
|
487
|
+
* @returns its first seven characters.
|
|
488
|
+
*/
|
|
489
|
+
function shortenHash(hash: string): string {
|
|
490
|
+
return hash.slice(0, SHORT_HASH_LENGTH);
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/** The formatter, in the shape changesets loads from `changelog`. */
|
|
494
|
+
const CHANGELOG_FUNCTIONS: ChangelogFunctions = {
|
|
495
|
+
getDependencyReleaseLine,
|
|
496
|
+
getReleaseLine,
|
|
497
|
+
};
|
|
498
|
+
|
|
499
|
+
export default CHANGELOG_FUNCTIONS;
|