All files / nodejs/src/daemon delete-daemon-state.ts

0% Statements 0/7
0% Branches 0/4
0% Functions 0/1
0% Lines 0/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 { rm } from 'node:fs/promises';
import { homedir } from 'node:os';
import { join } from 'node:path';
 
/**
 * Options for deleting daemon state.
 */
export type DeleteDaemonStateOptions = {
  /** UNIX socket path. Defaults to ~/.ocap/daemon.sock. */
  socketPath?: string;
  /** SQLite database filename. Defaults to ~/.ocap/kernel.sqlite. */
  dbFilename?: string;
};
 
/**
 * Delete all daemon state: kernel DB, bundles cache, and socket.
 *
 * @param options - Optional overrides for file paths.
 */
export async function deleteDaemonState(
  options?: DeleteDaemonStateOptions,
): Promise<void> {
  const ocapDir = join(homedir(), '.ocap');
  const socketPath = options?.socketPath ?? join(ocapDir, 'daemon.sock');
  const dbFilename = options?.dbFilename ?? join(ocapDir, 'kernel.sqlite');
  const bundlesDir = join(ocapDir, 'bundles');
 
  const pidPath = join(ocapDir, 'daemon.pid');
  const logPath = join(ocapDir, 'daemon.log');
 
  await Promise.all([
    rm(dbFilename, { force: true }),
    rm(socketPath, { force: true }),
    rm(bundlesDir, { recursive: true, force: true }),
    rm(pidPath, { force: true }),
    rm(logPath, { force: true }),
  ]);
}