11const fs = require ( 'fs' )
2-
2+ const { set } = require ( 'immutadot' )
33const libnpm = require ( 'libnpm' )
44const semverCompare = require ( 'semver-compare' )
55const { generateChangelog, logger } = require ( '@gitmoji-changelog/core' )
6- const { buildMarkdownFile } = require ( '@gitmoji-changelog/markdown' )
6+ const { buildMarkdownFile, getLatestVersion } = require ( '@gitmoji-changelog/markdown' )
7+
78
89const pkg = require ( '../package.json' )
910
10- async function getLatestVersion ( ) {
11+ async function getGitmojiChangelogLatestVersion ( ) {
1112 const watchdog = new Promise ( resolve => setTimeout ( resolve , 500 , { version : pkg . version } ) )
1213 const request = libnpm . manifest ( 'gitmoji-changelog@latest' )
1314
@@ -21,7 +22,7 @@ async function main(options = {}) {
2122 logger . info ( `${ options . mode } ${ options . output } ` )
2223
2324 try {
24- const latestVersion = await getLatestVersion ( )
25+ const latestVersion = await getGitmojiChangelogLatestVersion ( )
2526 if ( semverCompare ( latestVersion , pkg . version ) > 0 ) {
2627 logger . warn ( `You got an outdated version of gitmoji-changelog, please update! (yours: ${ pkg . version } , latest: ${ latestVersion } )` )
2728 logger . warn ( 'Just do the following npm command to update it:' )
@@ -36,22 +37,25 @@ async function main(options = {}) {
3637 }
3738
3839 try {
39- const changelog = await generateChangelog ( options )
40+ switch ( options . format ) {
41+ case 'json' : {
42+ const changelog = await generateChangelog ( options )
4043
41- if ( changelog . meta . package ) {
42- const { name, version } = changelog . meta . package
43- logger . info ( `${ name } v${ version } ` )
44- }
45- if ( changelog . meta . repository ) {
46- logger . info ( changelog . meta . repository . url )
47- }
44+ logMetaData ( changelog )
4845
49- switch ( options . format ) {
50- case 'json' :
5146 fs . writeFileSync ( options . output , JSON . stringify ( changelog ) )
5247 break
53- default :
54- await buildMarkdownFile ( changelog , options )
48+ }
49+ default : {
50+ const lastVersion = getLatestVersion ( options . output )
51+ const newOptions = set ( options , 'meta.lastVersion' , lastVersion )
52+
53+ const changelog = await generateChangelog ( newOptions )
54+
55+ logMetaData ( changelog )
56+
57+ await buildMarkdownFile ( changelog , newOptions )
58+ }
5559 }
5660 logger . success ( `changelog updated into ${ options . output } ` )
5761 } catch ( e ) {
@@ -62,4 +66,14 @@ async function main(options = {}) {
6266 process . exit ( 0 )
6367}
6468
69+ function logMetaData ( changelog ) {
70+ if ( changelog . meta . package ) {
71+ const { name, version } = changelog . meta . package
72+ logger . info ( `${ name } v${ version } ` )
73+ }
74+ if ( changelog . meta . repository ) {
75+ logger . info ( changelog . meta . repository . url )
76+ }
77+ }
78+
6579module . exports = { main }
0 commit comments