# nrjdalal/karabiner-human-config architecture

> Architecture diagram of the GitHub repository nrjdalal/karabiner-human-config, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nrjdalal/karabiner-human-config
- Repository: https://github.com/nrjdalal/karabiner-human-config

## Overview

##### Project type and purpose
- This is a Node.js CLI tool (npm package) named karabiner-human-config.
- Its purpose is to take a human‐friendly JSON config (`konfig.json`) and generate a Karabiner-Elements JSON file (`karabiner.json`) with complex key-mapping rules.

##### File structure
- Top‐level:
- `bin/` – CLI entrypoint (`index.ts`) and transformation libraries (`lib/*.ts`)
- `constants/` – alias definitions (`custom-aliases.ts`, `manipulator-keys.ts`)
- `examples/` – sample input (`nrjdalal.json`) and sample output (`karabiner.json`)
- config/build: `tsconfig.json`, `tsup.config.ts`
- release pipeline: `.changeset/`, `.github/workflows/release.yml`
- No frontend or database; the architecture is a simple command‐line → parser/transformer → filesystem pipeline.

##### README for additional insights
- Describes CLI usage, input/output defaults, aliasing system, transformation rules.

##### Components & interactions
a. Main components
1. User & Shell: invokes `npx karabiner-human-config`.
2. CLI Entrypoint (`bin/index.ts` → compiled JS)
3. Parser & Normalizer (`bin/lib/from.ts`, `to.ts`, helper utils)
4. Alias Resolver (`constants/custom-aliases.ts`, `manipulator-keys.ts`)
5. Transformer (`bin/lib/transformer.ts`)
6. File I/O module (`bin/utils/index.ts`)
7. Generated Artifact (`karabiner.json`)
8. External Consumer: Karabiner-Elements (reads the generated JSON)

b. Relationships & data flow
- CLI reads `konfig.json` via File I/O
- Raw config → Parser
- Parser + Alias Resolver → Abstract representation
- Abstract representation → Transformer → Karabiner JSON schema
- Transformer output → File I/O writes `karabiner.json`
- Karabiner-Elements imports `karabiner.json` at runtime

c. Architectural patterns & principles
- Single‐responsibility modules: each file in `bin/lib` handles one aspect of transformation
- Centralized constants for alias mapping → easy extensibility
- Pipeline pattern: input → parse → transform → output
- Decoupled I/O → pure transformation logic (eases testing)

d. Key technologies & libraries
- Node.js & TypeScript
- tsup (bundler)
- Changesets & GitHub Actions (release automation)
- Karabiner-Elements JSON schema (external target)

##### Detailed coverage
- Build vs. runtime separation (e.g., tsup compile step before CLI execution)
- Inputs & outputs explicitly: `konfig.json` → `karabiner.json`

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Runtime Pipeline
    User["User & Shell"]:::runtime
    subgraph "Runtime Pipeline"
        direction TB
        CLI["CLI Entrypoint"]:::runtime
        FileIO["File I/O Module"]:::runtime
        InputConfig["konfig.json"]:::config
        ParserSub["Parser & Normalizer"]:::runtime
        Transformer["Transformer"]:::runtime
        AliasSub["Alias Resolver"]:::config
        Output["karabiner.json"]:::config
        Karabiner["Karabiner-Elements"]:::external
    end

    %% Examples
    subgraph "Examples" 
        direction TB
        ExampleIn["nrjdalal.json"]:::config
        ExampleOut["karabiner.json"]:::config
    end

    %% Development & Release
    subgraph "Development & Release" 
        direction TB
        TSConfig["tsconfig.json"]:::dev
        TSUp["tsup.config.ts"]:::dev
        ChangesetDir[".changeset/"]:::dev
        Workflow[".github/workflows/release.yml"]:::dev
        ChangesetJS[".github/changeset-version.js"]:::dev
    end

    %% Connections Runtime
    User -->|runs| CLI
    CLI -->|reads config| FileIO
    FileIO -->|loads JSON| InputConfig
    FileIO -->|provides raw config| ParserSub
    ParserSub -->|resolves aliases| AliasSub
    AliasSub -->|provides mapping| ParserSub
    ParserSub -->|abstract representation| Transformer
    Transformer -->|generates JSON| FileIO
    FileIO -->|writes output| Output
    Output -->|import| Karabiner

    %% Examples links
    CLI -.->|can use sample| ExampleIn
    Output -.->|sample output| ExampleOut

    %% Dev & Release links
    TSConfig --> TSUp
    TSUp --> CLI
    ChangesetDir --> Workflow
    Workflow --> CLI
    ChangesetJS --> Workflow

    %% Click Events
    click CLI "https://github.com/nrjdalal/karabiner-human-config/blob/main/bin/index.ts"
    click FileIO "https://github.com/nrjdalal/karabiner-human-config/blob/main/bin/utils/index.ts"
    click InputConfig "https://github.com/nrjdalal/karabiner-human-config/blob/main/konfig.json"
    click Transformer "https://github.com/nrjdalal/karabiner-human-config/blob/main/bin/lib/transformer.ts"
    click ExampleIn "https://github.com/nrjdalal/karabiner-human-config/blob/main/examples/nrjdalal.json"
    click ExampleOut "https://github.com/nrjdalal/karabiner-human-config/blob/main/examples/karabiner.json"
    click TSConfig "https://github.com/nrjdalal/karabiner-human-config/blob/main/tsconfig.json"
    click TSUp "https://github.com/nrjdalal/karabiner-human-config/blob/main/tsup.config.ts"
    click ChangesetDir "https://github.com/nrjdalal/karabiner-human-config/tree/main/.changeset/"
    click Workflow "https://github.com/nrjdalal/karabiner-human-config/blob/main/.github/workflows/release.yml"
    click ChangesetJS "https://github.com/nrjdalal/karabiner-human-config/blob/main/.github/changeset-version.js"

    %% Parser Modules
    subgraph "Parser & Normalizer Modules"
        direction TB
        From["from.ts"]:::runtime
        To["to.ts"]:::runtime
        ToAfter["to-after-key-up.ts"]:::runtime
        ToAlone["to-if-alone.ts"]:::runtime
        ToHeld["to-if-held-down.ts"]:::runtime
    end
    ParserSub --> From
    ParserSub --> To
    ParserSub --> ToAfter
    ParserSub --> ToAlone
    ParserSub --> ToHeld

    click From "https://github.com/nrjdalal/karabiner-human-config/blob/main/bin/lib/from.ts"
    click To "https://github.com/nrjdalal/karabiner-human-config/blob/main/bin/lib/to.ts"
    click ToAfter "https://github.com/nrjdalal/karabiner-human-config/blob/main/bin/lib/to-after-key-up.ts"
    click ToAlone "https://github.com/nrjdalal/karabiner-human-config/blob/main/bin/lib/to-if-alone.ts"
    click ToHeld "https://github.com/nrjdalal/karabiner-human-config/blob/main/bin/lib/to-if-held-down.ts"

    %% Alias Resolver Modules
    subgraph "Alias Resolver Modules"
        direction TB
        CustomAliases["custom-aliases.ts"]:::config
        ManipulatorKeys["manipulator-keys.ts"]:::config
        AliasIndex["index.ts"]:::config
    end
    AliasSub --> CustomAliases
    AliasSub --> ManipulatorKeys
    AliasSub --> AliasIndex

    click CustomAliases "https://github.com/nrjdalal/karabiner-human-config/blob/main/constants/custom-aliases.ts"
    click ManipulatorKeys "https://github.com/nrjdalal/karabiner-human-config/blob/main/constants/manipulator-keys.ts"
    click AliasIndex "https://github.com/nrjdalal/karabiner-human-config/blob/main/constants/index.ts"

    %% Styles
    classDef runtime fill:#cfe2f3,stroke:#2a7fb8,color:#0b3d91
    classDef config fill:#d9ead3,stroke:#6aa84f,color:#274e13
    classDef dev fill:#f9cb9c,stroke:#e69138,color:#893800
    classDef external fill:#e0e0e0,stroke:#888888,color:#444444
```
