-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
119 lines (104 loc) · 2.82 KB
/
index.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import {promises as fs} from 'node:fs';
import process from 'node:process';
import {chromium} from 'playwright';
import core from '@actions/core';
import {
filterMessage, shouldCapture, shouldFail, logLevels,
} from './utils.js';
// Launches a Chromium browser instance and prepares a new page.
const browser = await chromium.launch();
const page = await browser.newPage();
/**
* Map to store captured console messages categorized by log levels.
* @type {Map<string, string[]>}
*/
const consoleMessages = new Map([
[logLevels[0], []],
[logLevels[1], []],
[logLevels[2], []],
[logLevels[3], []],
]);
/**
* Port to run the web application on.
* @type {string}
* @default ''
*/
const port = process.env.PORT || '';
/**
* Wait time before capturing logs.
* @type {string}
* @default '5000'
*/
const waitTime = process.env.WAIT_TIME || '5000';
/**
* URL of the web application.
* @type {string}
* @default 'http://localhost'
*/
const webAppUrl = process.env.WEBAPP_URL || 'http://localhost';
/**
* Flag to indicate if the action should fail.
* @type {boolean}
* @default false
*/
let shouldFailAction = false;
/**
* Mapping of console message types to log levels.
* @type {Object.<string, string>}
*/
const logLevelMapping = {
log: 'info',
debug: 'verbose',
info: 'info',
error: 'error',
warning: 'warning',
dir: 'info',
dirxml: 'info',
table: 'info',
trace: 'verbose',
clear: 'verbose',
startGroup: 'verbose',
startGroupCollapsed: 'verbose',
endGroup: 'verbose',
assert: 'error',
profile: 'verbose',
profileEnd: 'verbose',
count: 'verbose',
timeEnd: 'verbose',
};
/**
* Event listener for console messages from the page.
* @param {ConsoleMessage} message - The console message object.
*/
page.on('console', message => {
const messageType = message.type();
const logLevel = logLevelMapping[messageType] || 'info';
const logMessage = message.text();
if (shouldCapture(logLevel)) {
const filteredMessage = filterMessage(logLevel, logMessage);
if (filteredMessage.length > 0) {
consoleMessages.get(logLevel).push(filteredMessage);
if (shouldFail(logLevel)) {
shouldFailAction = true;
}
}
}
});
await page.goto(port ? `${webAppUrl}:${port}` : webAppUrl);
await page.waitForTimeout(Number.parseInt(waitTime, 10)); // Wait for the specified time
console.log(' ');
console.log('Captured messages:', Object.fromEntries(consoleMessages));
// Remove keys with empty arrays
for (const [key, value] of consoleMessages) {
if (value.length === 0) {
consoleMessages.delete(key);
}
}
await fs.writeFile('console_output.json', JSON.stringify(Object.fromEntries(consoleMessages), null, 2));
await browser.close();
if (shouldFailAction) {
console.log(' ');
console.warn('Action should fail due to log level threshold, but will not exit here.');
core.exportVariable('SHOULD_FAIL_ACTION', 'true');
}
console.log(' ');