1- import type { GitCommit } from "changelogen" ;
21import type { Context } from "../../types/index.js" ;
32import { 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" ;
73import { isCI } from "std-env" ;
84import { Base } from "../base.js" ;
95import Bump from "./bump.js" ;
6+ import { getConventionalChangelog , getGitCommits } from "./changelog.js" ;
107import GitHub from "./github.js" ;
118
129export 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