@ankurhalder/codebase-snapshot 2.2.0 → 4.0.0

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/docs/cli.md ADDED
@@ -0,0 +1,315 @@
1
+ # CLI reference
2
+
3
+ ```bash
4
+ npx @ankurhalder/codebase-snapshot <command> [options]
5
+ ```
6
+
7
+ The package installs one executable, **`codebase-snapshot`**, with a short alias **`snap`**.
8
+ Both are the same file. After `npx @ankurhalder/codebase-snapshot generate` the command
9
+ becomes available as `npx codebase-snapshot` for the rest of that session; after
10
+ `npm install -D` use `npx codebase-snapshot` or `npx snap` from the project.
11
+
12
+ Running with no command is the same as `generate`.
13
+
14
+ ## Contents
15
+
16
+ - [Commands](#commands)
17
+ - [`generate`](#generate)
18
+ - [`validate` / `check`](#validate--check)
19
+ - [`report`](#report)
20
+ - [`init`](#init)
21
+ - [`explain <path>`](#explain-path)
22
+ - [`diff <before> <after>`](#diff-before-after)
23
+ - [`describe`](#describe)
24
+ - [`skill`](#skill)
25
+ - [Options](#options)
26
+ - [Exit codes](#exit-codes)
27
+ - [Output streams](#output-streams)
28
+ - [Examples](#examples)
29
+
30
+ ## Commands
31
+
32
+ ### `generate`
33
+
34
+ ```bash
35
+ codebase-snapshot generate [options]
36
+ ```
37
+
38
+ Scans the repository and writes the snapshot to `outDir` (default `./snapshot`). This is the
39
+ default command, so `codebase-snapshot` alone does the same thing.
40
+
41
+ Detection is automatic: a preset is chosen from the 25 in the
42
+ [preset table](configuration.md#project-presets), which supplies layers, kinds and ecosystem
43
+ ignores. The preset's *recommended* part budget is not applied by `generate` — that runs on
44
+ `--max-part-bytes` (default 307200) or your config. `init` is what writes the recommended
45
+ budget into a fresh config, so a project set up with `init` gets the preset's figure.
46
+
47
+ Exit `0` when the snapshot passes its own integrity check, `1` when it does not.
48
+
49
+ ```
50
+ 📸 Generating snapshot for: /path/to/repo
51
+ Branch: main | Part budget: 300 KB
52
+
53
+ ✅ Snapshot generated in 0.14s:
54
+ - Tracked files: 128
55
+ - Total lines: 9,431
56
+ - Total size: 0.41 MB
57
+ - Parts written: 3 under snapshot/parts/
58
+ - Reports: snapshot/reports/ (project-tree, navigation, audit)
59
+ - Manifest: snapshot/manifest.json
60
+ - Fingerprint: e4607b570268c8def0911babbace97e4a0196d9eaf2cac22c8847500883d70aa
61
+ - Integrity: passed
62
+ ```
63
+
64
+ The fingerprint is printed in full — all 64 hex characters, never elided — and it matches
65
+ `manifest.fingerprint`.
66
+
67
+ The summary only lists artifacts that were actually written, so a run with
68
+ `--only-artifacts parts` does not claim a manifest exists.
69
+
70
+ ### `validate` / `check`
71
+
72
+ ```bash
73
+ codebase-snapshot validate [options]
74
+ ```
75
+
76
+ Compares the snapshot on disk with the repository. Reports five classes of drift — modified
77
+ files, deleted files, files added since the snapshot, damaged or missing parts, and config
78
+ changes — plus bookkeeping mismatches in the manifest.
79
+
80
+ Exit `0` when the snapshot is current, `1` on any drift. It never writes anything.
81
+
82
+ ### `report`
83
+
84
+ ```bash
85
+ codebase-snapshot report [options]
86
+ ```
87
+
88
+ Prints snapshot health without re-scanning everything: counts, layer breakdown, warnings,
89
+ and the validation verdict. Requires an existing `manifest.json` (exit `1` if missing). Also
90
+ exits `1` when it detects drift.
91
+
92
+ ### `init`
93
+
94
+ ```bash
95
+ codebase-snapshot init [options]
96
+ ```
97
+
98
+ Detects the project and writes `snapshot.config.ts` (or `.mjs`, see below) containing
99
+ **every** setting at its current value, commented and grouped. It is a reference you can
100
+ keep, not just a diff.
101
+
102
+ It also has two side effects on files you own, and it says so when it does them:
103
+
104
+ ```
105
+ ✅ Created snapshot.config.ts (preset: node-lib)
106
+ Added npm scripts: snapshot, snapshot:validate
107
+ Added to .gitignore: snapshot/, snapshot.tmp/
108
+ ```
109
+
110
+ - `scripts.snapshot` and `scripts.snapshot:validate` are added to `package.json` if those keys
111
+ are absent. Existing values are left alone, and the file is rewritten as 2-space JSON.
112
+ Without a `package.json` it notes that scripts were not added.
113
+ - `snapshot/` and `snapshot.tmp/` are appended to `.gitignore` if they are not already listed;
114
+ the file is created if missing, and entries already present are not duplicated.
115
+
116
+ Nothing else in the repository is touched.
117
+
118
+ Interactive by default; with `--yes` it accepts every default and never prompts, which is
119
+ what agents and release checks should use.
120
+
121
+ The filename adapts to the project: `.ts` only when the running Node can strip types
122
+ (≥ 22.6) **and** the project is an ES module; otherwise `.mjs`, which loads everywhere. The
123
+ generated file is a plain object with no imports, so it works even when the package is only
124
+ run through `npx` and never installed.
125
+
126
+ Related options: `--preset`, `--include`, `--exclude`, `--yes`, `--minimal` and `--force`
127
+ apply to `init` only. `init` also accepts `--dry-run`, `--max-part-bytes`, `--out-dir`,
128
+ `--include-sensitive-files`, `--skip-artifacts` and `--only-artifacts`, which it shares with
129
+ the commands that re-read a snapshot, and records their effect in the generated file. The
130
+ `Applies to` column is in the [options table](#options).
131
+
132
+ ### `explain <path>`
133
+
134
+ ```bash
135
+ codebase-snapshot explain src/server.ts
136
+ ```
137
+
138
+ Answers "what would happen to this file?" — layer, kind, language, size, which part it lands
139
+ in, and the exact reason if it is skipped (`sensitive-file`, `binary`, `default-ignore`,
140
+ `excluded`, and the other reasons in [Troubleshooting](troubleshooting.md#a-file-is-missing-from-the-snapshot)).
141
+
142
+ Exit `0` whenever the path could be inspected, whether or not it would be included — the
143
+ answer is in the output. Exit `1` only when the command itself could not run.
144
+
145
+ This is the fastest way to debug a file that is unexpectedly missing from a snapshot.
146
+
147
+ With `--format json` the same answer is one object on stdout, with only the keys that apply:
148
+
149
+ ```json
150
+ {
151
+ "path": "src/math.ts",
152
+ "exists": true,
153
+ "included": true,
154
+ "layer": "src",
155
+ "kind": "other",
156
+ "language": "typescript",
157
+ "layerMatch": "function (custom predicate)",
158
+ "bytes": 63,
159
+ "lines": 3,
160
+ "secretCount": 0,
161
+ "secrets": [],
162
+ "part": "part-001.md",
163
+ "inclusionMode": "full"
164
+ }
165
+ ```
166
+
167
+ | Key | Meaning |
168
+ | :--- | :--- |
169
+ | `path` | The path you asked about, relative to `--cwd` and with POSIX separators. |
170
+ | `exists` | Whether anything is on disk at that path. |
171
+ | `included` | Whether the scanner would include it in the next snapshot. |
172
+ | `reason`, `detail` | Why not, when `included` is `false`. The same strings `manifest.skipped[]` uses, plus `missing` and `not-discovered`. |
173
+ | `layer`, `kind`, `language` | The classification, present only when `included` is `true`. |
174
+ | `layerMatch`, `kindMatch` | Which rule chose the layer or kind. A glob is printed as written (`src/api/**`); a `RegExp` as its literal form (`/\.tsx$/i`); a function matcher, which is what the presets use, as `function (custom predicate)`. Absent when no rule matched and the `other` fallback was used. |
175
+ | `bytes`, `lines` | Size of the file on disk. |
176
+ | `secretCount`, `secrets` | How many secret rules fire, and the first 20 as `{ rule, line }` — never the matched text. |
177
+ | `part` | The part filename from the last written manifest, or `null` when it is not in that snapshot. |
178
+ | `inclusionMode` | `full` or `preview`, when a part entry exists. |
179
+
180
+ When `included` is `false` the object stops after `path`, `exists`, `included`, `reason` and
181
+ `detail`.
182
+
183
+ ### `diff <before> <after>`
184
+
185
+ ```bash
186
+ codebase-snapshot diff ./snapshot-old ./snapshot
187
+ codebase-snapshot diff ./a/manifest.json ./b/manifest.json
188
+ ```
189
+
190
+ Compares two snapshots and lists added, removed and changed files. Either argument may be a
191
+ snapshot directory or a `manifest.json`. Useful in review: "what changed in the context we
192
+ hand to the model?"
193
+
194
+ ### `describe`
195
+
196
+ ```bash
197
+ codebase-snapshot describe # human-readable
198
+ codebase-snapshot describe --format json # machine-readable contract
199
+ ```
200
+
201
+ Prints the tool's complete contract: the supported invocation, an ordered workflow, every
202
+ command, every option with its default, the exit-code meanings, all presets, the artifact
203
+ list, config filenames, output layout, safety rules and a troubleshooting table. The JSON
204
+ form is what an agent should read instead of `--help`. See [Using this with an AI
205
+ agent](agent.md).
206
+
207
+ ### `skill`
208
+
209
+ ```bash
210
+ codebase-snapshot skill # print on stdout
211
+ codebase-snapshot skill --install # write every location
212
+ codebase-snapshot skill --install --target claude
213
+ ```
214
+
215
+ Prints or installs the bundled agent skill. Installation writes an `AGENTS.md` section,
216
+ `.claude/skills/codebase-snapshot/SKILL.md` and `.cursor/rules/codebase-snapshot.mdc`;
217
+ `--target` limits it to one. Re-running replaces only its own marked block, so an existing
218
+ `AGENTS.md` keeps its content. `--format json` reports what was created, updated or
219
+ unchanged.
220
+
221
+ Printing goes to **stdout** so it can be redirected: `codebase-snapshot skill > SKILL.md`.
222
+
223
+ ## Options
224
+
225
+ An option a command does not accept is **rejected**, not ignored: exit `2` and a message
226
+ naming the commands that do accept it. Options marked *init only* or *skill only* are the
227
+ narrowest cases of the same rule.
228
+
229
+ | Option | Type | Default | Applies to | Meaning |
230
+ | :--- | :--- | :--- | :--- | :--- |
231
+ | `--cwd <path>` | path | current directory | generate, validate, report, explain, init, skill | Repository to operate on. Must exist and be a directory. `diff` and `describe` take no `--cwd`. |
232
+ | `--out-dir <path>` | path | `snapshot` | generate, validate, report, explain, init | Where the snapshot is written or read. May be absolute. |
233
+ | `--max-part-bytes <n>` | size | `307200` (300 KB) | generate, validate, report, explain, init | Hard ceiling per markdown part. Accepts `120k`, `1.5mb`. Minimum 4096. |
234
+ | `--large-file-bytes <n>` | size | `204800` (200 KB) | generate, validate, report, explain | Above this, a file becomes a head/tail preview. |
235
+ | `--max-monolith-bytes <n>` | size | unset | generate, validate, report, explain | Skip the single-file monolith when it would exceed this. |
236
+ | `--include-monolith` | flag | off | generate, validate, report, explain | Also write `full-codebase.json` and `full-codebase.md`. |
237
+ | `--include-sensitive-files` | flag | off | generate, validate, report, explain, init | Emit `.env`, `*.pem`, `.npmrc` and similar. Off by default; they are reported as skipped. |
238
+ | `--skip-artifacts <names>` | list | unset | generate, validate, report, explain, init | Comma-separated artifacts to omit. |
239
+ | `--only-artifacts <names>` | list | unset | generate, validate, report, explain, init | Write only these. Cannot be combined with `--skip-artifacts`. |
240
+ | `--format <text\|json>` | enum | `text` | every command | `json` writes a pure JSON payload to stdout. |
241
+ | `--dry-run` | flag | off | generate, init | Compute everything, print it, write nothing. |
242
+ | `--watch` | flag | off | generate | Regenerate on every change until interrupted. |
243
+ | `--no-config` | flag | off | generate, validate, report, explain | Ignore every `snapshot.config.*` file and run on the built-in defaults. |
244
+ | `--preset <name>` | name | detected | *init only* | Force a preset instead of detecting one. |
245
+ | `--include <globs>` | list | unset | *init only* | Allow-list recorded in the generated config. |
246
+ | `--exclude <globs>` | list | unset | *init only* | Exclude globs recorded in the generated config. |
247
+ | `--yes` | flag | off | *init only* | Never prompt; accept every default. |
248
+ | `--minimal` | flag | off | *init only* | Write only settings that differ from the defaults. |
249
+ | `--force` | flag | off | *init only* | Overwrite an existing config. |
250
+ | `--install` | flag | off | *skill only* | Write the skill into the repository instead of printing it. |
251
+ | `--target <name>` | enum | `all` | *skill only* | `claude`, `agents`, `cursor` or `all`. |
252
+ | `-h`, `--help` | flag | — | every command | Print usage. |
253
+ | `-v`, `--version` | flag | — | every command | Print the version on stdout: a bare `v`-prefixed semver such as `v4.0.0`, with no package name. |
254
+
255
+ The artifact and size options apply to `validate`, `report` and `explain` as well as
256
+ `generate` because those commands re-read the snapshot: pass the selection you generated
257
+ with and the comparison is like for like. `validate --skip-artifacts parts` on a snapshot
258
+ built without parts therefore agrees, while the same flag on a full snapshot reports a
259
+ config mismatch.
260
+
261
+ Every option also accepts the `--option=value` form. That form is required for negative
262
+ numbers, which otherwise look like flags.
263
+
264
+ ## Exit codes
265
+
266
+ | Code | Meaning |
267
+ | :--- | :--- |
268
+ | `0` | Success. For `validate`/`check`, no drift. |
269
+ | `1` | Drift detected, generation refused or failed (nothing was written), or the state the command needed was not there. |
270
+ | `2` | Usage error: unknown flag or command, an option the command does not accept, bad value, wrong argument count. |
271
+
272
+ `1` covers the cases where the tool did the right thing but the state is not what you wanted
273
+ — drift, a refused output directory, an unparseable config. `2` means the command itself was
274
+ wrong and retrying it unchanged will fail the same way.
275
+
276
+ ## Output streams
277
+
278
+ - **stdout** carries the payload. With `--format json` it is pure JSON, safe to pipe into
279
+ `jq` or `JSON.parse`.
280
+ - **stderr** carries progress, the human summary, warnings and errors.
281
+
282
+ So `codebase-snapshot describe --format json > contract.json` and
283
+ `codebase-snapshot validate --format json | jq .drift` both work.
284
+
285
+ ## Examples
286
+
287
+ ```bash
288
+ # The common path
289
+ npx @ankurhalder/codebase-snapshot generate
290
+
291
+ # A tighter budget for a small model context, in a separate directory
292
+ npx @ankurhalder/codebase-snapshot generate --out-dir .context --max-part-bytes 120k
293
+
294
+ # Just the code parts, no reports
295
+ npx @ankurhalder/codebase-snapshot generate --only-artifacts parts,manifest
296
+
297
+ # One file per part, with everything else left alone
298
+ npx @ankurhalder/codebase-snapshot validate
299
+
300
+ # Why is this file missing?
301
+ npx @ankurhalder/codebase-snapshot explain src/generated/schema.ts
302
+
303
+ # What changed between two snapshots?
304
+ npx @ankurhalder/codebase-snapshot diff old-snapshot snapshot
305
+
306
+ # Non-interactive setup, then a JSON dry run to inspect the result
307
+ npx @ankurhalder/codebase-snapshot init --yes
308
+ npx @ankurhalder/codebase-snapshot generate --dry-run --format json | jq .config
309
+
310
+ # Keep the snapshot fresh while editing
311
+ npx @ankurhalder/codebase-snapshot generate --watch
312
+
313
+ # Give an agent everything it needs
314
+ npx @ankurhalder/codebase-snapshot skill --install
315
+ ```