From 6b5a760a31a40875954c946ed68d264750570397 Mon Sep 17 00:00:00 2001 From: Fonger <5862369+Fonger@users.noreply.github.com> Date: Wed, 18 May 2022 20:13:02 +0800 Subject: [PATCH] fix(tsconfig-references): append new line at EOL of tsconfig.json --- packages/tsconfig-references/src/index.ts | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/packages/tsconfig-references/src/index.ts b/packages/tsconfig-references/src/index.ts index 5bcd95a..d7ab57c 100644 --- a/packages/tsconfig-references/src/index.ts +++ b/packages/tsconfig-references/src/index.ts @@ -13,6 +13,7 @@ interface TsReference { interface TsConfig { indent: string; + newLineEOF: string; references?: TsReference[]; [key: string]: unknown; } @@ -29,15 +30,19 @@ async function readTsConfig( return { ...JSON.parse(content), indent: detectIndent(content).indent, + newLineEOF: getNewLineAtEOF(content), }; } async function writeTsConfig( workspace: Workspace, - { indent, ...tsConfig }: TsConfig, + { indent, newLineEOF, ...tsConfig }: TsConfig, ): Promise { const path = getTsConfigPath(workspace); - await xfs.writeFilePromise(path, JSON.stringify(tsConfig, null, indent)); + await xfs.writeFilePromise( + path, + JSON.stringify(tsConfig, null, indent) + newLineEOF, + ); } async function isTsWorkspace(workspace: Workspace): Promise { @@ -120,3 +125,17 @@ const plugin: Plugin = { }; export default plugin; + +function getNewLineAtEOF(input: string) { + const length = input.length; + + if (input[length - 1] === '\n') { + if (input[length - 2] === '\r') { + return '\r\n'; + } + + return '\n'; + } + + return ''; +}