-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Changesets changelog package (#168)
- Loading branch information
1 parent
f30335b
commit 3eb7110
Showing
6 changed files
with
203 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@zazen/changesets-changelog': major | ||
--- | ||
|
||
Initial release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } | ||
], | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.