Skip to content

Commit 14d52e0

Browse files
committed
Only stringify once and correctly detect existing file newline
1 parent b17dc9a commit 14d52e0

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

lib/common/file-system.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ 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";
810
import stringifyPackage = require("stringify-package");
911
import detectNewline = require("detect-newline");
1012

@@ -207,10 +209,16 @@ export class FileSystem implements IFileSystem {
207209
space = this.getIndentationCharacter(filename);
208210
}
209211

210-
let stringifiedData = JSON.stringify(data, null, space);
211-
if (path.basename(filename) === "package.json") {
212-
const newline = detectNewline(stringifiedData);
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+
}
213219
stringifiedData = stringifyPackage(data, space, newline);
220+
} else {
221+
stringifiedData = JSON.stringify(data, null, space);
214222
}
215223

216224
return this.writeFile(filename, stringifiedData, encoding);

0 commit comments

Comments
 (0)