Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect for indentation #77

Merged
merged 1 commit into from
Oct 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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