Skip to content

Commit 15c943f

Browse files
LogikgateFatme
authored andcommitted
Use npm's modules to stringify package.json file before save. Fixes #4049
1 parent 5ac9442 commit 15c943f

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

lib/common/declarations.d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -2026,3 +2026,13 @@ interface ILockFile {
20262026
*/
20272027
check(lockFilePath?: string, lockFileOpts?: ILockFileOptions): boolean;
20282028
}
2029+
2030+
declare module "stringify-package" {
2031+
function stringifyPackage(data: any, indent: any, newline: string): string
2032+
export = stringifyPackage
2033+
}
2034+
2035+
declare module "detect-newline" {
2036+
function detectNewline(data: string): string | null;
2037+
export = detectNewline
2038+
}

lib/common/file-system.ts

+9-1
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 stringifyPackage = require("stringify-package");
9+
import detectNewline = require("detect-newline");
810

911
// TODO: Add .d.ts for mkdirp module (or use it from @types repo).
1012
const mkdirp = require("mkdirp");
@@ -205,7 +207,13 @@ export class FileSystem implements IFileSystem {
205207
space = this.getIndentationCharacter(filename);
206208
}
207209

208-
return this.writeFile(filename, JSON.stringify(data, null, space), encoding);
210+
let stringifiedData = JSON.stringify(data, null, space);
211+
if (path.basename(filename) === "package.json") {
212+
const newline = detectNewline(stringifiedData);
213+
stringifiedData = stringifyPackage(data, space, newline);
214+
}
215+
216+
return this.writeFile(filename, stringifiedData, encoding);
209217
}
210218

211219
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)