Skip to content

Commit

Permalink
Format changelog using Prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrtenz committed Sep 28, 2023
1 parent 73f60d0 commit 1e113d0
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
},
"dependencies": {
"@metamask/action-utils": "^1.0.0",
"@metamask/auto-changelog": "^3.3.0",
"@metamask/utils": "^5.0.2",
"debug": "^4.3.4",
"execa": "^5.1.1",
Expand All @@ -35,7 +36,6 @@
},
"devDependencies": {
"@lavamoat/allow-scripts": "^2.3.1",
"@metamask/auto-changelog": "^3.0.0",
"@metamask/eslint-config": "^10.0.0",
"@metamask/eslint-config-jest": "^10.0.0",
"@metamask/eslint-config-nodejs": "^10.0.0",
Expand Down
22 changes: 22 additions & 0 deletions src/functional.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,9 @@ describe('create-release-branch (functional)', () => {
## [Unreleased]
## [2.0.0]
### Uncategorized
- Update "a"
- Initial commit
Expand All @@ -317,7 +319,9 @@ describe('create-release-branch (functional)', () => {
## [Unreleased]
## [2.0.0]
### Uncategorized
- Initial commit
[Unreleased]: https://github.com/example-org/example-repo/compare/@scope/[email protected]
Expand Down Expand Up @@ -362,7 +366,9 @@ describe('create-release-branch (functional)', () => {
## [Unreleased]
## [1.0.0]
### Added
- Initial release
[Unreleased]: https://github.com/example-org/example-repo/compare/@scope/[email protected]
Expand All @@ -376,7 +382,9 @@ describe('create-release-branch (functional)', () => {
## [Unreleased]
## [1.0.0]
### Added
- Initial release
[Unreleased]: https://github.com/example-org/example-repo/compare/@scope/[email protected]
Expand Down Expand Up @@ -413,11 +421,15 @@ describe('create-release-branch (functional)', () => {
## [Unreleased]
## [2.0.0]
### Uncategorized
- Update "a"
## [1.0.0]
### Added
- Initial release
[Unreleased]: https://github.com/example-org/example-repo/compare/@scope/[email protected]
Expand All @@ -432,7 +444,9 @@ describe('create-release-branch (functional)', () => {
## [Unreleased]
## [1.0.0]
### Added
- Initial release
[Unreleased]: https://github.com/example-org/example-repo/compare/@scope/[email protected]
Expand Down Expand Up @@ -486,7 +500,9 @@ describe('create-release-branch (functional)', () => {
## [Unreleased]
## [1.0.0]
### Added
- Initial release
[Unreleased]: https://github.com/example-org/example-repo/compare/@scope/[email protected]
Expand Down Expand Up @@ -522,7 +538,9 @@ describe('create-release-branch (functional)', () => {
## [Unreleased]
## [1.0.0]
### Uncategorized
- Update "a"
[Unreleased]: https://github.com/example-org/example-repo/compare/@scope/[email protected]
Expand All @@ -536,7 +554,9 @@ describe('create-release-branch (functional)', () => {
## [Unreleased]
## [1.0.0]
### Added
- Initial release
[Unreleased]: https://github.com/example-org/example-repo/compare/@scope/[email protected]
Expand Down Expand Up @@ -869,7 +889,9 @@ The release spec file has been retained for you to edit again and make the neces
## [Unreleased]
## [2.0.0]
### Uncategorized
- Update "a"
- Initial commit
Expand Down
28 changes: 28 additions & 0 deletions src/package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
createNoopWriteStream,
} from '../tests/unit/helpers';
import {
formatChangelog,
readMonorepoRootPackage,
readMonorepoWorkspacePackage,
updatePackage,
Expand Down Expand Up @@ -475,6 +476,7 @@ describe('package', () => {
projectRootDirectory: sandbox.directoryPath,
repoUrl: 'https://repo.url',
tagPrefixes: ['package@', 'v'],
formatter: formatChangelog,
})
.mockResolvedValue('new changelog');
await fs.promises.writeFile(changelogPath, 'existing changelog');
Expand Down Expand Up @@ -609,4 +611,30 @@ describe('package', () => {
});
});
});

describe('formatChangelog', () => {
it('formats a changelog', () => {
const unformattedChangelog = `# Changelog
## 1.0.0
- Some change
## 0.0.1
- Some other change
`;

expect(formatChangelog(unformattedChangelog)).toMatchInlineSnapshot(`
"# Changelog
## 1.0.0
- Some change
## 0.0.1
- Some other change
"
`);
});
});
});
13 changes: 13 additions & 0 deletions src/package.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs, { WriteStream } from 'fs';
import path from 'path';
import { format } from 'util';
import prettier from 'prettier';
import { updateChangelog } from '@metamask/auto-changelog';
import { WriteStreamLike, readFile, writeFile, writeJsonFile } from './fs';
import { isErrorWithCode } from './misc-utils';
Expand Down Expand Up @@ -229,6 +230,17 @@ export async function readMonorepoWorkspacePackage({
};
}

/**
* Format the given changelog using Prettier. This is extracted into a separate
* function for coverage purposes.
*
* @param changelog - The changelog to format.
* @returns The formatted changelog.
*/
export function formatChangelog(changelog: string) {
return prettier.format(changelog, { parser: 'markdown' });
}

/**
* Updates the changelog file of the given package using
* `@metamask/auto-changelog`. Assumes that the changelog file is located at the
Expand Down Expand Up @@ -272,6 +284,7 @@ async function updatePackageChangelog({
projectRootDirectory: pkg.directoryPath,
repoUrl: repositoryUrl,
tagPrefixes: [`${pkg.validatedManifest.name}@`, 'v'],
formatter: formatChangelog,
});

if (newChangelogContent) {
Expand Down
1 change: 1 addition & 0 deletions tests/functional/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export async function sleepFor(duration: number): Promise<void> {
export function buildChangelog(variantContent: string): string {
const invariantContent = normalizeMultilineString(`
# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
Expand Down
19 changes: 10 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -957,17 +957,18 @@ __metadata:
languageName: node
linkType: hard

"@metamask/auto-changelog@npm:^3.0.0":
version: 3.1.0
resolution: "@metamask/auto-changelog@npm:3.1.0"
"@metamask/auto-changelog@npm:^3.3.0":
version: 3.3.0
resolution: "@metamask/auto-changelog@npm:3.3.0"
dependencies:
diff: ^5.0.0
execa: ^5.1.1
prettier: ^2.8.8
semver: ^7.3.5
yargs: ^17.0.1
bin:
auto-changelog: dist/cli.js
checksum: cd3c833100c19a80e0b7ae8c174fbbe225700eef5fbabd9b0d6c4b439c8af839792111a17d9ddf2e1939839736413afd7402d2cc08ece373622b5a410da5e9fe
checksum: 1b308f0f6beafb479da19398bc51315888c8fa7e1688ee835a96d5e2ca8dccf2edac80ac835335122b2c3ff6c76f03d202a35316122bbfa7c071954d5227f732
languageName: node
linkType: hard

Expand All @@ -977,7 +978,7 @@ __metadata:
dependencies:
"@lavamoat/allow-scripts": ^2.3.1
"@metamask/action-utils": ^1.0.0
"@metamask/auto-changelog": ^3.0.0
"@metamask/auto-changelog": ^3.3.0
"@metamask/eslint-config": ^10.0.0
"@metamask/eslint-config-jest": ^10.0.0
"@metamask/eslint-config-nodejs": ^10.0.0
Expand Down Expand Up @@ -5045,12 +5046,12 @@ __metadata:
languageName: node
linkType: hard

"prettier@npm:^2.2.1":
version: 2.7.1
resolution: "prettier@npm:2.7.1"
"prettier@npm:^2.2.1, prettier@npm:^2.8.8":
version: 2.8.8
resolution: "prettier@npm:2.8.8"
bin:
prettier: bin-prettier.js
checksum: 55a4409182260866ab31284d929b3cb961e5fdb91fe0d2e099dac92eaecec890f36e524b4c19e6ceae839c99c6d7195817579cdffc8e2c80da0cb794463a748b
checksum: b49e409431bf129dd89238d64299ba80717b57ff5a6d1c1a8b1a28b590d998a34e083fa13573bc732bb8d2305becb4c9a4407f8486c81fa7d55100eb08263cf8
languageName: node
linkType: hard

Expand Down

0 comments on commit 1e113d0

Please sign in to comment.