@@ -5,6 +5,10 @@ import * as injector from "./yok";
55import * as crypto from "crypto" ;
66import * as shelljs from "shelljs" ;
77import { 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).
1014const 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 {
0 commit comments