Files
archery/scripts/build.js
Cory Sanin b20bbf40fc
All checks were successful
App Image CI / Build app image (pull_request) Successful in -1m26s
NPM Audit Check / Check NPM audit (pull_request) Successful in -2m14s
Track and display build authors
2025-09-23 16:09:31 -05:00

74 lines
2.4 KiB
JavaScript

document.addEventListener('DOMContentLoaded', function () {
const logContainer = this.getElementById('logs');
const followLogsBtn = this.getElementById('followCheckmark');
const buildStatusTxt = this.getElementById('buildStatus');
let started = logContainer.childElementCount > 0;
/**
* 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);
}
}
function handleScrollToggleClick() {
window.scrollTo(0, followLogsBtn.checked ? document.body.scrollHeight : 0);
}
/**
* 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 += loc.pathname + 'ws';
var ws = new WebSocket(new_uri);
ws.onmessage = function (message) {
const buildEvent = JSON.parse(message.data);
if (!started) {
started = true;
buildStatusTxt.replaceChild(document.createTextNode('running'), buildStatusTxt.firstChild);
}
if (buildEvent.type === 'finish') {
ws.close();
buildStatusTxt.replaceChild(document.createTextNode(buildEvent.message), buildStatusTxt.firstChild);
appendLines([`finished: ${buildEvent.message}`]);
const cancelBtn = document.getElementById('cancelRow');
cancelBtn.parentElement.removeChild(cancelBtn);
}
else {
appendLines(splitLines(buildEvent.message), buildEvent.type === 'err');
scrollToBottom();
}
}
}
connect();
followLogsBtn.checked = false;
followLogsBtn.addEventListener('change', handleScrollToggleClick);
});