# adopisoft/minify-dir architecture

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

- Interactive diagram: https://gitdiagram.com/adopisoft/minify-dir
- Repository: https://github.com/adopisoft/minify-dir

## Overview

##### Main Components
- Input Source Directory: Represents the directory supplied by the user (e.g., “src”) containing files and subdirectories.
- File Walker/Chooser: A module that recursively processes the directory tree, applies “excludePatterns” to ignore files/folders (like .git or any other configured exclusions), and categorizes files based on type (JS, TS, or non-code assets).
- Code Removal Module: This component (represented by src/remove-code.ts) processes files and removes blocks of code marked for removal if conditions are met (e.g., removeIf(prod) … endRemoveIf(prod)).
- Minification Engine: Integrated with the “uglify-js” library, it is responsible for minifying JavaScript code. This is optionally engaged if the "minify" flag is true.
- Typescript Transpiler Module: Triggered via src/tsc.ts when processing TypeScript files using provided tsc configuration options.
- Output/Copier Module: This part handles writing the processed files (minified or transpiled) into the destination directory (“dest”, such as “release”), and copying non-JS files as per the “copy” flag.
- Option Handler & Configurator: Initializes and passes the options object (minify, copy, removeCode conditions, tsc options, etc.) and sets the basePath.
- Logging/Error Handling: Although not explicitly a separate file, include components for logging (like the console outputs in usage) and catching errors during the process.

##### Design the Interactions Between Components
- The process begins when a request is made (via the minifyDir call) with a source directory and options.
- The File Walker component scans the source, filters files based on excludePatterns, and sends each file for processing.
- JS and TS files are routed to the Code Removal Module first; the code blocks are conditionally removed.
- Once refined, JS files are sent to the Minification Engine, while TS files are forwarded to the Typescript Transpiler.
- In parallel, non-code assets (files that are not JavaScript/TypeScript) are handled by the Output/Copier Module and simply copied to the destination if “copy” is true.
- Processed files are then stored in the appropriate subdirectories within the destination directory, mirroring the original structure.
- Error handling and logging are present throughout all phases to capture issues and notify the user.

##### Architectural Patterns & Design Principles
- Separation of Concerns: Each component (e.g., file handling, code removal, minification, transpilation) is isolated for clarity and maintainability.
- Modular Design: The different functionality is split into distinct modules (e.g., index.ts for coordination, remove-code.ts for block removal).
- Pipeline / Flow-Based Processing: Files pass through a sequential pipeline where each step (filtering, code removal, transformation, copying) is clearly demarcated.
- Configurability: The behavior of the system is driven by an options object that tailors the flow (e.g., toggling minification, setting tsc options, and specifying removal conditions).

## Diagram (Mermaid)

```mermaid
graph TD
    A["Input Source Directory"]:::io
    B["File Walker & Option Handler"]:::fileProcess
    C["Code Removal Module"]:::codeRemoval
    D["Minification Engine"]:::minify
    E["Typescript Transpiler Module"]:::tstranspile
    F["Output/Copier Module"]:::io
    L["Logging/Error Handling"]:::logging
    T["Test Suite Environment"]:::test

    A -->|"scans"| B
    B -->|"filters & routes"| C
    C -->|"JS files"| D
    C -->|"TS files"| E
    D -->|"processed JS"| F
    E -->|"transpiled TS"| F
    B -->|"non-code assets"| F
    F -->|"logs output"| L
    T -.->|"validates"| B

    click A "https://github.com/adopisoft/minify-dir/tree/main/test/sample-files/src"
    click B "https://github.com/adopisoft/minify-dir/blob/main/src/index.ts"
    click C "https://github.com/adopisoft/minify-dir/blob/main/src/remove-code.ts"
    click D "https://github.com/adopisoft/minify-dir/blob/main/src/index.ts"
    click E "https://github.com/adopisoft/minify-dir/blob/main/src/tsc.ts"
    click F "https://github.com/adopisoft/minify-dir/blob/main/src/index.ts"
    click T "https://github.com/adopisoft/minify-dir/tree/main/test/"

    classDef io fill:#D3D3D3,stroke:#000,stroke-width:1px;
    classDef fileProcess fill:#ADD8E6,stroke:#000,stroke-width:1px;
    classDef codeRemoval fill:#6495ED,stroke:#000,stroke-width:1px;
    classDef minify fill:#90EE90,stroke:#000,stroke-width:1px;
    classDef tstranspile fill:#FFA07A,stroke:#000,stroke-width:1px;
    classDef logging fill:#FFB6C1,stroke:#000,stroke-width:1px;
    classDef test fill:#FFD700,stroke:#000,stroke-width:1px;
```
