From 7f395be1beeec192330936692537697d3b3395ff Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Fri, 5 Jan 2024 09:05:15 +0000 Subject: [PATCH] fix(@angular-devkit/core): retain existing EOL when updating workspace config This commit updates the JSON utility to retain the existing EOF when updating the workspace config. (cherry picked from commit d77c0053136986a8e6241a11a28d3604783922fb) --- .../core/src/workspace/json/writer.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/packages/angular_devkit/core/src/workspace/json/writer.ts b/packages/angular_devkit/core/src/workspace/json/writer.ts index a5d0fb145a06..fc23f524cb2c 100644 --- a/packages/angular_devkit/core/src/workspace/json/writer.ts +++ b/packages/angular_devkit/core/src/workspace/json/writer.ts @@ -7,6 +7,7 @@ */ import { applyEdits, modify } from 'jsonc-parser'; +import { EOL } from 'node:os'; import { JsonObject, JsonValue } from '../../json'; import { ProjectDefinition, TargetDefinition, WorkspaceDefinition } from '../definitions'; import { WorkspaceHost } from '../host'; @@ -163,6 +164,7 @@ function updateJsonWorkspace(metadata: JsonWorkspaceMetadata): string { formattingOptions: { insertSpaces: true, tabSize: 2, + eol: getEOL(content), }, }); @@ -171,3 +173,18 @@ function updateJsonWorkspace(metadata: JsonWorkspaceMetadata): string { return content; } + +function getEOL(content: string): string { + const CRLF = '\r\n'; + const LF = '\n'; + const newlines = content.match(/(?:\r?\n)/g); + + if (newlines?.length) { + const crlf = newlines.filter((l) => l === CRLF).length; + const lf = newlines.length - crlf; + + return crlf > lf ? CRLF : LF; + } + + return EOL; +}