/ components / StructuredDiff / colorDiff.ts
colorDiff.ts
 1  import {
 2    ColorDiff,
 3    ColorFile,
 4    getSyntaxTheme as nativeGetSyntaxTheme,
 5    type SyntaxTheme,
 6  } from 'color-diff-napi'
 7  import { isEnvDefinedFalsy } from '../../utils/envUtils.js'
 8  
 9  export type ColorModuleUnavailableReason = 'env'
10  
11  /**
12   * Returns a static reason why the color-diff module is unavailable, or null if available.
13   * 'env' = disabled via CLAUDE_CODE_SYNTAX_HIGHLIGHT
14   *
15   * The TS port of color-diff works in all build modes, so the only way to
16   * disable it is via the env var.
17   */
18  export function getColorModuleUnavailableReason(): ColorModuleUnavailableReason | null {
19    if (isEnvDefinedFalsy(process.env.CLAUDE_CODE_SYNTAX_HIGHLIGHT)) {
20      return 'env'
21    }
22    return null
23  }
24  
25  export function expectColorDiff(): typeof ColorDiff | null {
26    return getColorModuleUnavailableReason() === null ? ColorDiff : null
27  }
28  
29  export function expectColorFile(): typeof ColorFile | null {
30    return getColorModuleUnavailableReason() === null ? ColorFile : null
31  }
32  
33  export function getSyntaxTheme(themeName: string): SyntaxTheme | null {
34    return getColorModuleUnavailableReason() === null
35      ? nativeGetSyntaxTheme(themeName)
36      : null
37  }