Skip to content

Commit

Permalink
Add Changesets changelog package (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
stormwarning authored Nov 17, 2023
1 parent f30335b commit 3eb7110
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"access": "public",
"baseBranch": "main",
"changelog": [
"@changesets/changelog-github",
"../packages/changesets-changelog/index.js",
{ "repo": "stormwarning/zazen" }
],
"commit": false,
Expand Down
5 changes: 5 additions & 0 deletions .changeset/friendly-lions-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@zazen/changesets-changelog': major
---

Initial release
18 changes: 18 additions & 0 deletions packages/changesets-changelog/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# @zazen/changesets-changelog

> A simplified version of [@changesets/changelog-github](https://github.com/changesets/changesets/tree/main/packages/changelog-github).
## Usage

```shell
npm install --save-dev @changesets/cli @zazen/changesets-changelog
```

Add the following to your `.changeset/config.json`:

```json
"changelog": [
"@changesets/changelog-github",
{ "repo": "org/repo" }
],
```
130 changes: 130 additions & 0 deletions packages/changesets-changelog/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/**
* Custom changelog functions to modify release line formatting.
*
* @see https://github.com/atlassian/changesets/blob/main/packages/changelog-github/src/index.ts
*/

import { getInfo, getInfoFromPullRequest } from '@changesets/get-github-info'

/**
* @param {Record<string, any> | null} options
*/
function validateOptions(options) {
if (!options || !options.repo) {
throw new Error(
'Please provide a repo to this changelog generator like this:\n"changelog": ["@zazen/changesets-changelog", { "repo": "org/repo" }]',
)
}
}

/** @type {import('@changesets/types').ChangelogFunctions} */
const changelogFunctions = {
async getDependencyReleaseLine(changesets, dependenciesUpdated, options) {
validateOptions(options)
if (dependenciesUpdated.length === 0) return ''

let changesetCommits = await Promise.all(
changesets.map(async (cs) => {
if (cs.commit) {
let { links } = await getInfo({
repo: options.repo,
commit: cs.commit,
})
return links.commit
}
}),
)
let changesetLink = `- Updated dependencies ${changesetCommits
.filter(Boolean)
.join(', ')}:`
let updatedDepenenciesList = dependenciesUpdated.map(
(dependency) => ` - ${dependency.name}@${dependency.newVersion}`,
)

return [changesetLink, ...updatedDepenenciesList].join('\n')
},

async getReleaseLine(changeset, options) {
validateOptions(options)

let [repoOwner] = options.repo.split('/')
/** @type {number} */
let prFromSummary
/** @type {string} */
let commitFromSummary
/** @type {Array<string>} */
let usersFromSummary = []

let replacedSummary = changeset.summary
.replace(/^\s*(?:pr|pull|pull\s+request):\s*#?(\d+)/im, (_, pr) => {
let prNumber = Number(pr)
if (!Number.isNaN(prNumber)) prFromSummary = prNumber
return ''
})
.replace(/^\s*commit:\s*(\S+)/im, (_, commit) => {
commitFromSummary = commit
return ''
})
.replaceAll(/^\s*(?:author|user):\s*@?(\S+)/gim, (_, user) => {
// Ignore user if it's your own repo.
if (!user.includes(repoOwner)) usersFromSummary.push(user)
return ''
})
.trim()

let [firstLine, ...futureLines] = replacedSummary
.split('\n')
.map((l) => l.trimEnd())

let links = await (async () => {
if (prFromSummary !== undefined) {
let { links } = await getInfoFromPullRequest({
repo: options.repo,
pull: prFromSummary,
})

if (commitFromSummary) {
links = {
...links,
commit: `[\`${commitFromSummary}\`](https://github.com/${options.repo}/commit/${commitFromSummary})`,
}
}

return links
}

let commitToFetchFrom = commitFromSummary || changeset.commit
if (commitToFetchFrom) {
let { links } = await getInfo({
repo: options.repo,
commit: commitToFetchFrom,
})
return links
}

return {
commit: null,
pull: null,
user: null,
}
})()
let users =
usersFromSummary.length > 0
? usersFromSummary
.map(
(userFromSummary) =>
`[@${userFromSummary}](https://github.com/${userFromSummary})`,
)
.join(', ')
: links.user.includes(repoOwner) && links.user
let userThanks = users === null ? '' : `\n Thanks ${users}!`
let versionInfo =
links.pull === null ? ` ${links.commit}` : ` (${links.pull})`

return `\n\n- ${firstLine}${versionInfo}${userThanks}\n${futureLines
.map((l) => ` ${l}`)
.join('\n')}`
},
}

export default changelogFunctions
28 changes: 28 additions & 0 deletions packages/changesets-changelog/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "@zazen/changesets-changelog",
"version": "1.0.0",
"description": "Generate changelogs, free of weariness and confusion",
"keywords": [
"changelog",
"changesets"
],
"homepage": "https://github.com/stormwarning/zazen/blob/main/packages/changesets-changelog",
"repository": {
"type": "git",
"url": "git+https://github.com/stormwarning/zazen.git",
"directory": "packages/changesets-changelog"
},
"license": "ISC",
"author": "Jeff (https://tidaltheory.io)",
"files": [
"index.js"
],
"dependencies": {
"@changesets/get-github-info": "0.5.2",
"dotenv": "16.3.1"
},
"devDependencies": {
"@changesets/parse": "0.3.16",
"@changesets/types": "5.2.1"
}
}
27 changes: 21 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3eb7110

Please sign in to comment.