/**
* hn config management
*
* Config file: ~/.config/hypernote/hn.json
*/
import fs from 'bare-fs'
import path from 'bare-path'
import os from 'bare-os'
const CONFIG_DIR = path.join(os.homedir(), '.config', 'hypernote')
const CONFIG_FILE = path.join(CONFIG_DIR, 'hn.json')
const DEFAULTS = {
server: 'http://localhost:4040',
token: null
}
export function loadConfig () {
try {
const data = fs.readFileSync(CONFIG_FILE, 'utf-8')
return { ...DEFAULTS, ...JSON.parse(data) }
} catch {
return { ...DEFAULTS }
}
}
export function saveConfig (cfg) {
try {
fs.mkdirSync(CONFIG_DIR, { recursive: true })
} catch {}
fs.writeFileSync(CONFIG_FILE, JSON.stringify(cfg, null, 2) + '\n')
}
export function getServer (flags) {
if (flags.server) return flags.server
const cfg = loadConfig()
return cfg.server || DEFAULTS.server
}
export function getToken (flags) {
if (flags.token) return flags.token
const cfg = loadConfig()
return cfg.token || null
}
/**
* Check if hn has been set up.
* Passes if device-identity.json exists OR a token is already configured.
*/
export function hasDeviceIdentity () {
// Check device-identity.json
const deviceFile = path.join(CONFIG_DIR, 'device-identity.json')
try {
fs.statSync(deviceFile)
return true
} catch {}
// Existing setup with token
const cfg = loadConfig()
if (cfg.token) return true
return false
}