Skip to content

Commit

Permalink
BREAKING: Bump prettier to ^3.3.3 (#202)
Browse files Browse the repository at this point in the history
* Bump `prettier` to `^3.3.3`

* Update APIs to support Prettier 3

* Fix engines field

* Set Prettier peer dependency range to `>=3.0.0`
  • Loading branch information
Mrtenz authored Sep 23, 2024
1 parent b07f539 commit 0a736b6
Show file tree
Hide file tree
Showing 12 changed files with 1,347 additions and 2,703 deletions.
1 change: 1 addition & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ module.exports = {
singleQuote: true,
tabWidth: 2,
trailingComma: 'all',
plugins: ['prettier-plugin-packagejson'],
};
56 changes: 30 additions & 26 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,31 @@
"name": "@metamask/auto-changelog",
"version": "3.4.4",
"description": "Utilities for validating and updating \"Keep a Changelog\" formatted changelogs",
"publishConfig": {
"registry": "https://registry.npmjs.org/",
"access": "public"
"repository": {
"type": "git",
"url": "https://github.com/MetaMask/auto-changelog.git"
},
"license": "(MIT OR Apache-2.0)",
"main": "dist/index.js",
"bin": "dist/cli.js",
"engines": {
"node": "^18.18 || >=20"
},
"files": [
"dist/"
],
"repository": {
"type": "git",
"url": "https://github.com/MetaMask/auto-changelog.git"
},
"license": "(MIT OR Apache-2.0)",
"scripts": {
"test": "jest",
"test:watch": "jest --watch",
"prepack": "./scripts/prepack.sh",
"lint:eslint": "eslint . --cache --ext js,ts",
"lint:misc": "prettier '**/*.json' '**/*.md' '!CHANGELOG.md' '**/*.yml' '!.yarnrc.yml' --ignore-path .gitignore",
"build": "tsc --project tsconfig.build.json",
"build:clean": "rimraf dist && yarn build",
"changelog": "node dist/cli.js",
"lint": "yarn lint:eslint && yarn lint:misc --check",
"lint:eslint": "eslint . --cache --ext js,ts",
"lint:fix": "yarn lint:eslint --fix && yarn lint:misc --write",
"build:clean": "rimraf dist && yarn build",
"build": "tsc --project tsconfig.build.json",
"changelog": "node dist/cli.js"
"lint:misc": "prettier '**/*.json' '**/*.md' '!CHANGELOG.md' '**/*.yml' '!.yarnrc.yml' --ignore-path .gitignore",
"prepack": "./scripts/prepack.sh",
"test": "jest",
"test:watch": "jest --watch"
},
"dependencies": {
"diff": "^5.0.0",
"execa": "^5.1.1",
"prettier": "^2.8.8",
"semver": "^7.3.5",
"yargs": "^17.0.1"
},
Expand All @@ -52,22 +44,34 @@
"@typescript-eslint/eslint-plugin": "^5.42.1",
"@typescript-eslint/parser": "^5.42.1",
"eslint": "^8.40.0",
"eslint-config-prettier": "^8.5.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jest": "^27.1.5",
"eslint-plugin-jsdoc": "^39.6.2",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.2.1",
"jest": "^26.4.2",
"eslint-plugin-prettier": "^5.2.1",
"jest": "^29.7.0",
"outdent": "^0.8.0",
"prettier": "^3.3.3",
"prettier-plugin-packagejson": "^2.5.2",
"rimraf": "^3.0.2",
"ts-jest": "^26.5.6",
"ts-jest": "^29.2.5",
"typescript": "~4.8.4"
},
"peerDependencies": {
"prettier": ">=3.0.0"
},
"packageManager": "yarn@3.2.4",
"engines": {
"node": "^18.18 || >=20"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org/"
},
"lavamoat": {
"allowScripts": {
"@lavamoat/preinstall-always-fail": false
}
},
"packageManager": "yarn@3.2.4"
}
}
10 changes: 6 additions & 4 deletions src/changelog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
`;

describe('Changelog', () => {
it('should allow creating an empty changelog', () => {
it('should allow creating an empty changelog', async () => {
const changelog = new Changelog({
repoUrl: 'fake://metamask.io',
});
expect(changelog.toString()).toStrictEqual(emptyChangelog);

expect(await changelog.toString()).toStrictEqual(emptyChangelog);
});

it('should allow creating an empty changelog with a custom tag prefix', () => {
it('should allow creating an empty changelog with a custom tag prefix', async () => {
const changelog = new Changelog({
repoUrl: 'fake://metamask.io',
tagPrefix: 'example@v',
});
expect(changelog.toString()).toStrictEqual(emptyChangelog);

expect(await changelog.toString()).toStrictEqual(emptyChangelog);
});
});
21 changes: 18 additions & 3 deletions src/changelog.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as markdown from 'prettier/plugins/markdown';
import { format as formatWithPrettier } from 'prettier/standalone';
import semver from 'semver';

import {
Expand All @@ -8,6 +10,19 @@ import {
} from './constants';
import { PackageRename } from './shared-types';

/**
* Format a Markdown changelog string.
*
* @param changelog - The changelog string to format.
* @returns The formatted changelog string.
*/
export async function format(changelog: string): Promise<string> {
return formatWithPrettier(changelog, {
parser: 'markdown',
plugins: [markdown],
});
}

/**
* `Object.getOwnPropertyNames()` is intentionally generic: it returns the
* immediate property names of an object, but it cannot make guarantees about
Expand Down Expand Up @@ -37,7 +52,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
/**
* Formatter function that formats a Markdown changelog string.
*/
export type Formatter = (changelog: string) => string;
export type Formatter = (changelog: string) => string | Promise<string>;

type ReleaseMetadata = {
/**
Expand Down Expand Up @@ -571,7 +586,7 @@ export default class Changelog {
*
* @returns The stringified changelog.
*/
toString(): string {
async toString(): Promise<string> {
const changelog = `${changelogTitle}
${changelogDescription}
Expand All @@ -584,6 +599,6 @@ ${stringifyLinkReferenceDefinitions(
this.#packageRename,
)}`;

return this.#formatter(changelog);
return await this.#formatter(changelog);
}
}
13 changes: 5 additions & 8 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

import { promises as fs, constants as fsConstants } from 'fs';
import path from 'path';
import prettier from 'prettier';
import semver from 'semver';
import type { Argv } from 'yargs';
import { hideBin } from 'yargs/helpers';
import yargs from 'yargs/yargs';

import { Formatter } from './changelog';
import { format, Formatter } from './changelog';
import { unreleased, Version } from './constants';
import { generateDiff } from './generate-diff';
import { createEmptyChangelog } from './init';
Expand Down Expand Up @@ -184,7 +183,7 @@ async function validate({
const changelogContent = await readChangelog(changelogPath);

try {
validateChangelog({
await validateChangelog({
changelogContent,
currentVersion,
repoUrl,
Expand Down Expand Up @@ -239,7 +238,7 @@ type InitOptions = {
* @param options.tagPrefix - The prefix used in tags before the version number.
*/
async function init({ changelogPath, repoUrl, tagPrefix }: InitOptions) {
const changelogContent = createEmptyChangelog({ repoUrl, tagPrefix });
const changelogContent = await createEmptyChangelog({ repoUrl, tagPrefix });
await saveChangelog(changelogPath, changelogContent);
}

Expand Down Expand Up @@ -466,10 +465,8 @@ async function main() {
}
}

const formatter = (changelog: string) => {
return usePrettier
? prettier.format(changelog, { parser: 'markdown' })
: changelog;
const formatter = async (changelog: string) => {
return usePrettier ? await format(changelog) : changelog;
};

if (command === 'update') {
Expand Down
2 changes: 1 addition & 1 deletion src/generate-diff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ const testCases = [

describe('generateDiff', () => {
for (const { description, before, after, expected } of testCases) {
it(`${description}`, () => {
it(`${description}`, async () => {
const diff = generateDiff(before, after);
expect(diff).toStrictEqual(expected);
});
Expand Down
13 changes: 7 additions & 6 deletions src/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createEmptyChangelog } from './init';

const exampleRepoUrl =
'https://github.com/ExampleUsernameOrOrganization/ExampleRepository/';

const emptyChangelog = `# Changelog
All notable changes to this project will be documented in this file.
Expand All @@ -14,15 +15,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
`;

describe('createEmptyChangelog', () => {
it('creates an empty changelog', () => {
expect(createEmptyChangelog({ repoUrl: exampleRepoUrl })).toStrictEqual(
emptyChangelog,
);
it('creates an empty changelog', async () => {
expect(
await createEmptyChangelog({ repoUrl: exampleRepoUrl }),
).toStrictEqual(emptyChangelog);
});

it('creates an empty changelog with a custom tag prefix', () => {
it('creates an empty changelog with a custom tag prefix', async () => {
expect(
createEmptyChangelog({ repoUrl: exampleRepoUrl, tagPrefix: 'foo' }),
await createEmptyChangelog({ repoUrl: exampleRepoUrl, tagPrefix: 'foo' }),
).toStrictEqual(emptyChangelog);
});
});
4 changes: 2 additions & 2 deletions src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import Changelog from './changelog';
* @param options.tagPrefix - The prefix used in tags before the version number.
* @returns The initial changelog text.
*/
export function createEmptyChangelog({
export async function createEmptyChangelog({
repoUrl,
tagPrefix = 'v',
}: {
repoUrl: string;
tagPrefix?: string;
}) {
const changelog = new Changelog({ repoUrl, tagPrefix });
return changelog.toString();
return await changelog.toString();
}
2 changes: 1 addition & 1 deletion src/update-changelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ export async function updateChangelog({
});
}

const newChangelogContent = changelog.toString();
const newChangelogContent = await changelog.toString();
const isChangelogUpdated = changelogContent !== newChangelogContent;
return isChangelogUpdated ? newChangelogContent : undefined;
}
Expand Down
Loading

0 comments on commit 0a736b6

Please sign in to comment.