# depcheck/depcheck architecture

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

- Interactive diagram: https://gitdiagram.com/depcheck/depcheck
- Repository: https://github.com/depcheck/depcheck

## Overview

##### Main Components
- CLI Interface – Represented by files such as src/cli.js and the entry point in src/index.js. This component handles user input, processes command-line arguments, and triggers the dependency analysis flow.
- Core Analysis Engine – The main orchestrator that ties in parsers, detectors, and special modules. This is essentially the logic in src/index.js that uses configuration files (like .depcheckrc) and utilities.
- Detector Modules – Located in src/detector, these modules (e.g., exportDeclaration.js, requireCallExpression.js, and others) implement various rules to detect how dependencies are referenced in the code.
- Parser Modules – Found in src/parser, these handle different syntaxes: ES6, ES7, JSX, CoffeeScript, TypeScript, GraphQL, Sass, Vue, etc. They analyze the files to extract dependency information.
- Special Modules – Under src/special, these address non-standard or framework-specific dependency declarations (e.g., babel.js, eslint.js, gatsby.js, webpack.js, etc.). They clarify dependencies used in configuration or build environments.
- Utility and Configuration Components – In src/utils, modules like configuration-reader.js and parser.js interplay with the file system, handle exceptions, and support configuration via external files.
- Test and Build Infrastructure – Although not part of the runtime flow, test directories and build scripts (e.g., under bin and build) indicate how the project is structured for development, continuous integration, and deployment.

##### Relationships & Data Flow
- The CLI then calls the core analysis engine, which reads the project’s configuration (possibly merging CLI overrides with .depcheckrc configuration) using utility modules.
- The core engine iterates over the source files, distributing them to appropriate parser modules based on file patterns (syntax types). The parsers return parsed data on dependency usage.
- Parsed data is then fed into a series of detectors that analyze import/require statements and other patterns (such as express view engine usage, grunt tasks, etc.) to determine dependency usage.
- Special modules augment this process by addressing dependencies in contextual cases like Babel configurations or ESLint setups.
- Finally, the results are aggregated (including unused dependencies, missing declarations, and detailed usage maps) and formatted for output (either neatly to the console or as JSON).

##### Architectural and Design Patterns
- Modularity – Each type of analysis (parsing, detection, handling specials) is split into its own module or set of modules, making it easy to plug new language supports or frameworks.
- Plugin/Pluggable Design – Depcheck’s “pluggable” design (as mentioned in the README with a reference to the pluggable design document) encourages adding or replacing parsers, detectors, and special handlers without modifying the core logic.
- Utility Abstraction – Helpers for file operations, configuration reading, and exception management are encapsulated in the utils folder, reinforcing a clean, layered design.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "User Input Layer"
        CLI["CLI Interface"]:::cli
    end

    subgraph "Core Analysis Layer"
        Engine["Core Analysis Engine"]:::engine
        Utilities["Utilities & Configuration"]:::utils
    end

    subgraph "Analysis Modules Layer"
        Parsers["Parsers"]:::parser
        Detectors["Detectors"]:::detector
        Special["Special Handlers"]:::special
    end

    subgraph "Test & Build Layer"
        Test["Test Infrastructure"]:::test
        Build["Build Scripts"]:::build
    end

    CLI -->|"triggers"| Engine
    Engine -->|"readsConfig"| Utilities
    Engine -->|"delegatesTo"| Parsers
    Engine -->|"delegatesTo"| Detectors
    Engine -->|"appliesSpecial"| Special
    Parsers -->|"returnsData"| Engine
    Detectors -->|"returnsData"| Engine
    Special -->|"augmentsData"| Engine

    classDef cli fill:#f9c74f,stroke:#333,stroke-width:2px;
    classDef engine fill:#90be6d,stroke:#333,stroke-width:2px;
    classDef parser fill:#577590,stroke:#333,stroke-width:2px;
    classDef detector fill:#f9844a,stroke:#333,stroke-width:2px;
    classDef special fill:#f3722c,stroke:#333,stroke-width:2px;
    classDef utils fill:#43aa8b,stroke:#333,stroke-width:2px;
    classDef test fill:#277da1,stroke:#333,stroke-width:2px;
    classDef build fill:#277da1,stroke:#333,stroke-width:2px;

    click CLI "https://github.com/depcheck/depcheck/blob/main/src/cli.js"
    click Engine "https://github.com/depcheck/depcheck/blob/main/src/index.js"
    click Utilities "https://github.com/depcheck/depcheck/tree/main/src/utils/"
    click Parsers "https://github.com/depcheck/depcheck/tree/main/src/parser/"
    click Detectors "https://github.com/depcheck/depcheck/tree/main/src/detector/"
    click Special "https://github.com/depcheck/depcheck/tree/main/src/special/"
    click Test "https://github.com/depcheck/depcheck/blob/main/bin/depcheck.js"
    click Build "https://github.com/depcheck/depcheck/tree/main/build/"
```
