# ytetsuro/sabik architecture

> Architecture diagram of the GitHub repository ytetsuro/sabik, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-24). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ytetsuro/sabik
- Repository: https://github.com/ytetsuro/sabik

## Overview

##### Project type and purpose
- Sabik is a Node.js-based CLI tool that analyzes source code to compute various complexity metrics (Cyclomatic, Cognitive, Halstead, LOC, Maintainability) and emits an HTML or JSON report.
- It is purely modular, with no external servers or databases; it reads files from the local filesystem and writes static outputs.

##### File structure
Top-level src folders:
- src/Sabik – Core orchestration: CLI entry, DI container, file finder, main Sabik class
- src/Analyzer – Pipeline to generate ASTs and compute metrics; uses Adapter pattern for AST generation and multiple CodeMetricsCalculator submodules
- src/Language – Language-specific AST generators and converter classes for PHP and TypeScript
- src/Reporter – Two reporting branches:
– HTML: FileBuilder, ScriptBuilder, and in-browser front-end (Components, Entities, Infrastructure/Event)
– JSON: Converter + JSON FileBuilder
- bin – CLI shim scripts (run/run.cmd)

##### README insights
- CLI usage: npx sabik <target>
- Options: output path, HTML/JSON, include/exclude globs
- Supports JavaScript/TypeScript/PHP
- Metrics: Cyclomatic, Cognitive, Halstead, LOC, Maintainability

##### Main components & relationships
A. CLI Layer
– Bin scripts call src/index.ts → initializes DIContainer → instantiates Sabik class
B. Core Orchestrator (Sabik)
– Uses FileFinder to discover target files
– Delegates each file to Analyzer
C. Analyzer
1. AST Extraction
- Adapter → Language-specific ASTGenerator (TS/PHP) → yields ASTNodes
2. Metrics Calculation
- For each metric type (Cyclomatic, Cognitive, Halstead, LOC, Maintainability)
- Uses CodeMetricsCalculator modules (Calculator + Adapter + MetricsValue classes)
3. Aggregation
- FromASTNode and FromOtherMetrics modules combine results into unified Metrics objects
D. Reporter
1. JSON Reporter
- Converter (MetricsList, Summaries) → JSON FileBuilder → writes .json
2. HTML Reporter
- FileBuilder → emits static HTML, CSS
- ScriptBuilder → bundles front-end code
- Front-end SPA
– Components (Pages, Parts)
– Entities (domain models for Metrics, FileMetrics, etc.)
– Infrastructure/Event (Flux-style EventStore, Actions)
– Repository & ResourceLoader (fetch or load embedded JSON)
E. Filesystem I/O
– Input: source files
– Output: report files (HTML directory or JSON file)

##### Architectural patterns & principles
- Modular layering: CLI → Core → Analyzer → Language Adapters → Metrics Calculators → Reporter
- Adapter pattern: separates AST generation and metric extraction from core logic
- Dependency Injection: DIContainer resolves components (Sabik, Analyzer, Reporter)
- Front-end MVC/Flux: EventStore + Components + Entities + Repository
- Single Responsibility & Open-Closed: each metric, language, or report format implemented as a separate plugin/module

## Diagram (Mermaid)

```mermaid
flowchart TD
  subgraph "Node.js Runtime"
    direction TB

    subgraph "CLI & Orchestration"
      direction TB
      UCLI["User CLI"]:::cli
      BinSh["bin/run"]:::cli
      BinCmd["bin/run.cmd"]:::cli
      Index["src/index.ts"]:::cli
      DIContainer["src/Sabik/DIContainer.ts"]:::orchestration
      SabikCore["src/Sabik/Sabik.ts"]:::orchestration
      FileFinder["src/Sabik/FileFinder/FileFinder.ts"]:::orchestration
      FileModel["src/Sabik/FileFinder/File.ts"]:::orchestration
      FileSys["Filesystem I/O"]:::io
    end

    subgraph "Analyzer & Language Adapters"
      direction TB
      AnalyzerCore["src/Analyzer/Analyzer.ts"]:::analyzer
      ASTExtractor["src/Analyzer/ASTNodeExtractor.ts"]:::analyzer
      Adapter["src/Analyzer/Adapter/ASTGenerator.ts"]:::analyzer
      MetricsCalcs["Metrics Calculators"]:::metrics
      MetricsAgg["Metrics Aggregator"]:::analyzer
    end

    subgraph Reporter
      direction TB
      JSONR["JSON Reporter"]:::reporting
      HTMLR["HTML Reporter"]:::reporting
    end

    subgraph "Front-end SPA"
      direction TB
      FE["Front-end Bootstrap & SPA"]:::frontend
    end

  end

  %% Connections
  UCLI -->|executes| BinSh
  UCLI -->|executes| BinCmd
  BinSh -->|calls| Index
  BinCmd -->|calls| Index
  Index -->|uses| DIContainer
  DIContainer -->|instantiates| SabikCore
  SabikCore -->|discovers files| FileFinder
  FileFinder -->|reads| FileSys
  FileModel -->|models| FileSys
  SabikCore -->|delegates| AnalyzerCore
  AnalyzerCore -->|extracts AST| ASTExtractor
  ASTExtractor -->|via Adapter| Adapter
  AnalyzerCore -->|computes| MetricsCalcs
  ASTExtractor -->|yields| MetricsAgg
  MetricsCalcs -->|yields| MetricsAgg
  MetricsAgg -->|branches metrics| JSONR
  MetricsAgg -->|branches metrics| HTMLR
  JSONR -->|writes JSON| FileSys
  HTMLR -->|builds files| FileSys
  HTMLR -->|bundles script| FileSys
  HTMLR -->|serves| FE
  FE -->|fetches data| FileSys

  %% Click events
  click BinSh "https://github.com/ytetsuro/sabik/tree/develop/bin/run"
  click BinCmd "https://github.com/ytetsuro/sabik/blob/develop/bin/run.cmd"
  click Index "https://github.com/ytetsuro/sabik/blob/develop/src/index.ts"
  click DIContainer "https://github.com/ytetsuro/sabik/blob/develop/src/Sabik/DIContainer.ts"
  click SabikCore "https://github.com/ytetsuro/sabik/blob/develop/src/Sabik/Sabik.ts"
  click FileFinder "https://github.com/ytetsuro/sabik/blob/develop/src/Sabik/FileFinder/FileFinder.ts"
  click FileModel "https://github.com/ytetsuro/sabik/blob/develop/src/Sabik/FileFinder/File.ts"
  click AnalyzerCore "https://github.com/ytetsuro/sabik/blob/develop/src/Analyzer/Analyzer.ts"
  click ASTExtractor "https://github.com/ytetsuro/sabik/blob/develop/src/Analyzer/ASTNodeExtractor.ts"
  click Adapter "https://github.com/ytetsuro/sabik/blob/develop/src/Analyzer/Adapter/ASTGenerator.ts"
  click MetricsCalcs "https://github.com/ytetsuro/sabik/tree/develop/src/Analyzer/CodeMetricsCalculator/CognitiveComplexity"
  click MetricsAgg "https://github.com/ytetsuro/sabik/tree/develop/src/Analyzer/FromASTNode"
  click JSONR "https://github.com/ytetsuro/sabik/blob/develop/src/Reporter/JSON/Converter/MetricsList.ts"
  click HTMLR "https://github.com/ytetsuro/sabik/blob/develop/src/Reporter/HTML/ScriptBuilder.ts"
  click FE "https://github.com/ytetsuro/sabik/blob/develop/src/Reporter/HTML/FrontEnd/Bootstrap.ts"

  %% Styles
  classDef cli fill:#D0E6FF,stroke:#0366D6,color:#0366D6,stroke-width:1px;
  classDef orchestration fill:#B3D9FF,stroke:#0366D6,color:#0366D6,stroke-width:1px;
  classDef analyzer fill:#DFF2D8,stroke:#2E8B57,color:#2E8B57,stroke-width:1px;
  classDef metrics fill:#EBF5E0,stroke:#6AA84F,color:#6AA84F,stroke-width:1px;
  classDef reporting fill:#FFE0B2,stroke:#D9822B,color:#D9822B,stroke-width:1px;
  classDef frontend fill:#FFF4C1,stroke:#E0A800,color:#E0A800,stroke-width:1px;
  classDef io fill:#F0F0F0,stroke:#888888,color:#555555,stroke-width:1px,stroke-dasharray: 5 5;
```
