Skip to content

Commit

Permalink
fix(build): include latest changelog when publishing
Browse files Browse the repository at this point in the history
  • Loading branch information
wyze committed Apr 30, 2021
1 parent 93943b8 commit e9360be
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions scripts/generate-changelog.js
@@ -1,6 +1,7 @@
'use strict'

const { exec } = require('child_process')
const { readFile } = require('fs').promises

/**
* This parses tags from git metadata and returns a tuple of the tag and version.
Expand Down Expand Up @@ -28,26 +29,43 @@ const fetchTags = () =>
)
})

/**
* Gets version about to be published from lerna along with todays date.
* We use this to pass to clog for our latest changelog entry.
*
* @return {Promise<[date: string, version: string]>}
*/
const getUpcomingTagInfo = async () => {
const json = await readFile('./lerna.json', 'utf8')
const parsed = JSON.parse(json)
const [date] = new Date().toJSON().split('T')

return [date, `v${parsed.version}`]
}

/**
* Send tag information to clog to generate our changelog entry
*
* @param {Object} info - Information needed to build a changelog entry
* @param {[date: string, version: string]} info.tag - Tag tuple with date and version
* @param {string | undefined} info.from - Beginning tag version range
* @param {boolean | undefined} info.fromLatestTag - Use latest tag up until now
* @param {string | undefined} info.to - End tag version range
* @return {Promise<string>}
*/
const changes = ({ tag: [date, version], from, to }) =>
const changes = ({ tag: [date, version], from, fromLatestTag, to }) =>
new Promise((resolve, reject) => {
exec(
`clog ${from ? ` --from ${from}` : ''}${
to ? ` --to ${to}` : ''
} --setversion ${version} --setdate ${date} -o CHANGELOG.md`,
(err, stdout) => {
if (err) return reject(err)
resolve(stdout.trim().split('\n'))
}
)
const args = [
from ? ` --from ${from}` : '',
to ? ` --to ${to}` : '',
fromLatestTag ? ' --from-latest-tag' : '',
` --setdate ${date}`,
].join('')

exec(`clog ${args} --setversion ${version} -o CHANGELOG.md`, (err, stdout) => {
if (err) return reject(err)
resolve(stdout.trim().split('\n'))
})
})

/**
Expand All @@ -60,11 +78,14 @@ const changes = ({ tag: [date, version], from, to }) =>
*/
const run = async () => {
const tags = await fetchTags()

for (let i = 0; i < tags.length; i++) {
if (i === 0) await changes({ tag: tags[i], from: '0.31.0', to: tags[i][1] })
else await changes({ tag: tags[i], from: tags[i - 1][1], to: tags[i][1] })
}

await changes({ tag: await getUpcomingTagInfo(), fromLatestTag: true })

await new Promise((resolve, reject) => {
exec('git add --all', err => {
if (err) return reject(err)
Expand Down

0 comments on commit e9360be

Please sign in to comment.