Skip to content

Commit be0701b

Browse files
author
Dimitar Tachev
authored
Merge pull request #4072 from flypapertech/add-trailing-newline-when-saving-packagejson
feat(): Add trailing newline when saving package.json
2 parents 141dfe2 + 14d52e0 commit be0701b

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

lib/common/declarations.d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -2011,3 +2011,13 @@ interface ILockFile {
20112011
*/
20122012
check(lockFilePath?: string, lockFileOpts?: ILockFileOptions): boolean;
20132013
}
2014+
2015+
declare module "stringify-package" {
2016+
function stringifyPackage(data: any, indent: any, newline: string): string
2017+
export = stringifyPackage
2018+
}
2019+
2020+
declare module "detect-newline" {
2021+
function detectNewline(data: string): string | null;
2022+
export = detectNewline
2023+
}

lib/common/file-system.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import * as injector from "./yok";
55
import * as crypto from "crypto";
66
import * as shelljs from "shelljs";
77
import { parseJson } from "./helpers";
8+
import { PACKAGE_JSON_FILE_NAME } from "../constants";
9+
import { EOL } from "os";
10+
import stringifyPackage = require("stringify-package");
11+
import detectNewline = require("detect-newline");
812

913
// TODO: Add .d.ts for mkdirp module (or use it from @types repo).
1014
const mkdirp = require("mkdirp");
@@ -205,7 +209,19 @@ export class FileSystem implements IFileSystem {
205209
space = this.getIndentationCharacter(filename);
206210
}
207211

208-
return this.writeFile(filename, JSON.stringify(data, null, space), encoding);
212+
let stringifiedData;
213+
if (path.basename(filename) === PACKAGE_JSON_FILE_NAME) {
214+
let newline = EOL;
215+
if (fs.existsSync(filename)) {
216+
const existingFile = this.readText(filename);
217+
newline = detectNewline(existingFile);
218+
}
219+
stringifiedData = stringifyPackage(data, space, newline);
220+
} else {
221+
stringifiedData = JSON.stringify(data, null, space);
222+
}
223+
224+
return this.writeFile(filename, stringifiedData, encoding);
209225
}
210226

211227
public copyFile(sourceFileName: string, destinationFileName: string): void {

npm-shrinkwrap.json

+19-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"cli-table": "https://github.com/telerik/cli-table/tarball/v0.3.1.2",
3838
"color": "3.0.0",
3939
"colors": "1.1.2",
40+
"detect-newline": "2.1.0",
4041
"email-validator": "1.0.4",
4142
"esprima": "2.7.0",
4243
"gaze": "1.1.0",
@@ -73,6 +74,7 @@
7374
"shelljs": "0.7.6",
7475
"simple-plist": "0.2.1",
7576
"source-map": "0.5.6",
77+
"stringify-package": "1.0.0",
7678
"tabtab": "https://github.com/Icenium/node-tabtab/tarball/master",
7779
"tar": "4.4.4",
7880
"temp": "0.8.3",

0 commit comments

Comments
 (0)