From d14cf79fd323529c6fe6ca956d9a7fda93bb425b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Fri, 11 Feb 2022 00:40:45 +0100 Subject: [PATCH] Fixed an issue that caused created CHANGELOG files not being formatted (#749) * Fixed an issue that caused created CHANGELOG files not being formatted * update tests --- .changeset/quiet-trainers-push.md | 6 +++++ packages/apply-release-plan/src/index.test.ts | 25 ++++++++++++++----- packages/apply-release-plan/src/index.ts | 25 +++++++++++++------ .../cli/src/commands/version/version.test.ts | 2 ++ packages/release-utils/src/run.test.ts | 9 ++++++- 5 files changed, 52 insertions(+), 15 deletions(-) create mode 100644 .changeset/quiet-trainers-push.md diff --git a/.changeset/quiet-trainers-push.md b/.changeset/quiet-trainers-push.md new file mode 100644 index 000000000..864a7e959 --- /dev/null +++ b/.changeset/quiet-trainers-push.md @@ -0,0 +1,6 @@ +--- +"@changesets/apply-release-plan": patch +"@changesets/cli": patch +--- + +Fixed an issue that caused **created** CHANGELOG files not being formatted in the same way as the **updated** ones (this could happen when calling `changeset version` for the very first time for a package). diff --git a/packages/apply-release-plan/src/index.test.ts b/packages/apply-release-plan/src/index.test.ts index e14175754..043aed7c4 100644 --- a/packages/apply-release-plan/src/index.test.ts +++ b/packages/apply-release-plan/src/index.test.ts @@ -1382,6 +1382,7 @@ describe("apply release plan", () => { expect(readme.trim()).toEqual(outdent`# pkg-a ## 1.1.0 + ### Minor Changes - Hey, let's have fun with testing!`); @@ -1427,13 +1428,14 @@ describe("apply release plan", () => { expect(readme.trim()).toEqual(outdent`# pkg-a ## 1.1.0 + ### Minor Changes - Hey, let's have fun with testing! ### Patch Changes - - pkg-b@2.0.0`); + - pkg-b@2.0.0`); expect(readmeB.trim()).toEqual(outdent`# pkg-b @@ -1532,13 +1534,13 @@ describe("apply release plan", () => { expect(readme.trim()).toEqual( [ "# pkg-a\n", - "## 1.1.0", + "## 1.1.0\n", "### Minor Changes\n", "- Hey, let's have fun with testing!", - "- Random stuff", - " \n get it while it's hot!", - "- New feature, much wow", - " \n look at this shiny stuff!" + "- Random stuff\n", + " get it while it's hot!\n", + "- New feature, much wow\n", + " look at this shiny stuff!" ].join("\n") ); }); @@ -1609,6 +1611,7 @@ describe("apply release plan", () => { expect(readme.trim()).toEqual(outdent`# pkg-a ## 1.0.4 + ### Patch Changes - Hey, let's have fun with testing! @@ -1618,6 +1621,7 @@ describe("apply release plan", () => { expect(readmeB.trim()).toEqual(outdent`# pkg-b ## 1.2.1 + ### Patch Changes - Hey, let's have fun with testing! @@ -1691,6 +1695,7 @@ describe("apply release plan", () => { expect(readme.trim()).toEqual(outdent`# pkg-a ## 1.0.4 + ### Patch Changes - Hey, let's have fun with testing!`); @@ -1698,6 +1703,7 @@ describe("apply release plan", () => { expect(readmeB.trim()).toEqual(outdent`# pkg-b ## 1.2.1 + ### Patch Changes - Hey, let's have fun with testing!`); @@ -1781,6 +1787,7 @@ describe("apply release plan", () => { expect(readme.trim()).toEqual(outdent`# pkg-a ## 1.0.4 + ### Patch Changes - Hey, let's have fun with testing!`); @@ -1788,6 +1795,7 @@ describe("apply release plan", () => { expect(readmeB.trim()).toEqual(outdent`# pkg-b ## 1.2.1 + ### Patch Changes - Hey, let's have fun with testing! @@ -1797,6 +1805,7 @@ describe("apply release plan", () => { expect(readmeC.trim()).toEqual(outdent`# pkg-c ## 2.1.0 + ### Minor Changes - Hey, let's have fun with testing!`); @@ -1880,6 +1889,7 @@ describe("apply release plan", () => { expect(readme.trim()).toEqual(outdent`# pkg-a ## 1.0.4 + ### Patch Changes - Hey, let's have fun with testing!`); @@ -1887,6 +1897,7 @@ describe("apply release plan", () => { expect(readmeB.trim()).toEqual(outdent`# pkg-b ## 1.2.1 + ### Patch Changes - Hey, let's have fun with testing! @@ -1896,6 +1907,7 @@ describe("apply release plan", () => { expect(readmeC.trim()).toEqual(outdent`# pkg-c ## 2.0.1 + ### Patch Changes - Hey, let's have fun with testing!`); @@ -2184,6 +2196,7 @@ describe("apply release plan", () => { ).toBe(`# pkg-a ## 1.1.0 + ### Minor Changes - ${lastCommit}: Hey, let's have fun with testing! diff --git a/packages/apply-release-plan/src/index.ts b/packages/apply-release-plan/src/index.ts index e330d288f..b2f22e65d 100644 --- a/packages/apply-release-plan/src/index.ts +++ b/packages/apply-release-plan/src/index.ts @@ -275,7 +275,11 @@ async function updateChangelog( if (fs.existsSync(changelogPath)) { await prependFile(changelogPath, templateString, name, prettierConfig); } else { - await fs.writeFile(changelogPath, `# ${name}${templateString}`); + await writeFormattedMarkdownFile( + changelogPath, + `# ${name}${templateString}`, + prettierConfig + ); } } catch (e) { console.warn(e); @@ -304,21 +308,26 @@ async function prependFile( // if the file exists but doesn't have the header, we'll add it in if (!fileData) { const completelyNewChangelog = `# ${name}${data}`; - await fs.writeFile( + await writeFormattedMarkdownFile( filePath, - prettier.format(completelyNewChangelog, { - ...prettierConfig, - filepath: filePath, - parser: "markdown" - }) + completelyNewChangelog, + prettierConfig ); return; } const newChangelog = fileData.replace("\n", data); + await writeFormattedMarkdownFile(filePath, newChangelog, prettierConfig); +} + +async function writeFormattedMarkdownFile( + filePath: string, + content: string, + prettierConfig?: prettier.Options | null +) { await fs.writeFile( filePath, - prettier.format(newChangelog, { + prettier.format(content, { ...prettierConfig, filepath: filePath, parser: "markdown" diff --git a/packages/cli/src/commands/version/version.test.ts b/packages/cli/src/commands/version/version.test.ts index d62328404..4ca7c343c 100644 --- a/packages/cli/src/commands/version/version.test.ts +++ b/packages/cli/src/commands/version/version.test.ts @@ -585,6 +585,7 @@ describe("updateInternalDependents: always", () => { "# pkg-a ## 1.0.1 + ### Patch Changes - Updated dependencies [g1th4sh] @@ -595,6 +596,7 @@ describe("updateInternalDependents: always", () => { "# pkg-b ## 1.0.1 + ### Patch Changes - g1th4sh: This is not a summary diff --git a/packages/release-utils/src/run.test.ts b/packages/release-utils/src/run.test.ts index c5c20deaa..597178ee5 100644 --- a/packages/release-utils/src/run.test.ts +++ b/packages/release-utils/src/run.test.ts @@ -121,7 +121,10 @@ describe("version", () => { expect.stringContaining(`# pkg-a ## 1.1.0 -### Minor Changes`) + +### Minor Changes + +`) ); expect( await fs.readFile( @@ -132,7 +135,9 @@ describe("version", () => { expect.stringContaining(`# pkg-b ## 1.1.0 + ### Minor Changes + `) ); expect(changedPackages).toEqual([ @@ -221,7 +226,9 @@ describe("version", () => { expect.stringContaining(`# pkg-a ## 1.1.0 + ### Minor Changes + `) ); await expect(