Skip to content

feat(): Add trailing newline when saving package.json #4072

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

Conversation

Logikgate
Copy link
Contributor

PR Checklist

What is the current behavior?

Nativescript-CLI commands that save package.json are stripping the trailing newline that NPM inserts when saving package.json. This can cause un-needed commits of package.json where the only thing that has changed is the removal of the trailing newline.

What is the new behavior?

This pull request uses the module stringify-package, the same one used by NPM, to stringify package.json so that the formatting is kept the same. The method writeJSON now uses stringify-package only if it is saving a file named package.json.

Fixes/Implements/Closes #4049 .

@@ -205,7 +207,13 @@ export class FileSystem implements IFileSystem {
space = this.getIndentationCharacter(filename);
}

return this.writeFile(filename, JSON.stringify(data, null, space), encoding);
let stringifiedData = JSON.stringify(data, null, space);
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe this should be moved inside an else statement. No need to stringify data multiple times.

let stringifiedData;
if (path.basename(filename) === "package.json") {
	const newline = detectNewline(stringifiedData);
	stringifiedData = stringifyPackage(data, space, newline);
} else {
	stringifiedData = JSON.stringify(data, null, space);
}

@@ -205,7 +207,13 @@ export class FileSystem implements IFileSystem {
space = this.getIndentationCharacter(filename);
}

return this.writeFile(filename, JSON.stringify(data, null, space), encoding);
let stringifiedData = JSON.stringify(data, null, space);
if (path.basename(filename) === "package.json") {
Copy link
Contributor

@KristianDD KristianDD Oct 29, 2018

Choose a reason for hiding this comment

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

package.json might be used from constants:

import { PACKAGE_JSON_FILE_NAME } from "../constants";

if (path.basename(filename) === PACKAGE_JSON_FILE_NAME ) {
...
}

package.json Outdated
@@ -37,6 +37,7 @@
"cli-table": "https://github.com/telerik/cli-table/tarball/v0.3.1.2",
"color": "3.0.0",
"colors": "1.1.2",
"detect-newline": "^2.1.0",
Copy link
Contributor

Choose a reason for hiding this comment

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

It is safer to use an exact version. You can use npm i detect-newline --save-exact. Same goes for stringify-package dependency.

@DimitarTachev DimitarTachev merged commit be0701b into NativeScript:master Nov 2, 2018
@radeva
Copy link

radeva commented Nov 19, 2018

Hi @Logikgate ,

Congratulations on being one of the winners in the {N} First-time contributors contest!

You can claim your prize by contacting us at nativescriptwinners[at]progress.com not later than Nov 30th 2018 . Make sure you send us the following info: 
👉Your full name 
👉Your email 
👉Your country or residence 

Best regards,
The NativeScript Team

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants