archery/scripts/build.js
Cory Sanin 1fea301ecf
Some checks failed
App Image CI / Build app image (push) Failing after -18s
move to gitea
2025-04-29 20:56:09 -05:00

70 lines
2.0 KiB
JavaScript

document.addEventListener('DOMContentLoaded', function () {
const logContainer = this.getElementById('logs');
const followLogsBtn = this.getElementById('followCheckmark');
const buildStatusTxt = this.getElementById('buildStatus');
/**
* Get the correct path to establish a ws connection
* @param {Location} loc
*/
function wsPath(loc) {
return loc.pathname.replace(/\/$/, '') + '/ws';
}
/**
* Add log line to the DOM
* @param {string[]} str
*/
function appendLines(str, e = false) {
str.forEach(line => {
const p = document.createElement('p');
p.appendChild(document.createTextNode(line));
logContainer.appendChild(p);
});
}
/**
* Scroll to bottom of page if checkbox is checked
*/
function scrollToBottom() {
if (followLogsBtn.checked) {
window.scrollTo(0, document.body.scrollHeight);
}
}
/**
* Split string by newline char
* @param {string} str
*/
function splitLines(str) {
return str.split('\n').map(line => line.substring(line.lastIndexOf('\r') + 1));
}
/**
* Establish websocket connection
*/
function connect() {
const loc = window.location;
let new_uri = loc.protocol === 'https:' ? 'wss:' : 'ws:';
new_uri += "//" + loc.host;
new_uri += wsPath(loc);
var ws = new WebSocket(new_uri);
ws.onmessage = function (message) {
const buildEvent = JSON.parse(message.data);
if (buildEvent.type === 'finish') {
ws.close();
buildStatusTxt.replaceChild(document.createTextNode(buildEvent.message), buildStatusTxt.firstChild);
}
else {
appendLines(splitLines(buildEvent.message), buildEvent.type === 'err');
scrollToBottom();
}
}
}
connect();
followLogsBtn.checked = false;
followLogsBtn.addEventListener('change', scrollToBottom);
});