Skip to content

Commit

Permalink
Noviny/add prettier on changelog write (#61)
Browse files Browse the repository at this point in the history
* run prettier on changelogs before writing them
  • Loading branch information
Noviny committed Jun 1, 2019
1 parent 6dc510f commit 7fa4264
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .changeset/cuddly-mails-change/changes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"releases": [{ "name": "@changesets/cli", "type": "patch" }],
"dependents": []
}
3 changes: 3 additions & 0 deletions .changeset/cuddly-mails-change/changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
When bumping, run prettier over the changelog file.

If you want this option turned off, add `disabledLanguage: ["markdown"] to your prettier config.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Has Empty Changelog

## 1.0.0

- [patch] This existed before [b8bb699](https://bitbucket.org/atlassian/atlaskit-mk-2/commits/b8bb699)
- [minor] This also existed before [abcdefg](https://bitbucket.org/atlassian/atlaskit-mk-2/commits/abcdefg)
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const noChangelogFileChangeset = {
const filledChangelogContent = `# Has Empty Changelog
## 1.0.0
- [patch] This existed before [b8bb699](https://bitbucket.org/atlassian/atlaskit-mk-2/commits/b8bb699)
- [minor] This also existed before [abcdefg](https://bitbucket.org/atlassian/atlaskit-mk-2/commits/abcdefg)
`;
Expand Down Expand Up @@ -134,6 +135,7 @@ describe("updateChangelog", () => {
expect(updatedChangelog).toEqual(`# has-empty-changelog
## 1.1.0
### Minor Changes
- b8bb699: This is a summary
Expand All @@ -148,6 +150,7 @@ describe("updateChangelog", () => {
expect(updatedChangelog).toEqual(`# has-empty-changelog
## 1.1.0
### Minor Changes
- abcdefg: This is a second summary
Expand All @@ -168,21 +171,26 @@ describe("updateChangelog", () => {
const updatedExistingChangelog = fs
.readFileSync(existingChangelogPath)
.toString();

expect(updatedChangelog).toEqual(`# has-empty-changelog
## 1.1.0
### Minor Changes
- b8bb699: This is a summary
`);

expect(updatedExistingChangelog).toEqual(`# Has Empty Changelog
## 1.0.1
### Patch Changes
- b8bb699: This is a summary
## 1.0.0
- [patch] This existed before [b8bb699](https://bitbucket.org/atlassian/atlaskit-mk-2/commits/b8bb699)
- [minor] This also existed before [abcdefg](https://bitbucket.org/atlassian/atlaskit-mk-2/commits/abcdefg)
`);
Expand Down Expand Up @@ -224,11 +232,13 @@ describe("updateChangelog", () => {
expect(updatedChangelog).toEqual(`# Has Empty Changelog
## 1.1.0
### Minor Changes
- b8bb699: This is a summary
## 1.0.0
- [patch] This existed before [b8bb699](https://bitbucket.org/atlassian/atlaskit-mk-2/commits/b8bb699)
- [minor] This also existed before [abcdefg](https://bitbucket.org/atlassian/atlaskit-mk-2/commits/abcdefg)
`);
Expand Down
23 changes: 18 additions & 5 deletions packages/cli/src/utils/updateChangelog/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import fs from "fs-extra";

import path from "path";
import util from "util";
import prettier from "prettier";

import generateMarkdownTemplate from "./template";
import logger from "../logger";
import * as bolt from "../bolt-replacements";
Expand Down Expand Up @@ -35,7 +36,7 @@ export default async function updateChangelog(releaseObject, opts) {
const templateString = `\n\n${markdown.trim("\n")}\n`;
try {
if (fs.existsSync(changelogPath)) {
await prependFile(changelogPath, templateString, pkg);
await prependFile(changelogPath, templateString, pkg, cwd);
} else {
await writeFile(changelogPath, `# ${pkg.name}${templateString}`);
}
Expand All @@ -49,14 +50,26 @@ export default async function updateChangelog(releaseObject, opts) {
return udpatedChangelogs;
}

async function prependFile(filePath, data, pkg) {
async function prependFile(filePath, data, pkg, cwd) {
const prettierConfig = await prettier.resolveConfig(cwd);

const fileData = fs.readFileSync(filePath).toString();
// if the file exists but doesn't have the header, we'll add it in
if (!fileData) {
const completelyNewChangelog = `# ${pkg.name}${data}`;
fs.writeFileSync(filePath, completelyNewChangelog);
fs.writeFileSync(
filePath,
prettier.format(completelyNewChangelog, {
...prettierConfig,
parser: "markdown"
})
);
return;
}
const newChangelog = fileData.replace("\n", data);
fs.writeFileSync(filePath, newChangelog);

fs.writeFileSync(
filePath,
prettier.format(newChangelog, { ...prettierConfig, parser: "markdown" })
);
}

0 comments on commit 7fa4264

Please sign in to comment.