-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResultsIO.ts
36 lines (29 loc) · 879 Bytes
/
ResultsIO.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { existsSync, readFileSync, writeFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { ReportResult } from "./reporters/Reporter.js";
const __dirname = dirname(fileURLToPath(import.meta.url));
const encoding: BufferEncoding = "utf-8";
export class ResultsIO {
private readonly fileName: string;
constructor(fileName = "results.json") {
this.fileName = fileName;
}
get() {
let results: ReportResult[] = [];
if (existsSync(this.fileName)) {
results = JSON.parse(readFileSync(this.fileName, encoding));
}
return results;
}
private set(results: ReportResult[]) {
writeFileSync(
join(__dirname, this.fileName),
JSON.stringify(results, null, 2),
encoding
);
}
append(result: ReportResult) {
this.set([...this.get(), result]);
}
}