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