How simple-git-js/simple-git works
Project type and purpose
• This is an open-source, multi-package Node.js library (“simple-git”) that provides a lightweight JavaScript/TypeScript API for spawning and managing Git commands.
• It is organized as a Lerna monorepo:
• The core package lives under simple-git/ (source in src/, build scripts in scripts/, tests in test/).
• Consumer test packages in packages/ (to validate ES-module, JS and TS consumption).
• Documentation in docs/, examples in examples/, CI and release config under .github/, lerna.json, release-please-config.json.
File structure to infer architectural components
• Core API layer: simple-git/src/index.js exports the simpleGit() factory function.
• Command builder / facade: simple-git/src/lib/simple-git-api.ts, git-factory.ts
• Plugin system:
• src/lib/plugins/ houses built-in plugins (error-detection, progress-monitor, spawn-options, timeout, etc.)
• plugin-store.ts manages registration and lifecycle.
• Task and runner subsystem:
• src/lib/tasks/ implements one file per Git command (branch, commit, pull, push, etc.).
• src/lib/runners/ orchestrates execution: task queue, scheduler, executor chain, promise wrapping.
• Response parsing: src/lib/parsers/ reads raw stdout and builds typed data objects (DiffSummary, MergeSummary, etc.).
• Error models: src/lib/errors/ defines custom error types (GitError, GitResponseError, etc.).
• Build & CI:
• Build scripts: simple-git/scripts/build.js generates artifacts.
• Tests: unit/integration in simple-git/test/ run under Jest (jest.config.js).
• Lerna & release automation manage versioning and publishing.
README for additional insights
• The README describes:
• How the facade returns a SimpleGit instance.
• Promises vs. callbacks.
• Plugin customization points.
• Concurrency control (maxConcurrentProcesses).
• How options are passed through to the Git CLI.
a. Main components
1. Consumer Application (your Node.js or TS code)
2. Simple-Git Facade / API Layer
3. Plugin Store & Plugins
4. Command Builder & Task Definitions
5. Task Runner / Scheduler
6. Child-Process Executor (uses Node.js child_process.spawn)
7. Git Binary (external system dependency)
8. Response Parser & Error Mapper
9. Lerna Release & CI Pipeline (auxiliary)
b. Relationships & interactions
• The Consumer calls Facade → selects Task → Task Builder constructs CLI args → Plugin Store applies plugins (e.g. -c flags, timeout) → Task Runner enqueues request → Executor spawns child process → Git Binary runs → stdout/stderr streams → Response Parser transforms output → Errors (if any) are wrapped → Promise or callback resolves back to Consumer.
• CI Pipeline (GitHub Actions) watches code, runs Jest tests, runs Lerna & release-please to publish new versions.
c. Architectural patterns & principles
• Facade pattern: central simpleGit() interface hides complexity.
• Plugin pattern: pluggable detection and configuration behavior.
• Command pattern: each Git operation encapsulated in its own module.
• Builder pattern: options objects or arrays map to CLI flags.
• Scheduler: concurrency management and task queuing.
d. Key technologies & frameworks
• Node.js runtime
• TypeScript for core source and type definitions
• child_process for spawning external Git
• Jest for unit & integration tests
• Lerna and release-please for monorepo management and automated releases
• GitHub Actions for CI
Essential architectural elements
• Monorepo overlay: Lerna orchestrating multiple packages (simple-git, test-…-consumer)
• Release-automation feedback loop: code changes → CI → publish → version bump.