9 lines
784 B
JavaScript
9 lines
784 B
JavaScript
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
|
|
export class StateRecovery {
|
|
constructor({ file = path.join(process.env.XDG_STATE_HOME || path.join(process.env.HOME || '/tmp', '.local/state'), 'jarvis/state.json') } = {}) { this.file = file; }
|
|
load() { try { const state = JSON.parse(fs.readFileSync(this.file, 'utf8')); return { state: state.state === 'SLEEPING' ? 'SLEEPING' : 'ARMED', mode: state.mode || 'chat', recovered: true }; } catch { return { state: 'ARMED', mode: 'chat', recovered: false }; } }
|
|
save({ state, mode = 'chat' }) { fs.mkdirSync(path.dirname(this.file), { recursive: true }); const temp = `${this.file}.tmp`; fs.writeFileSync(temp, JSON.stringify({ state, mode, savedAt: new Date().toISOString() }) + '\n'); fs.renameSync(temp, this.file); }
|
|
}
|