Something went wrong. Try again.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243import { mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';import path from 'node:path';import { DEFAULT_TRACKING_SETTINGS, normalizeTrackingSettings } from './dist/tracking-config.mjs';import { normalizeAvatarSettings } from './dist/avatar-definitions.mjs';export { DEFAULT_TRACKING_SETTINGS, normalizeTrackingSettings } from './dist/tracking-config.mjs';
export class TrackingSettingsStore { constructor(file) { this.file = file; this.saved = { ...DEFAULT_TRACKING_SETTINGS }; this.warning = null; this.legacyAvatarSettings = {}; try { const data = JSON.parse(readFileSync(file, 'utf8')); if (data.version !== 1 || !data.settings || typeof data.settings !== 'object') throw Error('Unrecognized settings file'); this.saved = normalizeTrackingSettings(data.settings); const legacy = normalizeAvatarSettings('ghost', data.settings); for (const key of Object.keys(legacy)) if (Object.hasOwn(data.settings, key)) this.legacyAvatarSettings[key] = legacy[key]; } catch (error) { if (error.code !== 'ENOENT') this.warning = 'Could not read saved settings. Defaults are active; Save will replace the unreadable file.'; } } save(settings) { const next = normalizeTrackingSettings(settings); mkdirSync(path.dirname(this.file), { recursive: true }); writeFileSync( this.file + '.tmp', JSON.stringify({ version: 1, settings: next }, null, 2) + '\n', 'utf8', ); renameSync(this.file + '.tmp', this.file); this.saved = next; this.warning = null; } status() { return { savedSettings: { ...this.saved }, warning: this.warning }; }}