devsplain 1.2.0 → 1.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +115 -41
- package/bin/cli.js +591 -195
- package/bin/post-commit.js +96 -0
- package/bin/setup-hook.js +79 -0
- package/lib/config.js +124 -80
- package/lib/llm.js +221 -139
- package/package.json +43 -43
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Muhammad Wahaj
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,81 +1,155 @@
|
|
|
1
1
|
# devsplain
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
An industrial-grade, agent-agnostic CLI tool that automatically adds JSDoc and inline comments to your code using state-of-the-art LLMs, while guaranteeing code integrity with deterministic safety constraints.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
---
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
## Key Features
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
- **Mathematical Safety Invariants**: Uses an index-preserving splicing engine. Your functional code is mathematically verified to remain identical before and after commenting.
|
|
10
|
+
- **Multi-Language support**: Natively parses JavaScript, JSX, TypeScript, TSX, HTML, CSS, SCSS, Vue, Svelte, Python, Java, C, C++, C#, Go, Ruby, PHP, Rust, Swift, Kotlin, Dart, and Shell scripts.
|
|
11
|
+
- **Local Deterministic Scrubber**: The `--clean` flag strips comments locally using a deterministic lexical state machine—no LLM calls, API keys, or internet required.
|
|
12
|
+
- **Git Hook Automation**: Supports an automated two-commit Git hook workflow (`pre-commit` for quality, `post-commit` for auto-generated documentation commits) that prevents recursive commit loops.
|
|
13
|
+
- **Bring Your Own LLM**: Native setup wizard for Groq, Gemini, OpenAI, or any OpenAI-compatible API endpoint (like Ollama or LMStudio).
|
|
14
|
+
- **Exponential Backoff**: Resilient AI request handler that automatically retries rate-limited requests with exponential backoff.
|
|
15
|
+
- **Headless & Override Control**: Configure via environment variables or override global config settings dynamically on the fly with command-line flags.
|
|
16
|
+
|
|
17
|
+
---
|
|
10
18
|
|
|
11
|
-
|
|
19
|
+
## Architecture & Safety Guarantees
|
|
12
20
|
|
|
21
|
+
Many AI code formatters rewrite your code entirely, exposing you to logic regressions and subtle syntax corruption. `devsplain` is designed from the ground up to prevent this.
|
|
22
|
+
|
|
23
|
+
### The Splicing Engine
|
|
24
|
+
1. The CLI prepends line numbers to your source code and sends it to the LLM.
|
|
25
|
+
2. The LLM returns a structured JSON payload containing target line numbers and comment contents:
|
|
26
|
+
```json
|
|
27
|
+
[
|
|
28
|
+
{ "line": 12, "comment": "// Calculates the exponential backoff delay" }
|
|
29
|
+
]
|
|
30
|
+
```
|
|
31
|
+
3. The splicing engine inserts comments directly into the source array at the requested indices.
|
|
32
|
+
4. **Validation Check**: Before writing to disk, `devsplain` filters out the added comments and compares the remaining code lines against your original source. If a single line of executable code has changed, shifted, or been omitted, the process aborts immediately, safeguarding your code from corruption.
|
|
33
|
+
|
|
34
|
+
### String Literal Guardrails
|
|
35
|
+
The engine tracks lexical state across template strings, single quotes, double quotes, and multi-line docstrings (such as Python triple-quotes). Comment insertion is blocked if the target line resides within a string literal, preventing broken syntax.
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
## Installation
|
|
40
|
+
|
|
41
|
+
Install globally using `npm`:
|
|
13
42
|
```bash
|
|
14
43
|
npm install -g devsplain
|
|
15
44
|
```
|
|
16
45
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
Simply point `devsplain` at any file you want to comment:
|
|
20
|
-
|
|
46
|
+
Or run it on demand without installation:
|
|
21
47
|
```bash
|
|
22
|
-
devsplain
|
|
48
|
+
npx devsplain <file-or-directory> [options]
|
|
23
49
|
```
|
|
24
50
|
|
|
25
|
-
|
|
51
|
+
---
|
|
52
|
+
|
|
53
|
+
## Setup & Configuration
|
|
26
54
|
|
|
27
|
-
|
|
55
|
+
On its first run, `devsplain` launches an interactive configuration wizard to configure your preferred LLM provider.
|
|
28
56
|
|
|
57
|
+
To force re-run the configuration wizard at any time, execute:
|
|
29
58
|
```bash
|
|
30
|
-
devsplain
|
|
59
|
+
devsplain --config
|
|
31
60
|
```
|
|
32
61
|
|
|
33
|
-
|
|
62
|
+
Your settings are stored securely in `~/.devsplainrc` (configured with `chmod 600` on POSIX systems to restrict read access).
|
|
34
63
|
|
|
35
|
-
|
|
64
|
+
---
|
|
36
65
|
|
|
37
|
-
|
|
66
|
+
## CLI Usage & Options
|
|
38
67
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
- `--dry-run`: Interactive preview. Prints the AI's output to the terminal and waits for your approval before saving to the file. Extremely safe for testing!
|
|
43
|
-
- **(Default)**: A balanced mix of JSDoc headers and sparse inline comments for complex logic.
|
|
68
|
+
```bash
|
|
69
|
+
devsplain <file-or-directory> [options]
|
|
70
|
+
```
|
|
44
71
|
|
|
45
|
-
|
|
72
|
+
### Options
|
|
73
|
+
|
|
74
|
+
| Flag | Description |
|
|
75
|
+
|---|---|
|
|
76
|
+
| *(Default)* | Balanced commenting. Generates a mix of JSDoc block comments above functions and sparse inline comments for complex logical branches. |
|
|
77
|
+
| `--light` | Minimalist commenting. Adds JSDoc/block comments above functions, leaving function bodies untouched. |
|
|
78
|
+
| `--full` | Aggressive commenting. Explains complex logic blocks line-by-line inside functions. |
|
|
79
|
+
| `--dry-run` | Preview comments in the terminal without writing to files. Prompts for manual save confirmation. |
|
|
80
|
+
| `--force` | Bypasses the safety block check that prevents running `devsplain` on a dirty Git working tree. |
|
|
81
|
+
| `--clean` | Scrubber mode. Deterministically removes all comments and docstrings from source files. |
|
|
82
|
+
| `--provider <name>`| Temporary one-off override for the AI provider (`gemini`, `groq`, `openai`, `custom`) for this command run only (does not modify the saved config file). |
|
|
83
|
+
| `--model <name>` | Temporary one-off override for the model name for this command run only. |
|
|
84
|
+
| `--api-key <key>` | Temporary one-off override for the API key for this command run only. |
|
|
85
|
+
| `--base-url <url>` | Temporary one-off override for the API base URL for this command run only. |
|
|
86
|
+
| `--config` | Relaunches the configuration setup wizard. |
|
|
87
|
+
| `--setup-hook` | Installs Git pre-commit and post-commit hooks in the repository. |
|
|
88
|
+
| `--help, -h` | Displays the help menu. |
|
|
89
|
+
| `--version, -v` | Displays version information. |
|
|
90
|
+
|
|
91
|
+
### Usage Examples
|
|
46
92
|
|
|
47
93
|
```bash
|
|
48
|
-
|
|
94
|
+
# Light commenting on a single file
|
|
95
|
+
devsplain src/index.js --light
|
|
96
|
+
|
|
97
|
+
# Deep logic commenting on a folder (skips node_modules, .git, etc.)
|
|
49
98
|
devsplain src/ --full
|
|
50
|
-
devsplain legacy_code.js --clean
|
|
51
|
-
devsplain lib/ --dry-run
|
|
52
|
-
```
|
|
53
99
|
|
|
54
|
-
|
|
100
|
+
# Clean and scrub comments from your codebase locally without AI calls
|
|
101
|
+
devsplain lib/ --clean
|
|
55
102
|
|
|
56
|
-
|
|
103
|
+
# Headless run using overriding credentials
|
|
104
|
+
devsplain src/utils.ts --provider gemini --model gemini-2.0-flash --api-key YOUR_KEY
|
|
105
|
+
```
|
|
57
106
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
-
|
|
107
|
+
> [!WARNING]
|
|
108
|
+
> **Directory Traversal Caution**
|
|
109
|
+
> The built-in list of ignored folders (like `node_modules`, `.git`, `dist`, etc.) is not exhaustive. If you run `devsplain` on a broad directory that contains unignored directories (such as local caches or build directories), it may start commenting unwanted files in random folders.
|
|
110
|
+
>
|
|
111
|
+
> To prevent this, it is highly recommended to either:
|
|
112
|
+
> 1. Use the **Automated Git Hooks** to comment only on files modified in your commits.
|
|
113
|
+
> 2. Pass specific files or selective subfolders manually (e.g., `devsplain src/utils.ts`) instead of targeting broad directories.
|
|
61
114
|
|
|
62
|
-
|
|
115
|
+
---
|
|
63
116
|
|
|
64
|
-
|
|
117
|
+
## Environment Variables
|
|
65
118
|
|
|
66
|
-
|
|
67
|
-
2. **Gemini** - Google's free tier endpoints.
|
|
68
|
-
3. **OpenAI** - Standard paid ChatGPT models.
|
|
69
|
-
4. **Custom** - Point it at ANY OpenAI-compatible endpoint (e.g., local models via Ollama or LMStudio).
|
|
119
|
+
For headless environments, CI pipelines, or automated scripts, `devsplain` respects the following environment variables:
|
|
70
120
|
|
|
71
|
-
|
|
121
|
+
- `DEVSPLAIN_PROVIDER`: Selects the AI provider (`groq`, `gemini`, `openai`, `custom`).
|
|
122
|
+
- `DEVSPLAIN_API_KEY`: The API key to use for the selected provider.
|
|
123
|
+
- `DEVSPLAIN_MODEL`: Specify a custom model.
|
|
124
|
+
- `DEVSPLAIN_BASE_URL`: Custom base endpoint for local/private API runs.
|
|
72
125
|
|
|
73
126
|
---
|
|
74
127
|
|
|
75
|
-
|
|
128
|
+
## Automated Git Hooks
|
|
76
129
|
|
|
77
|
-
|
|
130
|
+
Ensure all code changes in your repository are automatically documented by configuring Git hooks.
|
|
131
|
+
|
|
132
|
+
> [!NOTE]
|
|
133
|
+
> **Commit Duration Note**
|
|
134
|
+
> When the Git hooks are enabled, committing code may take a few seconds longer. This is because `devsplain` needs to call the AI provider, generate comments, and splice them into the modified files before completing the commit cycle.
|
|
135
|
+
|
|
136
|
+
### Installation
|
|
137
|
+
Run the hook setup command inside your Git repository:
|
|
138
|
+
```bash
|
|
139
|
+
devsplain --setup-hook
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### Pipeline Architecture
|
|
143
|
+
1. **Pre-commit Hook**: Runs your project test suite (`npm test`). If the tests fail, the commit is blocked.
|
|
144
|
+
2. **Post-commit Hook**:
|
|
145
|
+
- Detects all modified files in the commit.
|
|
146
|
+
- Runs `devsplain` to generate comments on the modified files.
|
|
147
|
+
- Automatically stages and commits the comments in a secondary commit: `docs: auto-generated comments by devsplain`.
|
|
148
|
+
- The secondary commit runs with `--no-verify` to prevent recursive hook invocation.
|
|
149
|
+
3. **Resilience**: If the API key is missing or the network is offline during the post-commit process, the script logs a warning to stderr and exits with status code `0`. Your initial commit remains safe and unblocked.
|
|
150
|
+
|
|
151
|
+
---
|
|
78
152
|
|
|
79
153
|
## License
|
|
80
154
|
|
|
81
|
-
MIT
|
|
155
|
+
This project is licensed under the MIT License.
|