@@ -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+
222236export 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