Skip to content

Commit

Permalink
fix: default encoding and maintain EOL
Browse files Browse the repository at this point in the history
  • Loading branch information
setchy committed Apr 19, 2024
1 parent 9eb1999 commit cda9ad7
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import fs from 'node:fs';
import { EOL } from 'node:os';

const DEFAULT_ENCODING = 'utf-8';

export function readJSONFile<T>(filename: string): T {
return JSON.parse(fs.readFileSync(filename, 'utf8').toString());
return JSON.parse(fs.readFileSync(filename, DEFAULT_ENCODING).toString());
}

export function writeJSONFile(filename: string, contents: unknown) {
fs.writeFileSync(filename, `${JSON.stringify(contents, null, 2)}\n`);
fs.writeFileSync(
filename,
`${JSON.stringify(contents, null, 2)}\n`,
DEFAULT_ENCODING,
);
}

export function writeCSVFile(filename: string, data: string[]) {
fs.writeFileSync(filename, data.join('\n'));
fs.writeFileSync(filename, data.join('\n'), DEFAULT_ENCODING);
fs.appendFileSync(filename, EOL, DEFAULT_ENCODING);
}

0 comments on commit cda9ad7

Please sign in to comment.