Skip to content

Commit

Permalink
Fix an issue with not applying a custom .prettierrc configuration w…
Browse files Browse the repository at this point in the history
…ith `prettier@>= 3.1.1` (#1351)

* Fix prettier config resolving

* add changeset

* Resolve prettier config for each file changed

* Extended changeset

* Update .changeset/breezy-moles-behave.md

* Update .changeset/breezy-moles-behave.md

---------

Co-authored-by: bert.degeyter <bert.degeyter@showpad.com>
Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
  • Loading branch information
3 people committed May 17, 2024
1 parent 0bf89b3 commit c6da182
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 37 deletions.
7 changes: 7 additions & 0 deletions .changeset/breezy-moles-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@changesets/cli": patch
"@changesets/apply-release-plan": patch
"@changesets/write": patch
---

Fix an issue with not applying a custom `.prettierrc` configuration with `prettier@>= 3.1.1`
41 changes: 9 additions & 32 deletions packages/apply-release-plan/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ export default async function applyReleasePlan(
});

let prettierInstance = getPrettierInstance(cwd);
let prettierConfig = await prettierInstance.resolveConfig(cwd);

for (let release of finalisedRelease) {
let { changelog, packageJson, dir, name } = release;
Expand All @@ -144,13 +143,7 @@ export default async function applyReleasePlan(

if (changelog && changelog.length > 0) {
const changelogPath = path.resolve(dir, "CHANGELOG.md");
await updateChangelog(
changelogPath,
changelog,
name,
prettierInstance,
prettierConfig
);
await updateChangelog(changelogPath, changelog, name, prettierInstance);
touchedFiles.push(changelogPath);
}
}
Expand Down Expand Up @@ -273,26 +266,18 @@ async function updateChangelog(
changelogPath: string,
changelog: string,
name: string,
prettierInstance: typeof prettier,
prettierConfig: prettier.Options | null
prettierInstance: typeof prettier
) {
let templateString = `\n\n${changelog.trim()}\n`;

try {
if (fs.existsSync(changelogPath)) {
await prependFile(
changelogPath,
templateString,
name,
prettierInstance,
prettierConfig
);
await prependFile(changelogPath, templateString, name, prettierInstance);
} else {
await writeFormattedMarkdownFile(
changelogPath,
`# ${name}${templateString}`,
prettierInstance,
prettierConfig
prettierInstance
);
}
} catch (e) {
Expand All @@ -316,8 +301,7 @@ async function prependFile(
filePath: string,
data: string,
name: string,
prettierInstance: typeof prettier,
prettierConfig: prettier.Options | null
prettierInstance: typeof prettier
) {
const fileData = fs.readFileSync(filePath).toString();
// if the file exists but doesn't have the header, we'll add it in
Expand All @@ -326,32 +310,25 @@ async function prependFile(
await writeFormattedMarkdownFile(
filePath,
completelyNewChangelog,
prettierInstance,
prettierConfig
prettierInstance
);
return;
}
const newChangelog = fileData.replace("\n", data);

await writeFormattedMarkdownFile(
filePath,
newChangelog,
prettierInstance,
prettierConfig
);
await writeFormattedMarkdownFile(filePath, newChangelog, prettierInstance);
}

async function writeFormattedMarkdownFile(
filePath: string,
content: string,
prettierInstance: typeof prettier,
prettierConfig: prettier.Options | null
prettierInstance: typeof prettier
) {
await fs.writeFile(
filePath,
// Prettier v3 returns a promise
await prettierInstance.format(content, {
...prettierConfig,
...(await prettierInstance.resolveConfig(filePath)),
filepath: filePath,
parser: "markdown",
})
Expand Down
8 changes: 3 additions & 5 deletions packages/write/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Changeset } from "@changesets/types";
import fs from "fs-extra";
import humanId from "human-id";
import path from "path";
import prettier from "prettier";
import humanId from "human-id";
import { Changeset } from "@changesets/types";

function getPrettierInstance(cwd: string): typeof prettier {
try {
Expand Down Expand Up @@ -31,8 +31,6 @@ async function writeChangeset(
});

const prettierInstance = getPrettierInstance(cwd);
const prettierConfig = await prettierInstance.resolveConfig(cwd);

const newChangesetPath = path.resolve(changesetBase, `${changesetID}.md`);

// NOTE: The quotation marks in here are really important even though they are
Expand All @@ -49,7 +47,7 @@ ${summary}
newChangesetPath,
// Prettier v3 returns a promise
await prettierInstance.format(changesetContents, {
...prettierConfig,
...(await prettierInstance.resolveConfig(newChangesetPath)),
parser: "markdown",
})
);
Expand Down

0 comments on commit c6da182

Please sign in to comment.