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; +}