forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusageSettings.ts
More file actions
39 lines (34 loc) · 1.25 KB
/
Copy pathusageSettings.ts
File metadata and controls
39 lines (34 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { app } from 'electron'
import { join } from 'path'
import { readFileSync, writeFileSync } from 'fs'
import type { UsageSettings } from '../../shared/types'
import { DEFAULT_RETENTION_DAYS, normalizeRetentionDays } from './usageRetention'
const DEFAULTS: UsageSettings = { enabled: true, retentionDays: DEFAULT_RETENTION_DAYS }
function file(): string {
return join(app.getPath('userData'), 'usage-settings.json')
}
// Coerce a partial/untrusted settings object into a fully-valid one.
function sanitize(raw: Partial<UsageSettings>): UsageSettings {
return {
enabled: raw.enabled !== false,
retentionDays: normalizeRetentionDays(raw.retentionDays)
}
}
// Read the persisted settings, defaulting to enabled with the default retention.
// Never throws — a missing/corrupt file yields the defaults.
export function getUsageSettings(): UsageSettings {
try {
return sanitize(JSON.parse(readFileSync(file(), 'utf-8')) as Partial<UsageSettings>)
} catch {
return { ...DEFAULTS }
}
}
export function setUsageSettings(next: UsageSettings): UsageSettings {
const value = sanitize(next)
try {
writeFileSync(file(), JSON.stringify(value), 'utf-8')
} catch (e) {
console.warn('[usage] failed to persist settings:', e)
}
return value
}