Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.

fix: project's package.json indentation is not persisted #727

Merged
merged 3 commits into from
Dec 10, 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ jasmine-config/reporter.js.map

hooks
.DS_Store

!projectHelpers.spec.js
3 changes: 2 additions & 1 deletion jasmine-config/jasmine.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"spec_dir": ".",
"spec_files": [
"./!(node_modules)/**/*.spec.js"
"./!(node_modules)/**/*.spec.js",
"./*.spec.js"
],
"helpers": [
"jasmine-config/**/*.js"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"scripts": {
"postinstall": "node postinstall.js",
"postpack": "rm -rf node_modules",
"prepare": "tsc",
"prepare": "tsc && npm run jasmine",
"test": "npm run prepare && npm run jasmine",
"jasmine": "jasmine --config=jasmine-config/jasmine.json",
"version": "rm package-lock.json && conventional-changelog -p angular -i CHANGELOG.md -s && git add CHANGELOG.md"
Expand Down
22 changes: 18 additions & 4 deletions projectHelpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { resolve } = require("path");
const { readFileSync, writeFileSync } = require("fs");
const fs = require("fs");

const hook = require("nativescript-hook")(__dirname);

Expand Down Expand Up @@ -38,13 +38,26 @@ const isVue = ({ projectDir, packageJson } = {}) => {

const getPackageJson = projectDir => {
const packageJsonPath = getPackageJsonPath(projectDir);
return JSON.parse(readFileSync(packageJsonPath, "utf8"));
return JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
};

const writePackageJson = (content, projectDir) => {
const packageJsonPath = getPackageJsonPath(projectDir);
writeFileSync(packageJsonPath, JSON.stringify(content, null, 2))
const currentJsonContent = fs.readFileSync(packageJsonPath);
const indentation = getIndentationCharacter(currentJsonContent);
const stringifiedContent = JSON.stringify(content, null, indentation);
const currentPackageJsonContent = JSON.parse(currentJsonContent);

if (JSON.stringify(currentPackageJsonContent, null, indentation) !== stringifiedContent) {
fs.writeFileSync(packageJsonPath, stringifiedContent)
}
}

const getIndentationCharacter = (jsonContent) => {
const matches = jsonContent && jsonContent.toString().match(/{\r*\n*(\W*)"/m);
return matches && matches[1];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return (matches && matches[1]) || defaultIndentation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact we do not need default indentation, I've forgotten to remove the variable. I've already added a unit test that verifies we work correctly when the matches[1] group is undefined.

}

const getProjectDir = hook.findProjectDir;

const getPackageJsonPath = projectDir => resolve(projectDir, "package.json");
Expand Down Expand Up @@ -96,5 +109,6 @@ module.exports = {
isVue,
isTypeScript,
writePackageJson,
convertSlashesInPath
convertSlashesInPath,
getIndentationCharacter
};
110 changes: 110 additions & 0 deletions projectHelpers.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
const { getIndentationCharacter, writePackageJson } = require("./projectHelpers");
const fs = require("fs");

describe('projectHelpers', () => {
const originalReadFileSync = fs.readFileSync;
const originalWriteFileSync = fs.writeFileSync;
const tab = "\t";
const multipleSpaces = " ";
const twoSpaces = " ";

afterEach(() => {
fs.readFileSync = originalReadFileSync;
fs.writeFileSync = originalWriteFileSync;
});

describe('getIndentationCharacter', () => {
[
{
testName: 'returns two spaces when file starts with two spaces',
input: `{${twoSpaces}"abc": "1"${twoSpaces}}`,
expectedResult: twoSpaces
},
{
testName: 'returns two spaces when file starts with two spaces and binary content is passed',
input: Buffer.from(`{${twoSpaces}"abc": "1"${twoSpaces}}`),
expectedResult: twoSpaces
},
{
testName: 'returns empty string when file starts without any indentation',
input: `{"abc": "1"}`,
expectedResult: ''
},
{
testName: 'returns tab when file starts with tab',
input: `{${tab}"abc": "1"${tab}}`,
expectedResult: tab
},
{
testName: 'returns two spaces when file starts with two spaces and new line before them',
input: `{\n${twoSpaces}"abc": "1"\n}`,
expectedResult: twoSpaces
},
{
testName: 'returns tab when file starts with tab and new line before them',
input: `{\n${tab}"abc": "1"\n}`,
expectedResult: tab
},
{
testName: 'returns multiple spaces when file starts with multiple spaces and new line before them',
input: `{\n${multipleSpaces}"abc": "1"\n}`,
expectedResult: multipleSpaces
}
].forEach(({ testName, input, expectedResult }) => {
it(testName, () => {
expect(getIndentationCharacter(input)).toEqual(expectedResult);
});
});
});

describe('writePackageJson', () => {
const mockFileSystemApi = () => {
const data = {
isWriteFileSyncCalled: false
};

fs.readFileSync = (p) => {
return JSON.stringify({ a: 1 });
};

fs.writeFileSync = (p, c) => {
data.isWriteFileSyncCalled = true;
};

return data;
};

it('does not write package.json when content has not changed', () => {
const data = mockFileSystemApi();
writePackageJson({ a: 1 }, "projDir");
expect(data.isWriteFileSyncCalled).toBe(false);
});

it('writes content, when the new one is different from the current one', () => {
const data = mockFileSystemApi();
writePackageJson({ b: 2 }, "projDir");
expect(data.isWriteFileSyncCalled).toBe(true);
});

it('keeps indentation of the package.json when rewriting it', () => {
let currentIndentSymbol = tab;
fs.readFileSync = (p) => {
return JSON.stringify({ a: 1 }, null, currentIndentSymbol);
};

let writtenContent = null;
fs.writeFileSync = (p, c) => {
writtenContent = c;
};

// Ensure tab indentation is persisted
writePackageJson({ b: 2 }, "projDir");
expect(writtenContent).toBe(`{\n${tab}"b": 2\n}`);

// Ensure spaces indentation is persisted
currentIndentSymbol = multipleSpaces;
writePackageJson({ b: 2 }, "projDir");
expect(writtenContent).toBe(`{\n${multipleSpaces}"b": 2\n}`);
});
});
});