const fs = require('fs')
const path = require('path')
const log = require('npmlog/log')

const lerna = require('../lerna')
const {CHANGELOG_FILENAME} = require('./constants')

const changelogPath = path.join(process.cwd(), CHANGELOG_FILENAME)
const content = fs.readFileSync(changelogPath, 'utf-8')

const lines = content
  .split('\n')
  .map(line => {
    if (line === '---') {
      const today = new Date()
      const date = `${today.getUTCFullYear()}-${today.getUTCMonth() + 1}-${today.getUTCDate()}`

      return `---\n\n## ${lerna.version} (${date})`
    }

    return line
  })
  .join('\n')

fs.writeFile(changelogPath, lines, 'utf-8', err => {
  if (err) {
    log.error('jss', 'Error while updating changelog')
    process.exit(1)
  } else {
    log.info('jss', 'Successfully updated changelog')
  }
})

@@ -363,18 +363,30 @@
 line
- === '---'
+.startsWith('## Next')
 ) %7B%0A
@@ -531,11 +531,15 @@
 rn %60
----
+$%7Bline%7D
 %5Cn%5Cn

make it more obvious where to add changelogs for the next version