File tree Expand file tree Collapse file tree 2 files changed +34
-1
lines changed
packages/schematics/angular/utility Expand file tree Collapse file tree 2 files changed +34
-1
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @license
3
+ * Copyright Google LLC All Rights Reserved.
4
+ *
5
+ * Use of this source code is governed by an MIT-style license that can be
6
+ * found in the LICENSE file at https://angular.io/license
7
+ */
8
+
9
+ import { EOL } from 'node:os' ;
10
+
11
+ const CRLF = '\r\n' ;
12
+ const LF = '\n' ;
13
+
14
+ export function getEOL ( content : string ) : string {
15
+ const newlines = content . match ( / (?: \r ? \n ) / g) ;
16
+
17
+ if ( newlines ?. length ) {
18
+ const crlf = newlines . filter ( ( l ) => l === CRLF ) . length ;
19
+ const lf = newlines . length - crlf ;
20
+
21
+ return crlf > lf ? CRLF : LF ;
22
+ }
23
+
24
+ return EOL ;
25
+ }
Original file line number Diff line number Diff line change @@ -18,16 +18,22 @@ import {
18
18
parseTree ,
19
19
printParseErrorCode ,
20
20
} from 'jsonc-parser' ;
21
+ import { getEOL } from './eol' ;
21
22
22
23
export type InsertionIndex = ( properties : string [ ] ) => number ;
23
24
export type JSONPath = ( string | number ) [ ] ;
24
25
25
26
/** @private */
26
27
export class JSONFile {
27
28
content : string ;
29
+ private eol : string ;
28
30
29
- constructor ( private readonly host : Tree , private readonly path : string ) {
31
+ constructor (
32
+ private readonly host : Tree ,
33
+ private readonly path : string ,
34
+ ) {
30
35
this . content = this . host . readText ( this . path ) ;
36
+ this . eol = getEOL ( this . content ) ;
31
37
}
32
38
33
39
private _jsonAst : Node | undefined ;
@@ -81,7 +87,9 @@ export class JSONFile {
81
87
82
88
const edits = modify ( this . content , jsonPath , value , {
83
89
getInsertionIndex,
90
+
84
91
formattingOptions : {
92
+ eol : this . eol ,
85
93
insertSpaces : true ,
86
94
tabSize : 2 ,
87
95
} ,
You can’t perform that action at this time.
0 commit comments