Skip to content

Commit

Permalink
fix: Respect package.json indentation (#77)
Browse files Browse the repository at this point in the history
Respects the user's indentation in their `package.json`

Fixes #68
  • Loading branch information
nathanchapman authored and jhnns committed Oct 3, 2018
1 parent ff56fbb commit e766fb0
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
21 changes: 14 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"chalk": "^1.1.3",
"cli-cursor": "^2.1.0",
"cli-spinners": "^1.0.0",
"detect-indent": "^5.0.0",
"es6-error": "^4.0.2",
"pify": "^3.0.0",
"semver": "^5.3.0",
Expand Down
4 changes: 3 additions & 1 deletion src/tasks/updatePackageJson.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import detectIndent from "detect-indent";
import createUpdatedPackageJson from "./util/createUpdatedPackageJson";
import Sequence from "./util/Sequence";

Expand All @@ -6,7 +7,8 @@ function lastChar(str) {
}

function stringify(newPackageJson, oldPackageJsonStr) {
let newPackageJsonStr = JSON.stringify(newPackageJson, null, " ");
const indent = detectIndent(oldPackageJsonStr).indent || " ";
let newPackageJsonStr = JSON.stringify(newPackageJson, null, indent);
const lastCharFromOldPackageJson = lastChar(oldPackageJsonStr);

// Preserve the new line character at the end if there was one
Expand Down
19 changes: 19 additions & 0 deletions test/tasks/updatePackageJson.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,25 @@ describe("updatePackageJson()", () => {
)
);
});
it("should respect indentation", async () => {
const updtr = new FakeUpdtr();
const updateResults = [];
const fourSpacedPackageJson = outdatedRegular.replace(/ {2}/g, " ");
const tabbedPackageJson = outdatedRegular.replace(/ {2}/g, "\t");

updtr.writeFile.resolves();

updtr.readFile.resolves(fourSpacedPackageJson);

await updatePackageJson(updtr, updateResults);

updtr.readFile.resolves(tabbedPackageJson);

await updatePackageJson(updtr, updateResults);

expect(updtr.writeFile.getCall(0).args[1]).toBe(fourSpacedPackageJson);
expect(updtr.writeFile.getCall(1).args[1]).toBe(tabbedPackageJson);
});
it("should emit the expected events", async () => {
const updtr = new FakeUpdtr();
const updateResults = [module1ToLatestSuccess];
Expand Down

0 comments on commit e766fb0

Please sign in to comment.