Skip to content

Commit

Permalink
feat: make dep conventional-changelog optional (#57)
Browse files Browse the repository at this point in the history
* feat: make dep conventional-changelog optional

* docs: improve comments for changelog config
  • Loading branch information
northword authored Oct 17, 2024
1 parent b1fc581 commit bd968a1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 20 deletions.
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@
"lib": "dist",
"doc": "docs"
},
"peerDependencies": {
"conventional-changelog": "^6.0.0"
},
"peerDependenciesMeta": {
"conventional-changelog": {
"optional": true
}
},
"dependencies": {
"@commander-js/extra-typings": "^12.1.0",
"@inquirer/prompts": "^7.0.0",
Expand All @@ -68,7 +76,6 @@
"chalk": "^5.3.0",
"chokidar": "^4.0.1",
"commander": "^12.1.0",
"conventional-changelog": "^6.0.0",
"es-toolkit": "^1.25.2",
"esbuild": "^0.24.0",
"fs-extra": "^11.2.0",
Expand Down
38 changes: 23 additions & 15 deletions src/core/releaser/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type { Context } from "../../types/index.js";
import { execSync } from "node:child_process";
// @ts-expect-error no types
import conventionalChangelog from "conventional-changelog";
import { escapeRegExp } from "es-toolkit";
import { isCI } from "std-env";
import { Base } from "../base.js";
Expand Down Expand Up @@ -55,7 +53,7 @@ export default class Release extends Base {
await this.ctx.hooks.callHook("release:push", this.ctx);

// Get changelog
this.ctx.release.changelog = this.getChangelog();
this.ctx.release.changelog = await this.getChangelog();

// Publish to GitHub, Gitee
if (isGitHubEnabled) {
Expand All @@ -76,26 +74,34 @@ export default class Release extends Base {
);
}

getConventionalChangelog(): Promise<string> {
async getConventionalChangelog(): Promise<string> {
const { version } = this.ctx;
// @ts-expect-error no types
const { default: conventionalChangelog } = await import("conventional-changelog");

return new Promise((resolve, reject) => {
let changelog = "";
conventionalChangelog({ releaseCount: 2 }, { version })
conventionalChangelog({ releaseCount: 2, preset: "angular" }, { version })
.on("data", (chunk: any) => {
changelog += chunk.toString();
})
.on("end", () => {
this.logger.debug("changelog:", changelog.trim());
resolve(changelog.trim());
changelog = changelog
.split("\n")
.filter(line => !line.match(/^## .*/))
.join("\n")
.trim();
resolve(changelog);
})
.on("error", (err: any) => {
reject(err);
});
});
}

getGitLog(currentTag: string) {
getGitLog() {
const currentTag = this.ctx.release.bumpp.tag;

/**
* Get all git tags
*
Expand Down Expand Up @@ -151,7 +157,8 @@ export default class Release extends Base {
return execSync(`git log --pretty=format:"* %s (%h)" ${currentTag}`).toString().trim();
}

getFilteredChangelog(rawLog: string, commitMessage: string) {
getFilteredChangelog(rawLog: string) {
const commitMessage = this.ctx.release.bumpp.commit;
const filterRegex = new RegExp(escapeRegExp(commitMessage));

const filteredLog = rawLog
Expand All @@ -165,21 +172,22 @@ export default class Release extends Base {
return filteredLog;
}

getChangelog() {
async getChangelog() {
let changelog: string;
const changelogConfig = this.ctx.release.changelog;
if (typeof changelogConfig == "function") {
changelog = changelogConfig(this.ctx);
}
else if (!!changelogConfig && changelogConfig === "conventional-changelog") {
const rawLog = await this.getConventionalChangelog();
changelog = this.getFilteredChangelog(rawLog);
}
else if (!!changelogConfig && typeof changelogConfig == "string") {
changelog = execSync(changelogConfig).toString().trim();
}
else {
const resolvedCurrentTag = this.ctx.release.bumpp.tag;
const rawLog = this.getGitLog(resolvedCurrentTag);

const resolvedCommitMessage = this.ctx.release.bumpp.commit;
changelog = this.getFilteredChangelog(rawLog, resolvedCommitMessage);
const rawLog = this.getGitLog();
changelog = this.getFilteredChangelog(rawLog);
}
this.logger.debug(`Got changelog:\n${changelog}\n`);
return changelog;
Expand Down
13 changes: 9 additions & 4 deletions src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,14 +460,19 @@ export interface ReleaseConfig {
/**
* Changelog.
*
* If a string is provided, it must be command line
* and with changelog output to stdout.
* - "conventional-changelog": use conventional-changelog with angular preset,
* you must install "conventional-changelog" as an peer dependency.
* - string: a command line with changelog output to stdout.
*
* 如果提供了字符串,则必须为命令行,且变更日志输出到stdout
* 变更日志。
*
* - "conventional-changelog": 使用约定式变更日志,使用 angular 预设,
* 你需要手动安装 conventional-changelog 作为依赖项。
* - string: 命令行,变更日志输出到 stdout
*
* @default "git log {{previousTag}}..{{currentTag}}"
*/
changelog: string | ((ctx: Context) => string);
changelog: "conventional-changelog" | string | ((ctx: Context) => string);

/**
* Release to GitHub.
Expand Down

0 comments on commit bd968a1

Please sign in to comment.