Skip to content

Commit

Permalink
BREAKING: Format changelog using Prettier (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrtenz authored Dec 10, 2023
1 parent f969b0e commit c9257ac
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 500 deletions.
4 changes: 2 additions & 2 deletions jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
/**
* Dependencies that are ESM-only, and need to be transpiled by Babel.
* This list is used in the `transformIgnorePatterns` option below.
*
*
* You probably need to add a dependency to this list if the tests fail with something like:
* - `SyntaxError: Cannot use import statement outside a module`
* - `SyntaxError: Unexpected token 'export'`
* If so, identify the dependency that's causing the error via the stack trace, and add it
* to this list.
*
*
* No, we do not live in the best of all possible worlds. Why do you ask?
*
* For details on Jest's currently experimental ESM support see: https://github.com/jestjs/jest/issues/9430
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@types/jest": "^29.5.10",
"@types/jest-when": "^3.5.2",
"@types/node": "^17.0.23",
"@types/prettier": "^2.7.3",
"@types/rimraf": "^4.0.5",
"@types/which": "^3.0.0",
"@types/yargs": "^17.0.10",
Expand All @@ -75,6 +76,9 @@
"tsx": "^4.6.1",
"typescript": "~5.1.6"
},
"peerDependencies": {
"prettier": "^2"
},
"packageManager": "[email protected]",
"engines": {
"node": "^16.20 || ^18.16 || >=20"
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 @@ -773,7 +793,9 @@ describe('create-release-branch (functional)', () => {
## [Unreleased]
## [2.0.0]
### Uncategorized
- Update "a"
- Initial commit
Expand Down
33 changes: 33 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.js';
import {
formatChangelog,
readMonorepoRootPackage,
readMonorepoWorkspacePackage,
updatePackage,
Expand Down Expand Up @@ -494,11 +495,15 @@ describe('package', () => {
## [Unreleased]
## [2.0.0]
### Uncategorized
- Add isNewFunction ([#2](https://repo.url/compare/package/pull/2))
## [1.0.0] - 2020-01-01
### Changed
- Something else
[Unreleased]: https://repo.url/compare/[email protected]
Expand Down Expand Up @@ -572,6 +577,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 @@ -610,6 +616,7 @@ describe('package', () => {
projectRootDirectory: sandbox.directoryPath,
repoUrl: 'https://repo.url',
tagPrefixes: ['package@', 'v'],
formatter: formatChangelog,
})
.mockResolvedValue(undefined);
await fs.promises.writeFile(changelogPath, 'existing changelog');
Expand Down Expand Up @@ -673,4 +680,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
"
`);
});
});
});
14 changes: 14 additions & 0 deletions src/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs, { WriteStream } from 'fs';
import path from 'path';
import { format } from 'util';
import { parseChangelog, updateChangelog } from '@metamask/auto-changelog';
import prettier from 'prettier';
import { WriteStreamLike, readFile, writeFile, writeJsonFile } from './fs.js';
import { isErrorWithCode } from './misc-utils.js';
import {
Expand Down Expand Up @@ -272,13 +273,25 @@ export async function migrateUnreleasedChangelogChangesToRelease({
changelogContent,
repoUrl: repositoryUrl,
tagPrefix: `${pkg.validatedManifest.name}@`,
formatter: formatChangelog,
});

changelog.addRelease({ version });
changelog.migrateUnreleasedChangesToRelease(version);
await writeFile(pkg.changelogPath, changelog.toString());
}

/**
* 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 @@ -323,6 +336,7 @@ export async function updatePackageChangelog({
projectRootDirectory: pkg.directoryPath,
repoUrl: repositoryUrl,
tagPrefixes: [`${pkg.validatedManifest.name}@`, 'v'],
formatter: formatChangelog,
});

if (newChangelogContent) {
Expand Down
4 changes: 2 additions & 2 deletions src/project.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import path from 'path';
import { when } from 'jest-when';
import { SemVer } from 'semver';
import * as actionUtils from '@metamask/action-utils';
import { withSandbox } from '../tests/helpers';
import { withSandbox } from '../tests/helpers.js';
import {
buildMockPackage,
buildMockProject,
createNoopWriteStream,
} from '../tests/unit/helpers';
} from '../tests/unit/helpers.js';
import {
readProject,
restoreChangelogsForSkippedPackages,
Expand Down
1 change: 1 addition & 0 deletions tests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export function normalizeMultilineString(string: string): string {
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
Loading

0 comments on commit c9257ac

Please sign in to comment.