From 7ca7d478cc85d2db3fcdb60f8b12f20415836f9b Mon Sep 17 00:00:00 2001 From: Jaime Idolpx Date: Tue, 9 Jun 2026 13:31:14 -0400 Subject: [PATCH] feat(settings): add support for mirroring settings to SD card if mounted --- src/app/settings.ts | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/app/settings.ts b/src/app/settings.ts index c2182e4..3d77a06 100644 --- a/src/app/settings.ts +++ b/src/app/settings.ts @@ -16,12 +16,14 @@ import { createFolder, getFileContents, putFileContents, + stat, } from './webdav'; /** The canonical paths on the WebDAV server. */ export const SETTINGS_PATH = '/.sys/config.json'; export const DEVICES_PATH = '/.sys/devices.json'; const SETTINGS_DIR = '/.sys'; +const SD_SYS_DIR = '/sd/.sys'; /** * How long to wait after the last change before writing to disk. @@ -75,17 +77,29 @@ export async function readSettings(): Promise { * indentation so the file is diff-friendly. */ export async function writeSettings(config: SettingsConfig): Promise { - try { - await createFolder(SETTINGS_DIR, true); - } catch { - /* directory may already exist; ignore */ - } + try { await createFolder(SETTINGS_DIR, true); } catch { /* exists */ } + const { iec, ...mainConfig } = config; const { devices: iecDevices, ...iecBusConfig } = (iec ?? {}) as any; + const configJson = JSON.stringify({ ...mainConfig, iec: iecBusConfig }, null, 2) + '\n'; + const devicesJson = JSON.stringify({ iec: { devices: iecDevices } }, null, 2) + '\n'; + await Promise.all([ - putFileContents(SETTINGS_PATH, JSON.stringify({ ...mainConfig, iec: iecBusConfig }, null, 2) + '\n'), - putFileContents(DEVICES_PATH, JSON.stringify({ iec: { devices: iecDevices } }, null, 2) + '\n'), + putFileContents(SETTINGS_PATH, configJson), + putFileContents(DEVICES_PATH, devicesJson), ]); + + // Mirror to /sd/.sys/ if the SD card is mounted. + try { + const sdStat = await stat('/sd'); + if (sdStat) { + try { await createFolder(SD_SYS_DIR, true); } catch { /* exists */ } + await Promise.all([ + putFileContents(SD_SYS_DIR + '/config.json', configJson), + putFileContents(SD_SYS_DIR + '/devices.json', devicesJson), + ]); + } + } catch { /* /sd unreachable — skip mirror */ } } export type SaveStatus =