Skip to content

Commit

Permalink
fix(tsconfig-references): append new line at EOL of tsconfig.json
Browse files Browse the repository at this point in the history
  • Loading branch information
Fonger committed May 18, 2022
1 parent 398b1ae commit 6b5a760
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions packages/tsconfig-references/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface TsReference {

interface TsConfig {
indent: string;
newLineEOF: string;
references?: TsReference[];
[key: string]: unknown;
}
Expand All @@ -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<void> {
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<boolean> {
Expand Down Expand Up @@ -120,3 +125,17 @@ const plugin: Plugin<Hooks> = {
};

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

0 comments on commit 6b5a760

Please sign in to comment.