Skip to content

Commit c9257ac

Browse files
authored
BREAKING: Format changelog using Prettier (#100)
1 parent f969b0e commit c9257ac

File tree

8 files changed

+119
-500
lines changed

8 files changed

+119
-500
lines changed

jest.config.cjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
/**
77
* Dependencies that are ESM-only, and need to be transpiled by Babel.
88
* This list is used in the `transformIgnorePatterns` option below.
9-
*
9+
*
1010
* You probably need to add a dependency to this list if the tests fail with something like:
1111
* - `SyntaxError: Cannot use import statement outside a module`
1212
* - `SyntaxError: Unexpected token 'export'`
1313
* If so, identify the dependency that's causing the error via the stack trace, and add it
1414
* to this list.
15-
*
15+
*
1616
* No, we do not live in the best of all possible worlds. Why do you ask?
1717
*
1818
* For details on Jest's currently experimental ESM support see: https://github.com/jestjs/jest/issues/9430

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"@types/jest": "^29.5.10",
5151
"@types/jest-when": "^3.5.2",
5252
"@types/node": "^17.0.23",
53+
"@types/prettier": "^2.7.3",
5354
"@types/rimraf": "^4.0.5",
5455
"@types/which": "^3.0.0",
5556
"@types/yargs": "^17.0.10",
@@ -75,6 +76,9 @@
7576
"tsx": "^4.6.1",
7677
"typescript": "~5.1.6"
7778
},
79+
"peerDependencies": {
80+
"prettier": "^2"
81+
},
7882
"packageManager": "[email protected]",
7983
"engines": {
8084
"node": "^16.20 || ^18.16 || >=20"

src/functional.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,9 @@ describe('create-release-branch (functional)', () => {
302302
## [Unreleased]
303303
304304
## [2.0.0]
305+
305306
### Uncategorized
307+
306308
- Update "a"
307309
- Initial commit
308310
@@ -317,7 +319,9 @@ describe('create-release-branch (functional)', () => {
317319
## [Unreleased]
318320
319321
## [2.0.0]
322+
320323
### Uncategorized
324+
321325
- Initial commit
322326
323327
[Unreleased]: https://github.com/example-org/example-repo/compare/@scope/[email protected]
@@ -362,7 +366,9 @@ describe('create-release-branch (functional)', () => {
362366
## [Unreleased]
363367
364368
## [1.0.0]
369+
365370
### Added
371+
366372
- Initial release
367373
368374
[Unreleased]: https://github.com/example-org/example-repo/compare/@scope/[email protected]
@@ -376,7 +382,9 @@ describe('create-release-branch (functional)', () => {
376382
## [Unreleased]
377383
378384
## [1.0.0]
385+
379386
### Added
387+
380388
- Initial release
381389
382390
[Unreleased]: https://github.com/example-org/example-repo/compare/@scope/[email protected]
@@ -413,11 +421,15 @@ describe('create-release-branch (functional)', () => {
413421
## [Unreleased]
414422
415423
## [2.0.0]
424+
416425
### Uncategorized
426+
417427
- Update "a"
418428
419429
## [1.0.0]
430+
420431
### Added
432+
421433
- Initial release
422434
423435
[Unreleased]: https://github.com/example-org/example-repo/compare/@scope/[email protected]
@@ -432,7 +444,9 @@ describe('create-release-branch (functional)', () => {
432444
## [Unreleased]
433445
434446
## [1.0.0]
447+
435448
### Added
449+
436450
- Initial release
437451
438452
[Unreleased]: https://github.com/example-org/example-repo/compare/@scope/[email protected]
@@ -486,7 +500,9 @@ describe('create-release-branch (functional)', () => {
486500
## [Unreleased]
487501
488502
## [1.0.0]
503+
489504
### Added
505+
490506
- Initial release
491507
492508
[Unreleased]: https://github.com/example-org/example-repo/compare/@scope/[email protected]
@@ -522,7 +538,9 @@ describe('create-release-branch (functional)', () => {
522538
## [Unreleased]
523539
524540
## [1.0.0]
541+
525542
### Uncategorized
543+
526544
- Update "a"
527545
528546
[Unreleased]: https://github.com/example-org/example-repo/compare/@scope/[email protected]
@@ -536,7 +554,9 @@ describe('create-release-branch (functional)', () => {
536554
## [Unreleased]
537555
538556
## [1.0.0]
557+
539558
### Added
559+
540560
- Initial release
541561
542562
[Unreleased]: https://github.com/example-org/example-repo/compare/@scope/[email protected]
@@ -773,7 +793,9 @@ describe('create-release-branch (functional)', () => {
773793
## [Unreleased]
774794
775795
## [2.0.0]
796+
776797
### Uncategorized
798+
777799
- Update "a"
778800
- Initial commit
779801

src/package.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
createNoopWriteStream,
1313
} from '../tests/unit/helpers.js';
1414
import {
15+
formatChangelog,
1516
readMonorepoRootPackage,
1617
readMonorepoWorkspacePackage,
1718
updatePackage,
@@ -494,11 +495,15 @@ describe('package', () => {
494495
## [Unreleased]
495496
496497
## [2.0.0]
498+
497499
### Uncategorized
500+
498501
- Add isNewFunction ([#2](https://repo.url/compare/package/pull/2))
499502
500503
## [1.0.0] - 2020-01-01
504+
501505
### Changed
506+
502507
- Something else
503508
504509
[Unreleased]: https://repo.url/compare/[email protected]
@@ -572,6 +577,7 @@ describe('package', () => {
572577
projectRootDirectory: sandbox.directoryPath,
573578
repoUrl: 'https://repo.url',
574579
tagPrefixes: ['package@', 'v'],
580+
formatter: formatChangelog,
575581
})
576582
.mockResolvedValue('new changelog');
577583
await fs.promises.writeFile(changelogPath, 'existing changelog');
@@ -610,6 +616,7 @@ describe('package', () => {
610616
projectRootDirectory: sandbox.directoryPath,
611617
repoUrl: 'https://repo.url',
612618
tagPrefixes: ['package@', 'v'],
619+
formatter: formatChangelog,
613620
})
614621
.mockResolvedValue(undefined);
615622
await fs.promises.writeFile(changelogPath, 'existing changelog');
@@ -673,4 +680,30 @@ describe('package', () => {
673680
});
674681
});
675682
});
683+
684+
describe('formatChangelog', () => {
685+
it('formats a changelog', () => {
686+
const unformattedChangelog = `# Changelog
687+
## 1.0.0
688+
689+
- Some change
690+
## 0.0.1
691+
692+
- Some other change
693+
`;
694+
695+
expect(formatChangelog(unformattedChangelog)).toMatchInlineSnapshot(`
696+
"# Changelog
697+
698+
## 1.0.0
699+
700+
- Some change
701+
702+
## 0.0.1
703+
704+
- Some other change
705+
"
706+
`);
707+
});
708+
});
676709
});

src/package.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fs, { WriteStream } from 'fs';
22
import path from 'path';
33
import { format } from 'util';
44
import { parseChangelog, updateChangelog } from '@metamask/auto-changelog';
5+
import prettier from 'prettier';
56
import { WriteStreamLike, readFile, writeFile, writeJsonFile } from './fs.js';
67
import { isErrorWithCode } from './misc-utils.js';
78
import {
@@ -272,13 +273,25 @@ export async function migrateUnreleasedChangelogChangesToRelease({
272273
changelogContent,
273274
repoUrl: repositoryUrl,
274275
tagPrefix: `${pkg.validatedManifest.name}@`,
276+
formatter: formatChangelog,
275277
});
276278

277279
changelog.addRelease({ version });
278280
changelog.migrateUnreleasedChangesToRelease(version);
279281
await writeFile(pkg.changelogPath, changelog.toString());
280282
}
281283

284+
/**
285+
* Format the given changelog using Prettier. This is extracted into a separate
286+
* function for coverage purposes.
287+
*
288+
* @param changelog - The changelog to format.
289+
* @returns The formatted changelog.
290+
*/
291+
export function formatChangelog(changelog: string) {
292+
return prettier.format(changelog, { parser: 'markdown' });
293+
}
294+
282295
/**
283296
* Updates the changelog file of the given package using
284297
* `@metamask/auto-changelog`. Assumes that the changelog file is located at the
@@ -323,6 +336,7 @@ export async function updatePackageChangelog({
323336
projectRootDirectory: pkg.directoryPath,
324337
repoUrl: repositoryUrl,
325338
tagPrefixes: [`${pkg.validatedManifest.name}@`, 'v'],
339+
formatter: formatChangelog,
326340
});
327341

328342
if (newChangelogContent) {

src/project.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import path from 'path';
33
import { when } from 'jest-when';
44
import { SemVer } from 'semver';
55
import * as actionUtils from '@metamask/action-utils';
6-
import { withSandbox } from '../tests/helpers';
6+
import { withSandbox } from '../tests/helpers.js';
77
import {
88
buildMockPackage,
99
buildMockProject,
1010
createNoopWriteStream,
11-
} from '../tests/unit/helpers';
11+
} from '../tests/unit/helpers.js';
1212
import {
1313
readProject,
1414
restoreChangelogsForSkippedPackages,

tests/helpers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ export function normalizeMultilineString(string: string): string {
126126
export function buildChangelog(variantContent: string): string {
127127
const invariantContent = normalizeMultilineString(`
128128
# Changelog
129+
129130
All notable changes to this project will be documented in this file.
130131
131132
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

0 commit comments

Comments
 (0)