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

Conversation

rosen-vladimirov
Copy link
Contributor

In case project's package.json uses tabs or multiple spaces for indentation, the postinstall script of nativescript-dev-webpack overwrites it with two spaces whenever it is executed.
The problem is that the plugin tries to modify the package.json and persists it on every operation.
Fix the behavior by checking the indentation character and use it when stringifying the content.
Also compare the current content of the file with the one we will write and skip the write operation in case they match.
Add unit tests for the new methods.

PR Checklist

What is the current behavior?

When the project's package.json is indented with tabs or more than two spaces, running the postinstall script of the plugin overwrites the content with indentation with two spaces.

What is the new behavior?

Indentation is persisted. Also package.json will not be overwritten in case there's no need.

Fixes issue NativeScript/nativescript-cli#4190

@rosen-vladimirov rosen-vladimirov self-assigned this Dec 6, 2018
@rosen-vladimirov rosen-vladimirov force-pushed the vladimirov/persist-package-json-indent branch from b2b2146 to bd9a4eb Compare December 6, 2018 20:36
@Fatme
Copy link
Contributor

Fatme commented Dec 7, 2018

run ci

const getIndentationCharacter = (jsonContent) => {
const defaultIndentation = " ";
const matches = jsonContent.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.

Copy link
Contributor

@vchimev vchimev left a comment

Choose a reason for hiding this comment

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

Nice!
Btw, I'm not sure if the tests execute on a regular basis, however we could add npm prepack script to execute them.

@rosen-vladimirov rosen-vladimirov force-pushed the vladimirov/persist-package-json-indent branch from bd9a4eb to 3df741f Compare December 7, 2018 13:11
In case project's package.json uses tabs or multiple spaces for indentation, the postinstall script of nativescript-dev-webpack overwrites it with two spaces whenever it is executed.
The problem is that the plugin tries to modify the package.json and persists it on every operation.
Fix the behavior by checking the indentation character and use it when stringifying the content.
Also compare the current content of the file with the one we will write and skip the write operation in case they match.
Add unit tests for the new methods.
@rosen-vladimirov rosen-vladimirov force-pushed the vladimirov/persist-package-json-indent branch from 28e70de to e511b67 Compare December 10, 2018 11:27
@rosen-vladimirov rosen-vladimirov merged commit a45a45c into master Dec 10, 2018
@rosen-vladimirov rosen-vladimirov deleted the vladimirov/persist-package-json-indent branch December 10, 2018 11:58
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants