-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge-reports.ts
executable file
·56 lines (48 loc) · 1.61 KB
/
merge-reports.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env ts-node
import * as fsPromise from 'fs/promises';
import * as fs from 'fs';
import * as path from 'path';
import { getPackages } from '@lerna/project';
import { finished } from 'stream/promises';
interface FileError extends Error {
code: string;
}
function isFileError(e: unknown): e is FileError {
return !!e && typeof (e as Record<PropertyKey, unknown>)['code'] === 'string' && e instanceof Error;
}
const THIRD_LINE_INDEX = 2;
async function mergeReports(dir: string, targetDir: string, reportFileName: string): Promise<void> {
try {
await fsPromise.mkdir(targetDir);
} catch (e) {
if (!isFileError(e) || e.code !== 'EEXIST') {
throw e;
}
}
const completeReportStream = fs.createWriteStream(path.join(targetDir, reportFileName));
completeReportStream.write('<?xml version="1.0" encoding="UTF-8"?>\n<testExecutions version="1">\n');
const packages = await getPackages();
await Promise.all(
packages.map(async (pkg) => {
try {
const report = await fsPromise.readFile(path.join(pkg.location, reportFileName));
completeReportStream.write(
report
.toString()
.split('\n')
.slice(THIRD_LINE_INDEX, -1)
.join('\n')
.replaceAll(dir + path.sep, '') + '\n'
);
} catch (e) {
if (!isFileError(e) || e.code !== 'ENOENT') {
throw e;
}
}
})
);
completeReportStream.write('</testExecutions>\n');
completeReportStream.end();
await finished(completeReportStream);
}
void mergeReports(__dirname, path.join(__dirname, 'reports'), 'test-report.xml');