-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
report-collector.js
47 lines (44 loc) · 994 Bytes
/
report-collector.js
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
import { writeFile } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
export default new (class ReportCollector {
/**
* @type {string[]}
*/
#messageList = [];
#isRecording = false;
startRecord() {
this.#isRecording = true;
}
/**
* @param {string} message
*/
push(message) {
if (!this.#isRecording) return;
if (message.startsWith('#')) {
message = '##' + message;
}
this.#messageList.push(message);
}
outputMsg() {
if (process.env.CI) {
return writeFile(
fileURLToPath(new URL('../../_benchmark/internal-report.md', import.meta.url)),
removeAnsiStyles(this.#messageList.join('\n'))
);
}
}
})();
/**
* @param {string} text
* @returns {string}
*/
function removeAnsiStyles(text) {
const ansiRegex = new RegExp(
[
'[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\\u0007)',
'(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))'
].join('|'),
'g'
);
return text.replace(ansiRegex, '');
}