File tree Expand file tree Collapse file tree 2 files changed +30
-0
lines changed
packages/angular/cli/src/utilities Expand file tree Collapse file tree 2 files changed +30
-0
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 @@ -19,13 +19,15 @@ import {
19
19
parseTree ,
20
20
printParseErrorCode ,
21
21
} from 'jsonc-parser' ;
22
+ import { getEOL } from './eol' ;
22
23
23
24
export type InsertionIndex = ( properties : string [ ] ) => number ;
24
25
export type JSONPath = ( string | number ) [ ] ;
25
26
26
27
/** @internal */
27
28
export class JSONFile {
28
29
content : string ;
30
+ private eol : string ;
29
31
30
32
constructor ( private readonly path : string ) {
31
33
const buffer = readFileSync ( this . path ) ;
@@ -34,6 +36,8 @@ export class JSONFile {
34
36
} else {
35
37
throw new Error ( `Could not read '${ path } '.` ) ;
36
38
}
39
+
40
+ this . eol = getEOL ( this . content ) ;
37
41
}
38
42
39
43
private _jsonAst : Node | undefined ;
@@ -91,6 +95,7 @@ export class JSONFile {
91
95
formattingOptions : {
92
96
insertSpaces : true ,
93
97
tabSize : 2 ,
98
+ eol : this . eol ,
94
99
} ,
95
100
} ) ;
96
101
You can’t perform that action at this time.
0 commit comments