UNPKG

@yankeeinlondon/claudine

Version:

A simple library to help with Claude Code

46 lines (42 loc) 1.22 kB
import readline from "node:readline"; import { isNumber } from "inferred-types"; import { createKindError } from "@yankeeinlondon/kind-error"; //#region src/constants.ts const SUCCESS = 0; const ERROR = 1; const BLOCKING_ERROR = 2; //#endregion //#region src/errors.ts const Unexpected = createKindError("Unexpected", { library: "claudine" }); //#endregion //#region src/createHook.ts /** * **createHook**(hook) * * A builder pattern for handing Claude Code hooks. * * - [Docs](https://docs.anthropic.com/en/docs/claude-code/hooks) for Claude Hooks */ function createHook(_hook) { return { handler(handler) { try { const readStdIn = async function readStdin() { const rl = readline.createInterface({ input: process.stdin }); const lines = []; for await (const line of rl) lines.push(line); return lines.join("\n"); }; return { async handle() { const text = await readStdIn(); const event = JSON.parse(text); const resp = await handler(event); if (isNumber(resp)) process.exit(resp); else console.log(JSON.stringify(resp)); } }; } catch (e) { throw Unexpected.proxy(e); } } }; } //#endregion export { BLOCKING_ERROR, ERROR, SUCCESS, Unexpected, createHook };