build: improve update logic

This commit is contained in:
leukipp
2025-09-06 19:07:57 +02:00
parent 3ea65a4af8
commit f44d998016
4 changed files with 187 additions and 88 deletions
+1
View File
@@ -1,3 +1,4 @@
node_modules/ node_modules/
docs/ docs/
out/ out/
build.json
+34 -2
View File
@@ -1,4 +1,22 @@
const fs = require("fs"); const fs = require("fs");
const jszip = require("jszip");
const crypto = require("crypto");
const path = require("path");
const date = new Date().toISOString();
const generateBuildFile = (platform, arch, maker) => {
const package = fs.readFileSync(path.join(__dirname, "package.json"), "utf8");
const hash = crypto.createHash("sha256").update(package);
const data = {
id: hash.digest("hex").slice(-6),
platform: platform,
arch: arch,
maker: maker,
date: date,
};
return JSON.stringify(data, null, 2);
};
module.exports = { module.exports = {
packagerConfig: { packagerConfig: {
@@ -33,10 +51,24 @@ module.exports = {
}, },
], ],
hooks: { hooks: {
postPackage: async (config, results) => {
for (const outputPath of results.outputPaths) {
const [name, platform, arch] = path.basename(outputPath).split("-");
const buildFile = path.join(outputPath, "resources", "app", "build.json");
fs.writeFileSync(buildFile, generateBuildFile(platform, arch, "deb"), { encoding: "utf8" });
}
},
postMake: async (config, results) => { postMake: async (config, results) => {
for (const result of results) { for (const result of results) {
const artifacts = []; const artifacts = [];
result.artifacts.forEach((artifact) => { for (const artifact of result.artifacts) {
if (artifact.includes(".zip")) {
const [name, platform, arch] = path.basename(artifact).split("-");
const buildFile = path.join(`${name}-${platform}-${arch}`, "resources", "app", "build.json");
const zip = await jszip.loadAsync(fs.readFileSync(artifact));
zip.file(buildFile, generateBuildFile(platform, arch, "zip"));
fs.writeFileSync(artifact, await zip.generateAsync({ type: "nodebuffer" }));
}
if (artifact.includes("amd64")) { if (artifact.includes("amd64")) {
const renamed = artifact.replace("amd64", "x64"); const renamed = artifact.replace("amd64", "x64");
fs.renameSync(artifact, renamed); fs.renameSync(artifact, renamed);
@@ -44,7 +76,7 @@ module.exports = {
} else { } else {
artifacts.push(artifact); artifacts.push(artifact);
} }
}); }
result.artifacts = artifacts; result.artifacts = artifacts;
} }
}, },
+83 -19
View File
@@ -7,6 +7,7 @@ const hardware = require("./js/hardware");
const webview = require("./js/webview"); const webview = require("./js/webview");
const { app } = require("electron"); const { app } = require("electron");
global.APP = global.APP || {};
global.ARGS = global.ARGS || {}; global.ARGS = global.ARGS || {};
if (!process.env.DISPLAY) { if (!process.env.DISPLAY) {
@@ -23,20 +24,85 @@ if (!process.env.DISPLAY) {
* initialization tasks. * initialization tasks.
*/ */
app.whenReady().then(async () => { app.whenReady().then(async () => {
const lock = app.requestSingleInstanceLock(); if (!(await initApp()) || !(await initArgs())) {
if (!lock) { return;
console.error("TouchKio is already running"); }
// Show used arguments
console.log(`Arguments: ${JSON.stringify(ARGS, null, 2)}`);
// Chained init functions
const chained = [webview.init, hardware.init, integration.init];
for (const init of chained) {
if (!(await init())) {
break;
}
}
});
/**
* Initializes the global app object.
*
* @returns {bool} Returns true if the initialization was successful.
*/
const initApp = async () => {
const packageJsonPath = path.join(app.getAppPath(), "package.json");
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
const buildJsonPath = path.join(app.getAppPath(), "build.json");
const buildFileExists = fs.existsSync(buildJsonPath);
// Set required app infos
APP.name = app.getName();
APP.title = packageJson.title;
APP.version = app.getVersion();
APP.path = app.getAppPath();
APP.config = app.getPath("userData");
// Set additional update infos
APP.homepage = `https://github.com/${packageJson.author}/${packageJson.name}`;
APP.releases = {
url: `https://api.github.com/repos/${packageJson.author}/${packageJson.name}/releases`,
latest: null,
};
APP.scripts = {
install: `https://raw.githubusercontent.com/${packageJson.author}/${packageJson.name}/main/install.sh`,
};
// Set additional build infos
APP.build = {};
if (buildFileExists) {
APP.build = JSON.parse(fs.readFileSync(buildJsonPath, "utf8"));
}
// Request single app instance lock
if (!app.requestSingleInstanceLock()) {
console.error(`${APP.title} is already running`);
return app.quit(); return app.quit();
} }
return true;
};
/**
* Initializes the global args object.
*
* @returns {bool} Returns true if the initialization was successful.
*/
const initArgs = async () => {
let args = parseArgs(process); let args = parseArgs(process);
let argsProvided = !!Object.keys(args).length; let argsProvided = !!Object.keys(args).length;
let argsFilePath = path.join(app.getPath("userData"), "Arguments.json"); let argsFilePath = path.join(APP.config, "Arguments.json");
let argsFileExists = fs.existsSync(argsFilePath); let argsFileExists = fs.existsSync(argsFilePath);
// Show version and release info // Show version and release info
if ("help" in args || "version" in args) { if ("help" in args || "version" in args) {
console.log(`${app.getName()}-v${app.getVersion()}, https://github.com/leukipp/touchkio`); let build = "";
if (APP.build.id) {
build = ` (${APP.build.id}), built on ${APP.build.date} (${APP.build.platform}-${APP.build.arch}-${APP.build.maker})`;
}
console.log(`${APP.name}-v${APP.version}${build}\n${APP.homepage}`);
return app.quit(); return app.quit();
} }
@@ -56,19 +122,10 @@ app.whenReady().then(async () => {
if (!Array.isArray(args.web_url)) { if (!Array.isArray(args.web_url)) {
args.web_url = args.web_url.split(",").map((url) => url.trim()); args.web_url = args.web_url.split(",").map((url) => url.trim());
} }
global.ARGS = args; ARGS = args;
// Show used arguments return true;
console.log(`Arguments: ${JSON.stringify(global.ARGS, null, 2)}`); };
// Chained init functions
const chained = [webview.init, hardware.init, integration.init];
for (const init of chained) {
if (!(await init())) {
break;
}
}
});
/** /**
* Parses command-line arguments from the given process object. * Parses command-line arguments from the given process object.
@@ -155,6 +212,7 @@ const promptArgs = async (proc) => {
// Prompt questions and wait for the answers // Prompt questions and wait for the answers
let args = {}; let args = {};
let ignore = []; let ignore = [];
try {
for (const { key, question, fallback } of prompts) { for (const { key, question, fallback } of prompts) {
if (key === "mqtt") { if (key === "mqtt") {
const prompt = `${question} (${fallback}): `; const prompt = `${question} (${fallback}): `;
@@ -182,7 +240,13 @@ const promptArgs = async (proc) => {
} }
} }
} }
} catch (error) {
console.error(`\n${error.message}`);
args = {};
app.quit();
} finally {
read.close(); read.close();
}
return args; return args;
}; };
@@ -223,7 +287,7 @@ const readArgs = (path) => {
*/ */
const encrypt = (value) => { const encrypt = (value) => {
const iv = crypto.randomBytes(16); const iv = crypto.randomBytes(16);
const key = crypto.scryptSync(hardware.getMachineId(), app.getName(), 32); const key = crypto.scryptSync(hardware.getMachineId(), APP.name, 32);
const cipher = crypto.createCipheriv("aes-256-cbc", key, iv); const cipher = crypto.createCipheriv("aes-256-cbc", key, iv);
let encrypted = cipher.update(value, "utf8", "hex"); let encrypted = cipher.update(value, "utf8", "hex");
encrypted += cipher.final("hex"); encrypted += cipher.final("hex");
@@ -239,7 +303,7 @@ const encrypt = (value) => {
const decrypt = (value) => { const decrypt = (value) => {
const p = Buffer.from(value, "base64").toString("utf8").split(":"); const p = Buffer.from(value, "base64").toString("utf8").split(":");
const iv = Buffer.from(p.shift(), "hex"); const iv = Buffer.from(p.shift(), "hex");
const key = crypto.scryptSync(hardware.getMachineId(), app.getName(), 32); const key = crypto.scryptSync(hardware.getMachineId(), APP.name, 32);
const decipher = crypto.createDecipheriv("aes-256-cbc", key, iv); const decipher = crypto.createDecipheriv("aes-256-cbc", key, iv);
const buffer = Buffer.from(p.join(":"), "hex"); const buffer = Buffer.from(p.join(":"), "hex");
let decrypted = decipher.update(buffer, "binary", "utf8"); let decrypted = decipher.update(buffer, "binary", "utf8");
+43 -41
View File
@@ -1,5 +1,13 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Read arguments
ARG_UPDATE=false
for arg in "$@"; do
if [ "$arg" = "update" ]; then
ARG_UPDATE=true
fi
done
# Determine system architecture # Determine system architecture
echo -e "Determining system architecture..." echo -e "Determining system architecture..."
@@ -12,57 +20,39 @@ case "$(uname -m)" in
ARCH="x64" ARCH="x64"
;; ;;
*) *)
echo "Architecture $(uname -m) running $BITS-bit operating system is not supported." { echo "Architecture $(uname -m) running $BITS-bit operating system is not supported."; exit 1; }
exit 1
;; ;;
esac esac
if [ "$BITS" -ne 64 ]; then [ "$BITS" -eq 64 ] || { echo "Architecture $ARCH running $BITS-bit operating system is not supported."; exit 1; }
echo "Architecture $ARCH running $BITS-bit operating system is not supported."
exit 1
fi
echo "Architecture $ARCH running $BITS-bit operating system is supported." echo "Architecture $ARCH running $BITS-bit operating system is supported."
# Download the latest .deb package # Download the latest .deb package
echo -e "\nDownloading the latest release..." echo -e "\nDownloading the latest release..."
TMP_DIR=$(mktemp -d)
DEB_URL=$(wget -qO- https://api.github.com/repos/leukipp/touchkio/releases/latest | \ DEB_URL=$(wget -qO- https://api.github.com/repos/leukipp/touchkio/releases/latest | \
grep -o "\"browser_download_url\": \"[^\"]*_${ARCH}\.deb\"" | \ grep -o "\"browser_download_url\": \"[^\"]*_${ARCH}\.deb\"" | \
sed 's/"browser_download_url": "//;s/"//g') sed 's/"browser_download_url": "//;s/"//g')
TMP_DIR=$(mktemp -d)
DEB_PATH="${TMP_DIR}/$(basename "$DEB_URL")" DEB_PATH="${TMP_DIR}/$(basename "$DEB_URL")"
chmod 755 "$TMP_DIR" chmod 755 "$TMP_DIR"
if [ -z "$DEB_URL" ]; then [ -z "$DEB_URL" ] && { echo "Download url for .deb file not found."; exit 1; }
echo "Download url for .deb file not found." wget --show-progress -q -O "$DEB_PATH" "$DEB_URL" || { echo "Failed to download the .deb file."; exit 1; }
exit 1
fi
if ! wget --show-progress -q -O "$DEB_PATH" "$DEB_URL"; then
echo "Failed to download the .deb file."
exit 1
fi
# Install the latest .deb package # Install the latest .deb package
echo -e "\nInstalling the latest release..." echo -e "\nInstalling the latest release..."
if ! command -v apt &> /dev/null; then command -v apt &> /dev/null || { echo "Package manager apt was not found."; exit 1; }
echo "Package manager apt was not found." sudo apt install -y "$DEB_PATH" || { echo "Installation of .deb file failed."; exit 1; }
exit 1
fi
if ! sudo apt install "$DEB_PATH"; then
echo "Installation of .deb file failed."
exit 1
fi
# Create the systemd user service # Create the systemd user service
echo -e "\nCreating systemd user service..." echo -e "\nCreating systemd user service..."
SERVICE_FILE="$HOME/.config/systemd/user/touchkio.service" SERVICE_NAME="touchkio.service"
mkdir -p "$(dirname "$SERVICE_FILE")" || { echo "Failed to create directory for $SERVICE_FILE"; exit 1; } SERVICE_FILE="$HOME/.config/systemd/user/$SERVICE_NAME"
mkdir -p "$(dirname "$SERVICE_FILE")" || { echo "Failed to create directory for $SERVICE_FILE."; exit 1; }
SERVICE_CONTENT="[Unit] SERVICE_CONTENT="[Unit]
Description=TouchKio Description=TouchKio
After=graphical.target After=graphical.target
@@ -76,20 +66,28 @@ RestartSec=5s
[Install] [Install]
WantedBy=default.target" WantedBy=default.target"
if $ARG_UPDATE; then
if systemctl --user --quiet is-active "${SERVICE_NAME}"; then
systemctl --user restart "${SERVICE_NAME}"
echo "Existing $SERVICE_NAME restarted."
else
echo "Existing $SERVICE_NAME not running, start touchkio manually."
fi
exit 0
fi
SERVICE_CREATE=true SERVICE_CREATE=true
if [ -f "$SERVICE_FILE" ]; then if [ -f "$SERVICE_FILE" ]; then
read -p "Service $SERVICE_FILE exists, overwrite? (y/N) " overwrite read -p "Service $SERVICE_FILE exists, overwrite? (y/N) " overwrite
if [[ ! ${overwrite:-n} == [Yy]* ]]; then [[ ${overwrite:-n} == [Yy]* ]] || SERVICE_CREATE=false
SERVICE_CREATE=false
fi
fi fi
if [ "$SERVICE_CREATE" = true ]; then if $SERVICE_CREATE; then
echo "$SERVICE_CONTENT" > "$SERVICE_FILE" || { echo "Failed to write to $SERVICE_FILE"; exit 1; } echo "$SERVICE_CONTENT" > "$SERVICE_FILE" || { echo "Failed to write to $SERVICE_FILE."; exit 1; }
systemctl --user enable "$(basename "$SERVICE_FILE")" || { echo "Failed to enable service $SERVICE_FILE"; exit 1; } systemctl --user enable "$(basename "$SERVICE_FILE")" || { echo "Failed to enable service $SERVICE_FILE."; exit 1; }
echo "Service $SERVICE_FILE enabled" echo "Service $SERVICE_FILE enabled."
else else
echo "Service $SERVICE_FILE not created" echo "Service $SERVICE_FILE not created."
fi fi
# Export display variables # Export display variables
@@ -97,20 +95,22 @@ echo -e "\nExporting display variables..."
if [ -z "$DISPLAY" ]; then if [ -z "$DISPLAY" ]; then
export DISPLAY=":0" export DISPLAY=":0"
echo "DISPLAY was not set, defaulting to \"$DISPLAY\"" echo "DISPLAY was not set, defaulting to \"$DISPLAY\"."
else else
echo "DISPLAY is set to \"$DISPLAY\"" echo "DISPLAY is set to \"$DISPLAY\"."
fi fi
if [ -z "$WAYLAND_DISPLAY" ]; then if [ -z "$WAYLAND_DISPLAY" ]; then
export WAYLAND_DISPLAY="wayland-0" export WAYLAND_DISPLAY="wayland-0"
echo "WAYLAND_DISPLAY was not set, defaulting to \"$WAYLAND_DISPLAY\"" echo "WAYLAND_DISPLAY was not set, defaulting to \"$WAYLAND_DISPLAY\"."
else else
echo "WAYLAND_DISPLAY is set to \"$WAYLAND_DISPLAY\"" echo "WAYLAND_DISPLAY is set to \"$WAYLAND_DISPLAY\"."
fi fi
# Start the setup mode # Start the setup mode
echo "" echo ""
read -p "Start touchkio setup? (Y/n) " setup read -p "Start touchkio setup? (Y/n) " setup
if [[ ${setup:-y} == [Yy]* ]]; then if [[ ${setup:-y} == [Yy]* ]]; then
echo "/usr/bin/touchkio --setup" echo "/usr/bin/touchkio --setup"
/usr/bin/touchkio --setup /usr/bin/touchkio --setup
@@ -118,3 +118,5 @@ else
echo "/usr/bin/touchkio" echo "/usr/bin/touchkio"
/usr/bin/touchkio /usr/bin/touchkio
fi fi
exit 0