Skip to content

Commit

Permalink
Merge pull request #29 from Dcard/ts-config-newline
Browse files Browse the repository at this point in the history
  • Loading branch information
Fonger committed May 19, 2022
2 parents 398b1ae + 6b5a760 commit 67a2686
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions packages/tsconfig-references/src/index.ts
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 67a2686

Please sign in to comment.