Skip to content

Commit 0a98d6a

Browse files
committed
reafctor: split changelog module
1 parent 47b95cd commit 0a98d6a

2 files changed

Lines changed: 97 additions & 95 deletions

File tree

src/core/releaser/changelog.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import type { GitCommit } from "changelogen";
2+
import { execSync } from "node:child_process";
3+
import process from "node:process";
4+
import { generateMarkDown, getGitDiff, loadChangelogConfig } from "changelogen";
5+
import { escapeRegExp } from "es-toolkit";
6+
7+
export async function getConventionalChangelog(commits: GitCommit[]) {
8+
// If user do not use Conventional Commit,
9+
// return commit messages directly.
10+
if (!commits[0].type) {
11+
return commits.map(c => c.message).join("\n");
12+
}
13+
14+
const resolvedCommits = commits.map((c) => {
15+
if (c.type === "remove") {
16+
c.isBreaking = true;
17+
}
18+
return c;
19+
});
20+
21+
const _config = await loadChangelogConfig(process.cwd(), {
22+
types: {
23+
add: { title: "🚀 Enhancements", semver: "minor" },
24+
change: { title: "🩹 Fixes", semver: "patch" },
25+
remove: { title: "🩹 Fixes", semver: "minor" },
26+
},
27+
hideAuthorEmail: true,
28+
});
29+
const md = await generateMarkDown(resolvedCommits, _config);
30+
return md.split("\n").slice(3).join("\n");
31+
}
32+
33+
export async function getGitCommits(currentTag: string, commitMessageTemplate: string): Promise<GitCommit[]> {
34+
/**
35+
* Get all git tags
36+
*
37+
* @example
38+
*
39+
* ```bash
40+
* $ git tag -l --sort=v:refname
41+
* v2.0.9
42+
* v2.0.10
43+
* v2.0.11
44+
* v2.0.12
45+
* v2.0.13
46+
* v2.0.13-beta.1
47+
* v2.0.13-beta.2
48+
* v2.0.13-beta.3
49+
* v2.0.14
50+
* ```
51+
*/
52+
const tags = execSync("git tag -l --sort=v:refname").toString().trim().split("\n");
53+
54+
const currentTagIndex = tags.indexOf(currentTag);
55+
if (currentTagIndex === -1)
56+
throw new Error(`Tag "${currentTag}" not found.`);
57+
58+
let previousTagIndex = currentTagIndex - 1;
59+
let previousTag: string | undefined;
60+
61+
if (currentTagIndex === 0) {
62+
// If the current tag is the first tag, get all logs before this one
63+
previousTag = undefined;
64+
}
65+
// Otherwise, get log between this tag and previous one
66+
else if (currentTag.includes("-")) {
67+
// If current tag is pre-release, previous one should be any (include prerelease and official)
68+
previousTag = tags[previousTagIndex];
69+
}
70+
else {
71+
// If current tag is official release, previous one should be official too
72+
// Find the last non-pre-release tag
73+
while (previousTagIndex >= 0 && tags[previousTagIndex].includes("-")) {
74+
previousTagIndex--;
75+
}
76+
if (previousTagIndex < 0)
77+
// If no previous official release is found, get all logs up to the currentTag
78+
previousTag = undefined;
79+
else
80+
previousTag = tags[previousTagIndex];
81+
}
82+
83+
// const commitMessage = this.ctx.release.bumpp.commit;
84+
const filterRegex = new RegExp(escapeRegExp(commitMessageTemplate));
85+
const rawCommits = (await getGitDiff(previousTag, currentTag)).filter(c => !filterRegex.test(c.message));
86+
// @ts-expect-error we know options only needs scopeMap
87+
return parseCommits(rawCommits, { scopeMap: {} });
88+
89+
// if (previousTag)
90+
// return execSync(`git log --pretty=format:"* %s (%h)" ${previousTag}..${currentTag}`).toString().trim();
91+
// else
92+
// return execSync(`git log --pretty=format:"* %s (%h)" ${currentTag}`).toString().trim();
93+
}

src/core/releaser/index.ts

Lines changed: 4 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
import type { GitCommit } from "changelogen";
21
import type { Context } from "../../types/index.js";
32
import { execSync } from "node:child_process";
4-
import process from "node:process";
5-
import { generateMarkDown, getGitDiff, loadChangelogConfig, parseCommits } from "changelogen";
6-
import { escapeRegExp } from "es-toolkit";
73
import { isCI } from "std-env";
84
import { Base } from "../base.js";
95
import Bump from "./bump.js";
6+
import { getConventionalChangelog, getGitCommits } from "./changelog.js";
107
import GitHub from "./github.js";
118

129
export default class Release extends Base {
@@ -71,98 +68,10 @@ export default class Release extends Base {
7168
);
7269
}
7370

74-
async getConventionalChangelog(commits: GitCommit[]) {
75-
// If user do not use Conventional Commit,
76-
// return commit messages directly.
77-
if (!commits[0].type) {
78-
return commits.map(c => c.message).join("\n");
79-
}
80-
81-
const resolvedCommits = commits.map((c) => {
82-
if (c.type === "remove") {
83-
c.isBreaking = true;
84-
}
85-
return c;
86-
});
87-
88-
const _config = await loadChangelogConfig(process.cwd(), {
89-
types: {
90-
add: { title: "🚀 Enhancements", semver: "minor" },
91-
change: { title: "🩹 Fixes", semver: "patch" },
92-
remove: { title: "🩹 Fixes", semver: "minor" },
93-
},
94-
});
95-
const md = await generateMarkDown(resolvedCommits, _config);
96-
return md.split("\n").slice(3).join("\n");
97-
}
98-
99-
async getGitDiff(): Promise<GitCommit[]> {
100-
const currentTag = this.ctx.release.bumpp.tag;
101-
102-
/**
103-
* Get all git tags
104-
*
105-
* @example
106-
*
107-
* ```bash
108-
* $ git tag -l --sort=v:refname
109-
* v2.0.9
110-
* v2.0.10
111-
* v2.0.11
112-
* v2.0.12
113-
* v2.0.13
114-
* v2.0.13-beta.1
115-
* v2.0.13-beta.2
116-
* v2.0.13-beta.3
117-
* v2.0.14
118-
* ```
119-
*/
120-
const tags = execSync("git tag -l --sort=v:refname").toString().trim().split("\n");
121-
122-
const currentTagIndex = tags.indexOf(currentTag);
123-
if (currentTagIndex === -1)
124-
throw new Error(`Tag "${currentTag}" not found.`);
125-
126-
let previousTagIndex = currentTagIndex - 1;
127-
let previousTag: string | undefined;
128-
129-
if (currentTagIndex === 0) {
130-
// If the current tag is the first tag, get all logs before this one
131-
previousTag = undefined;
132-
}
133-
// Otherwise, get log between this tag and previous one
134-
else if (currentTag.includes("-")) {
135-
// If current tag is pre-release, previous one should be any (include prerelease and official)
136-
previousTag = tags[previousTagIndex];
137-
}
138-
else {
139-
// If current tag is official release, previous one should be official too
140-
// Find the last non-pre-release tag
141-
while (previousTagIndex >= 0 && tags[previousTagIndex].includes("-")) {
142-
previousTagIndex--;
143-
}
144-
if (previousTagIndex < 0)
145-
// If no previous official release is found, get all logs up to the currentTag
146-
previousTag = undefined;
147-
else
148-
previousTag = tags[previousTagIndex];
149-
}
150-
151-
const commitMessage = this.ctx.release.bumpp.commit;
152-
const filterRegex = new RegExp(escapeRegExp(commitMessage));
153-
const rawCommits = (await getGitDiff(previousTag, currentTag)).filter(c => !filterRegex.test(c.message));
154-
// @ts-expect-error we know options only needs scopeMap
155-
return parseCommits(rawCommits, { scopeMap: {} });
156-
157-
// if (previousTag)
158-
// return execSync(`git log --pretty=format:"* %s (%h)" ${previousTag}..${currentTag}`).toString().trim();
159-
// else
160-
// return execSync(`git log --pretty=format:"* %s (%h)" ${currentTag}`).toString().trim();
161-
}
162-
16371
async getChangelog() {
72+
const { commit, tag } = this.ctx.release.bumpp;
16473
let changelog: string;
165-
const rawCommit = await this.getGitDiff();
74+
const rawCommit = await getGitCommits(tag, commit);
16675
if (rawCommit.length === 0) {
16776
return "_No significant changes._";
16877
}
@@ -175,7 +84,7 @@ export default class Release extends Base {
17584
changelog = execSync(changelogConfig).toString().trim();
17685
}
17786
else {
178-
changelog = await this.getConventionalChangelog(rawCommit);
87+
changelog = await getConventionalChangelog(rawCommit);
17988
}
18089
this.logger.debug(`Got changelog:\n${changelog}\n`);
18190
return changelog;

0 commit comments

Comments
 (0)