-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(version): use conventional commit changelog writer for perf
- Loading branch information
1 parent
911b9b5
commit e9d7c52
Showing
9 changed files
with
178 additions
and
127 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 |
---|---|---|
|
@@ -726,7 +726,7 @@ describe('conventional-commits', () => { | |
|
||
const opts = { | ||
changelogPreset: 'conventional-changelog-angular', | ||
changelogIncludeCommitsGitAuthor: ' by (**%a**)', | ||
changelogIncludeCommitsGitAuthor: ' by **%a** (%e)', | ||
}; | ||
const [changelogOne, changelogTwo] = await Promise.all([ | ||
updateChangelog(pkg1, 'independent', opts), | ||
|
@@ -739,15 +739,15 @@ describe('conventional-commits', () => { | |
### Bug Fixes | ||
* **stuff:** changed ([SHA](https://github.com/lerna/conventional-commits-independent/commit/GIT_HEAD)) by (**Tester McPerson**) | ||
* **stuff:** changed ([SHA](https://github.com/lerna/conventional-commits-independent/commit/GIT_HEAD)) by **Tester McPerson** ([email protected]) | ||
`); | ||
expect(changelogTwo.newEntry.trimRight()).toMatchInlineSnapshot(` | ||
# [1.1.0](/compare/[email protected]@1.1.0) (YYYY-MM-DD) | ||
### Features | ||
* **thing:** added ([SHA](https://github.com/lerna/conventional-commits-independent/commit/GIT_HEAD)) by (**Tester McPerson**) | ||
* **thing:** added ([SHA](https://github.com/lerna/conventional-commits-independent/commit/GIT_HEAD)) by **Tester McPerson** ([email protected]) | ||
`); | ||
}); | ||
|
||
|
@@ -787,7 +787,7 @@ describe('conventional-commits', () => { | |
}; | ||
const opt2s = { | ||
changelogPreset: 'conventional-changelog-angular', | ||
changelogIncludeCommitsClientLogin: ' by (@%l, %a)', | ||
changelogIncludeCommitsClientLogin: ' from @%l, _%a (%e)_', | ||
commitsSinceLastRelease: [ | ||
{ | ||
authorName: 'Tester McPerson', | ||
|
@@ -817,7 +817,7 @@ describe('conventional-commits', () => { | |
### Features | ||
* **thing:** added ([SHA](https://github.com/lerna/conventional-commits-independent/commit/GIT_HEAD)) by (@tester-mcperson, Tester McPerson) | ||
* **thing:** added ([SHA](https://github.com/lerna/conventional-commits-independent/commit/GIT_HEAD)) from @tester-mcperson, _Tester McPerson ([email protected])_ | ||
`); | ||
}); | ||
}); | ||
|
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
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
103 changes: 103 additions & 0 deletions
103
packages/core/src/conventional-commits/writer-opts-transform.ts
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,103 @@ | ||
import { Context, GitRawCommitsOptions } from 'conventional-changelog-core'; | ||
import { Options as WriterOptions } from 'conventional-changelog-writer'; | ||
import { Commit } from 'conventional-commits-parser'; | ||
import { ChangelogConfig, RemoteCommit } from '../models'; | ||
|
||
const GIT_COMMIT_WITH_AUTHOR_FORMAT = | ||
'%B%n-hash-%n%H%n-gitTags-%n%d%n-committerDate-%n%ci%n-authorName-%n%an%n-authorEmail-%n%ae%n-gpgStatus-%n%G?%n-gpgSigner-%n%GS'; | ||
|
||
/** | ||
* Change the changelog config, we need to update the default format to include commit author name/email, | ||
* available formats can be found at Git's url: https://git-scm.com/docs/git-log#_pretty_formats | ||
* Add a `format` to the `conventional-changelog-core` of `gitRawCommitsOpts` will make it available in the commit template | ||
* https://github.com/conventional-changelog/conventional-changelog/blob/master/packages/git-raw-commits/index.js#L27 | ||
* then no matter which changelog preset is loaded, we'll append the git author name to the commit template | ||
* ie:: **deps:** update all non-major dependencies ([ed1db35](https://github.com/.../ed1db35)) (Renovate Bot) | ||
* @param {ChangelogConfig} config | ||
* @param {GitRawCommitsOptions} gitRawCommitsOpts | ||
* @param {WriterOptions} writerOpts | ||
* @param {string | boolean} [commitCustomFormat] | ||
*/ | ||
export function setConfigChangelogCommitGitAuthor( | ||
config: ChangelogConfig, | ||
gitRawCommitsOpts: GitRawCommitsOptions, | ||
writerOpts: WriterOptions, | ||
commitCustomFormat?: string | boolean | ||
) { | ||
gitRawCommitsOpts.format = GIT_COMMIT_WITH_AUTHOR_FORMAT; | ||
const extraCommitMsg = | ||
typeof commitCustomFormat === 'string' | ||
? commitCustomFormat.replace(/%a/g, '{{authorName}}' || '').replace(/%e/g, '{{authorEmail}}' || '') | ||
: `({{authorName}})`; | ||
writerOpts.commitPartial = | ||
config.writerOpts.commitPartial!.replace(/\n*$/, '') + ` {{#if @root.linkReferences~}}${extraCommitMsg}{{~/if}}\n`; | ||
} | ||
|
||
/** | ||
* Change the changelog config, we need to update the default format to include commit author name/email, | ||
* available formats can be found at Git's url: https://git-scm.com/docs/git-log#_pretty_formats | ||
* We also need to change the transform function and add remote client login (GitHub) | ||
* Add a `format` to the `conventional-changelog-core` of `gitRawCommitsOpts` will make it available in the commit template | ||
* https://github.com/conventional-changelog/conventional-changelog/blob/master/packages/git-raw-commits/index.js#L27 | ||
* then no matter which changelog preset is loaded, we'll append the git author name to the commit template | ||
* ie:: **deps:** update all non-major dependencies ([ed1db35](https://github.com/.../ed1db35)) (@renovate-bot) | ||
* @param {ChangelogConfig} config | ||
* @param {GitRawCommitsOptions} gitRawCommitsOpts | ||
* @param {WriterOptions} writerOpts | ||
* @param {RemoteCommit[]} commitsSinceLastRelease | ||
* @param {string | boolean} [commitCustomFormat] | ||
*/ | ||
export function setConfigChangelogCommitClientLogin( | ||
config: ChangelogConfig, | ||
gitRawCommitsOpts: GitRawCommitsOptions, | ||
writerOpts: WriterOptions, | ||
commitsSinceLastRelease: RemoteCommit[], | ||
commitCustomFormat?: string | boolean | ||
) { | ||
gitRawCommitsOpts.format = GIT_COMMIT_WITH_AUTHOR_FORMAT; | ||
const extraCommitMsg = | ||
typeof commitCustomFormat === 'string' | ||
? commitCustomFormat | ||
.replace(/%a/g, '{{authorName}}' || '') | ||
.replace(/%e/g, '{{authorEmail}}' || '') | ||
.replace(/%l/g, '{{userLogin}}' || '') | ||
: `(@{{userLogin}})`; | ||
writerOpts.commitPartial = | ||
config.writerOpts.commitPartial!.replace(/\n*$/, '') + ` {{#if @root.linkReferences~}}${extraCommitMsg}{{~/if}}\n`; | ||
|
||
// add commits since last release inte the transform function | ||
writerOpts.transform = writerOptsTransform.bind( | ||
null, | ||
config.writerOpts.transform as (cmt: Commit, ctx: Context) => Commit, | ||
commitsSinceLastRelease | ||
); | ||
} | ||
|
||
/** | ||
* Extend the writerOpts transform function from whichever preset config is loaded | ||
* We will execute the original writerOpts transform function, then from it we'll add extra properties to the commit object | ||
* @param {Transform} originalTransform | ||
* @param {RemoteCommit[]} commitsSinceLastRelease | ||
* @param {Commit} commit | ||
* @param {Context} context | ||
* @returns | ||
*/ | ||
export function writerOptsTransform( | ||
originalTransform: (cmt: Commit, ctx: Context) => Commit, | ||
commitsSinceLastRelease: RemoteCommit[], | ||
commit: Commit, | ||
context: Context | ||
) { | ||
// execute original writerOpts transform | ||
const extendedCommit = originalTransform(commit, context); | ||
|
||
// add client remote detail (login) | ||
if (extendedCommit) { | ||
const remoteCommit = commitsSinceLastRelease.find((c) => c.shortHash === commit.shortHash); | ||
if (remoteCommit?.login) { | ||
commit.userLogin = remoteCommit.login; | ||
} | ||
} | ||
|
||
return extendedCommit; | ||
} |
Oops, something went wrong.