Skip to content

Commit 9419e74

Browse files
feat: prepend changelog flag
1 parent 4a30534 commit 9419e74

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

cmd/semantic-release/main.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,17 @@ func cliHandler(cmd *cobra.Command, args []string) {
209209
NewVersion: newVer,
210210
})
211211
if conf.Changelog != "" {
212-
exitIfError(ioutil.WriteFile(conf.Changelog, []byte(changelogRes), 0644))
212+
oldFile := make([]byte, 0)
213+
if conf.PrependChangelog {
214+
oldFile, err = ioutil.ReadFile(conf.Changelog)
215+
if err == nil {
216+
oldFile = append([]byte("\n\n"), oldFile...)
217+
} else {
218+
oldFile = make([]byte, 0)
219+
}
220+
}
221+
changelogData := append([]byte(changelogRes), oldFile...)
222+
exitIfError(ioutil.WriteFile(conf.Changelog, changelogData, 0644))
213223
}
214224

215225
if conf.Dry {

pkg/config/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type Config struct {
3434
AllowInitialDevelopmentVersions bool
3535
AllowNoChanges bool
3636
MaintainedVersion string
37+
PrependChangelog bool
3738
}
3839

3940
func mustGetString(cmd *cobra.Command, name string) string {
@@ -120,6 +121,7 @@ func NewConfig(cmd *cobra.Command) (*Config, error) {
120121
AllowInitialDevelopmentVersions: mustGetBool(cmd, "allow-initial-development-versions"),
121122
AllowNoChanges: mustGetBool(cmd, "allow-no-changes"),
122123
MaintainedVersion: viper.GetString("maintainedVersion"),
124+
PrependChangelog: mustGetBool(cmd, "prepend-changelog"),
123125
}
124126
return conf, nil
125127
}
@@ -171,6 +173,7 @@ func InitConfig(cmd *cobra.Command) error {
171173
cmd.Flags().Bool("dry", false, "do not create release")
172174
cmd.Flags().Bool("allow-initial-development-versions", false, "semantic-release will start your initial development release at 0.1.0")
173175
cmd.Flags().Bool("allow-no-changes", false, "exit with code 0 if no changes are found, useful if semantic-release is automatically run")
176+
cmd.Flags().Bool("prepend-changelog", false, "if the changelog file already exist the new changelog is prepended")
174177
cmd.Flags().SortFlags = true
175178

176179
viper.AddConfigPath(".")

0 commit comments

Comments
 (0)