Skip to content

Commit 640a76a

Browse files
committed
fix(@angular/cli): retain existing EOL when updating JSON files
This commit updates the JSON utility to retain the existing EOF when updating files.
1 parent 94082a7 commit 640a76a

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
}

packages/angular/cli/src/utilities/json-file.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ import {
1919
parseTree,
2020
printParseErrorCode,
2121
} from 'jsonc-parser';
22+
import { getEOL } from './eol';
2223

2324
export type InsertionIndex = (properties: string[]) => number;
2425
export type JSONPath = (string | number)[];
2526

2627
/** @internal */
2728
export class JSONFile {
2829
content: string;
30+
private eol: string;
2931

3032
constructor(private readonly path: string) {
3133
const buffer = readFileSync(this.path);
@@ -34,6 +36,8 @@ export class JSONFile {
3436
} else {
3537
throw new Error(`Could not read '${path}'.`);
3638
}
39+
40+
this.eol = getEOL(this.content);
3741
}
3842

3943
private _jsonAst: Node | undefined;
@@ -91,6 +95,7 @@ export class JSONFile {
9195
formattingOptions: {
9296
insertSpaces: true,
9397
tabSize: 2,
98+
eol: this.eol,
9499
},
95100
});
96101

0 commit comments

Comments
 (0)