Skip to content

Commit

Permalink
fix(@angular-devkit/core): retain existing EOL when updating workspac…
Browse files Browse the repository at this point in the history
…e config

This commit updates the JSON utility to retain the existing EOF when updating the workspace config.

(cherry picked from commit d77c005)
  • Loading branch information
alan-agius4 committed Jan 5, 2024
1 parent a5c339e commit 3dc4db7
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/angular_devkit/core/src/workspace/json/writer.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -163,6 +164,7 @@ function updateJsonWorkspace(metadata: JsonWorkspaceMetadata): string {
formattingOptions: {
insertSpaces: true,
tabSize: 2,
eol: getEOL(content),
},
});

Expand All @@ -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;
}

0 comments on commit 3dc4db7

Please sign in to comment.