Skip to content

Commit e210a0c

Browse files
committed
Update APIs to support Prettier 3
1 parent e03682c commit e210a0c

File tree

11 files changed

+1349
-2440
lines changed

11 files changed

+1349
-2440
lines changed

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,18 @@
4444
"@typescript-eslint/eslint-plugin": "^5.42.1",
4545
"@typescript-eslint/parser": "^5.42.1",
4646
"eslint": "^8.40.0",
47-
"eslint-config-prettier": "^8.5.0",
47+
"eslint-config-prettier": "^9.1.0",
4848
"eslint-plugin-import": "^2.26.0",
4949
"eslint-plugin-jest": "^27.1.5",
5050
"eslint-plugin-jsdoc": "^39.6.2",
5151
"eslint-plugin-node": "^11.1.0",
52-
"eslint-plugin-prettier": "^4.2.1",
53-
"jest": "^26.4.2",
52+
"eslint-plugin-prettier": "^5.2.1",
53+
"jest": "^29.7.0",
5454
"outdent": "^0.8.0",
5555
"prettier": "^3.3.3",
5656
"prettier-plugin-packagejson": "^2.5.2",
5757
"rimraf": "^3.0.2",
58-
"ts-jest": "^26.5.6",
58+
"ts-jest": "^29.2.5",
5959
"typescript": "~4.8.4"
6060
},
6161
"peerDependencies": {

src/changelog.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
`;
1313

1414
describe('Changelog', () => {
15-
it('should allow creating an empty changelog', () => {
15+
it('should allow creating an empty changelog', async () => {
1616
const changelog = new Changelog({
1717
repoUrl: 'fake://metamask.io',
1818
});
19-
expect(changelog.toString()).toStrictEqual(emptyChangelog);
19+
20+
expect(await changelog.toString()).toStrictEqual(emptyChangelog);
2021
});
2122

22-
it('should allow creating an empty changelog with a custom tag prefix', () => {
23+
it('should allow creating an empty changelog with a custom tag prefix', async () => {
2324
const changelog = new Changelog({
2425
repoUrl: 'fake://metamask.io',
2526
tagPrefix: 'example@v',
2627
});
27-
expect(changelog.toString()).toStrictEqual(emptyChangelog);
28+
29+
expect(await changelog.toString()).toStrictEqual(emptyChangelog);
2830
});
2931
});

src/changelog.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as markdown from 'prettier/plugins/markdown';
2+
import { format as formatWithPrettier } from 'prettier/standalone';
13
import semver from 'semver';
24

35
import {
@@ -8,6 +10,19 @@ import {
810
} from './constants';
911
import { PackageRename } from './shared-types';
1012

13+
/**
14+
* Format a Markdown changelog string.
15+
*
16+
* @param changelog - The changelog string to format.
17+
* @returns The formatted changelog string.
18+
*/
19+
export async function format(changelog: string): Promise<string> {
20+
return formatWithPrettier(changelog, {
21+
parser: 'markdown',
22+
plugins: [markdown],
23+
});
24+
}
25+
1126
/**
1227
* `Object.getOwnPropertyNames()` is intentionally generic: it returns the
1328
* immediate property names of an object, but it cannot make guarantees about
@@ -37,7 +52,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3752
/**
3853
* Formatter function that formats a Markdown changelog string.
3954
*/
40-
export type Formatter = (changelog: string) => string;
55+
export type Formatter = (changelog: string) => string | Promise<string>;
4156

4257
type ReleaseMetadata = {
4358
/**
@@ -571,7 +586,7 @@ export default class Changelog {
571586
*
572587
* @returns The stringified changelog.
573588
*/
574-
toString(): string {
589+
async toString(): Promise<string> {
575590
const changelog = `${changelogTitle}
576591
${changelogDescription}
577592
@@ -584,6 +599,6 @@ ${stringifyLinkReferenceDefinitions(
584599
this.#packageRename,
585600
)}`;
586601

587-
return this.#formatter(changelog);
602+
return await this.#formatter(changelog);
588603
}
589604
}

src/cli.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
import { promises as fs, constants as fsConstants } from 'fs';
44
import path from 'path';
5-
import prettier from 'prettier';
65
import semver from 'semver';
76
import type { Argv } from 'yargs';
87
import { hideBin } from 'yargs/helpers';
98
import yargs from 'yargs/yargs';
109

11-
import { Formatter } from './changelog';
10+
import { format, Formatter } from './changelog';
1211
import { unreleased, Version } from './constants';
1312
import { generateDiff } from './generate-diff';
1413
import { createEmptyChangelog } from './init';
@@ -184,7 +183,7 @@ async function validate({
184183
const changelogContent = await readChangelog(changelogPath);
185184

186185
try {
187-
validateChangelog({
186+
await validateChangelog({
188187
changelogContent,
189188
currentVersion,
190189
repoUrl,
@@ -239,7 +238,7 @@ type InitOptions = {
239238
* @param options.tagPrefix - The prefix used in tags before the version number.
240239
*/
241240
async function init({ changelogPath, repoUrl, tagPrefix }: InitOptions) {
242-
const changelogContent = createEmptyChangelog({ repoUrl, tagPrefix });
241+
const changelogContent = await createEmptyChangelog({ repoUrl, tagPrefix });
243242
await saveChangelog(changelogPath, changelogContent);
244243
}
245244

@@ -466,10 +465,8 @@ async function main() {
466465
}
467466
}
468467

469-
const formatter = (changelog: string) => {
470-
return usePrettier
471-
? prettier.format(changelog, { parser: 'markdown' })
472-
: changelog;
468+
const formatter = async (changelog: string) => {
469+
return usePrettier ? await format(changelog) : changelog;
473470
};
474471

475472
if (command === 'update') {

src/generate-diff.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ const testCases = [
318318

319319
describe('generateDiff', () => {
320320
for (const { description, before, after, expected } of testCases) {
321-
it(`${description}`, () => {
321+
it(`${description}`, async () => {
322322
const diff = generateDiff(before, after);
323323
expect(diff).toStrictEqual(expected);
324324
});

src/init.test.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { createEmptyChangelog } from './init';
22

33
const exampleRepoUrl =
44
'https://github.com/ExampleUsernameOrOrganization/ExampleRepository/';
5+
56
const emptyChangelog = `# Changelog
67
All notable changes to this project will be documented in this file.
78
@@ -14,15 +15,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1415
`;
1516

1617
describe('createEmptyChangelog', () => {
17-
it('creates an empty changelog', () => {
18-
expect(createEmptyChangelog({ repoUrl: exampleRepoUrl })).toStrictEqual(
19-
emptyChangelog,
20-
);
18+
it('creates an empty changelog', async () => {
19+
expect(
20+
await createEmptyChangelog({ repoUrl: exampleRepoUrl }),
21+
).toStrictEqual(emptyChangelog);
2122
});
2223

23-
it('creates an empty changelog with a custom tag prefix', () => {
24+
it('creates an empty changelog with a custom tag prefix', async () => {
2425
expect(
25-
createEmptyChangelog({ repoUrl: exampleRepoUrl, tagPrefix: 'foo' }),
26+
await createEmptyChangelog({ repoUrl: exampleRepoUrl, tagPrefix: 'foo' }),
2627
).toStrictEqual(emptyChangelog);
2728
});
2829
});

src/init.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import Changelog from './changelog';
88
* @param options.tagPrefix - The prefix used in tags before the version number.
99
* @returns The initial changelog text.
1010
*/
11-
export function createEmptyChangelog({
11+
export async function createEmptyChangelog({
1212
repoUrl,
1313
tagPrefix = 'v',
1414
}: {
1515
repoUrl: string;
1616
tagPrefix?: string;
1717
}) {
1818
const changelog = new Changelog({ repoUrl, tagPrefix });
19-
return changelog.toString();
19+
return await changelog.toString();
2020
}

src/update-changelog.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ export async function updateChangelog({
310310
});
311311
}
312312

313-
const newChangelogContent = changelog.toString();
313+
const newChangelogContent = await changelog.toString();
314314
const isChangelogUpdated = changelogContent !== newChangelogContent;
315315
return isChangelogUpdated ? newChangelogContent : undefined;
316316
}

0 commit comments

Comments
 (0)