Skip to content

Commit 372b8ba

Browse files
committed
fix(projects): fix generate changelog
1 parent 22152e8 commit 372b8ba

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export async function getTotalChangelogMarkdown(options?: Partial<ChangelogOptio
8080
export async function generateChangelog(options?: Partial<ChangelogOption>) {
8181
const opts = await createOptions(options);
8282

83-
const existContent = await isVersionInMarkdown(opts.to, opts.output);
83+
const existContent = await isVersionInMarkdown(opts.to, opts.newVersion, opts.output);
8484

8585
if (!opts.regenerate && existContent) return;
8686

@@ -104,3 +104,5 @@ export async function generateTotalChangelog(options?: Partial<ChangelogOption>,
104104
}
105105

106106
export type { ChangelogOption };
107+
108+
generateChangelog();

src/markdown.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,7 @@ export function generateMarkdown(params: {
151151

152152
const lines: string[] = [];
153153

154-
const isNewVersion = !VERSION_REG.test(options.to);
155-
156-
const version = isNewVersion ? options.newVersion : options.to;
154+
const { version, isNewVersion } = getVersionInfo(options.to, options.newVersion);
157155

158156
const url = `https://github.com/${options.github.repo}/compare/${options.from}...${version}`;
159157

@@ -201,15 +199,20 @@ export function generateMarkdown(params: {
201199
return md;
202200
}
203201

204-
export async function isVersionInMarkdown(version: string, mdPath: string) {
202+
export async function isVersionInMarkdown(to: string, newVersion: string, mdPath: string) {
205203
let isIn = false;
206204

207-
const md = await readFile(mdPath, 'utf8');
205+
let md = '';
206+
try {
207+
md = await readFile(mdPath, 'utf8');
208+
} catch (error) {}
208209

209210
if (md) {
210211
const matches = md.match(VERSION_REG_OF_MARKDOWN);
211212

212213
if (matches?.length) {
214+
const { version } = getVersionInfo(to, newVersion);
215+
213216
const versionInMarkdown = `## [${version}]`;
214217

215218
isIn = matches.includes(versionInMarkdown);
@@ -219,13 +222,24 @@ export async function isVersionInMarkdown(version: string, mdPath: string) {
219222
return isIn;
220223
}
221224

225+
function getVersionInfo(to: string, newVersion: string) {
226+
const isNewVersion = !VERSION_REG.test(to);
227+
228+
const version = isNewVersion ? newVersion : to;
229+
230+
return {
231+
version,
232+
isNewVersion
233+
};
234+
}
235+
222236
export async function writeMarkdown(md: string, mdPath: string, regenerate = false) {
223237
let changelogMD: string = '';
224238

225239
const changelogPrefix = '# Changelog';
226240

227241
if (!existsSync(mdPath)) {
228-
await writeFile(mdPath, changelogPrefix, 'utf8');
242+
await writeFile(mdPath, `${changelogPrefix}\n\n`, 'utf8');
229243
}
230244

231245
if (!regenerate) {
@@ -239,7 +253,7 @@ export async function writeMarkdown(md: string, mdPath: string, regenerate = fal
239253
const lastEntry = changelogMD.match(/^###?\s+.*$/m);
240254

241255
if (lastEntry) {
242-
changelogMD += `${changelogMD.slice(0, lastEntry.index) + md}\n\n${changelogMD.slice(lastEntry.index)}`;
256+
changelogMD = `${changelogMD.slice(0, lastEntry.index) + md}\n\n${changelogMD.slice(lastEntry.index)}`;
243257
} else {
244258
changelogMD += `\n${md}\n\n`;
245259
}

0 commit comments

Comments
 (0)