Skip to content

Commit

Permalink
Fixed an issue that caused created CHANGELOG files not being formatted (
Browse files Browse the repository at this point in the history
#749)

* Fixed an issue that caused created CHANGELOG files not being formatted

* update tests
  • Loading branch information
Andarist committed Feb 10, 2022
1 parent d02f2b8 commit d14cf79
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 15 deletions.
6 changes: 6 additions & 0 deletions .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).
25 changes: 19 additions & 6 deletions packages/apply-release-plan/src/index.test.ts
Expand Up @@ -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!`);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
);
});
Expand Down Expand Up @@ -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!
Expand All @@ -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!
Expand Down Expand Up @@ -1691,13 +1695,15 @@ describe("apply release plan", () => {
expect(readme.trim()).toEqual(outdent`# pkg-a
## 1.0.4
### Patch Changes
- Hey, let's have fun with testing!`);

expect(readmeB.trim()).toEqual(outdent`# pkg-b
## 1.2.1
### Patch Changes
- Hey, let's have fun with testing!`);
Expand Down Expand Up @@ -1781,13 +1787,15 @@ describe("apply release plan", () => {
expect(readme.trim()).toEqual(outdent`# pkg-a
## 1.0.4
### Patch Changes
- Hey, let's have fun with testing!`);

expect(readmeB.trim()).toEqual(outdent`# pkg-b
## 1.2.1
### Patch Changes
- Hey, let's have fun with testing!
Expand All @@ -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!`);
Expand Down Expand Up @@ -1880,13 +1889,15 @@ describe("apply release plan", () => {
expect(readme.trim()).toEqual(outdent`# pkg-a
## 1.0.4
### Patch Changes
- Hey, let's have fun with testing!`);

expect(readmeB.trim()).toEqual(outdent`# pkg-b
## 1.2.1
### Patch Changes
- Hey, let's have fun with testing!
Expand All @@ -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!`);
Expand Down Expand Up @@ -2184,6 +2196,7 @@ describe("apply release plan", () => {
).toBe(`# pkg-a
## 1.1.0
### Minor Changes
- ${lastCommit}: Hey, let's have fun with testing!
Expand Down
25 changes: 17 additions & 8 deletions packages/apply-release-plan/src/index.ts
Expand Up @@ -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);
Expand Down Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/commands/version/version.test.ts
Expand Up @@ -585,6 +585,7 @@ describe("updateInternalDependents: always", () => {
"# pkg-a
## 1.0.1
### Patch Changes
- Updated dependencies [g1th4sh]
Expand All @@ -595,6 +596,7 @@ describe("updateInternalDependents: always", () => {
"# pkg-b
## 1.0.1
### Patch Changes
- g1th4sh: This is not a summary
Expand Down
9 changes: 8 additions & 1 deletion packages/release-utils/src/run.test.ts
Expand Up @@ -121,7 +121,10 @@ describe("version", () => {
expect.stringContaining(`# pkg-a
## 1.1.0
### Minor Changes`)
### Minor Changes
`)
);
expect(
await fs.readFile(
Expand All @@ -132,7 +135,9 @@ describe("version", () => {
expect.stringContaining(`# pkg-b
## 1.1.0
### Minor Changes
`)
);
expect(changedPackages).toEqual([
Expand Down Expand Up @@ -221,7 +226,9 @@ describe("version", () => {
expect.stringContaining(`# pkg-a
## 1.1.0
### Minor Changes
`)
);
await expect(
Expand Down

0 comments on commit d14cf79

Please sign in to comment.