exit.ts 1.3 KB

12345678910111213141516171819202122232425262728293031
  1. /**
  2. * CLI exit helpers for subcommand handlers.
  3. *
  4. * Consolidates the 4-5 line "print + lint-suppress + exit" block that was
  5. * copy-pasted ~60 times across `claude mcp *` / `claude plugin *` handlers.
  6. * The `: never` return type lets TypeScript narrow control flow at call sites
  7. * without a trailing `return`.
  8. */
  9. /* eslint-disable custom-rules/no-process-exit -- centralized CLI exit point */
  10. // `return undefined as never` (not a post-exit throw) — tests spy on
  11. // process.exit and let it return. Call sites write `return cliError(...)`
  12. // where subsequent code would dereference narrowed-away values under mock.
  13. // cliError uses console.error (tests spy on console.error); cliOk uses
  14. // process.stdout.write (tests spy on process.stdout.write — Bun's console.log
  15. // doesn't route through a spied process.stdout.write).
  16. /** Write an error message to stderr (if given) and exit with code 1. */
  17. export function cliError(msg?: string): never {
  18. // biome-ignore lint/suspicious/noConsole: centralized CLI error output
  19. if (msg) console.error(msg)
  20. process.exit(1)
  21. return undefined as never
  22. }
  23. /** Write a message to stdout (if given) and exit with code 0. */
  24. export function cliOk(msg?: string): never {
  25. if (msg) process.stdout.write(msg + '\n')
  26. process.exit(0)
  27. return undefined as never
  28. }