command-hook-utils.ts 721 B

1234567891011121314151617181920212223
  1. /**
  2. * Register a command hook in the OpenCode config if it doesn't already exist.
  3. * Returns true if the command was registered, false if it already existed.
  4. */
  5. export function registerCommandHook(
  6. opencodeConfig: Record<string, unknown>,
  7. commandName: string,
  8. template: string,
  9. description: string,
  10. ): boolean {
  11. const cmdConfig = (opencodeConfig as { command?: Record<string, unknown> })
  12. .command;
  13. if (cmdConfig?.[commandName]) return false;
  14. if (!opencodeConfig.command)
  15. (opencodeConfig as Record<string, unknown>).command = {};
  16. (
  17. (opencodeConfig as Record<string, unknown>).command as Record<
  18. string,
  19. unknown
  20. >
  21. )[commandName] = { template, description };
  22. return true;
  23. }