// How the CLI reads its saved session file back into a `Client`. // // A missing file means "not logged in" and returns null. A file that exists but // cannot be read back into a client (bad JSON, a missing field, a key of the // wrong length) throws an error saying the session file is corrupt, so the CLI // can tell the user which of the two it is. Needs `init()` first. import { existsSync, readFileSync } from "node:fs"; import type { ApiClientOptions } from "./api/client.js"; import { Client } from "./client.js"; export const loadSession = ( path: string, apiOptions?: ApiClientOptions, ): Client | null => { if (!existsSync(path)) return null; try { return Client.fromJSON( JSON.parse(readFileSync(path, "utf-8")), apiOptions, ); } catch (err) { const reason = err instanceof Error ? err.message : String(err); throw new Error(`Session file ${path} is corrupt: ${reason}`); } };