Skip to content

Commit f86547c

Browse files
committed
feat(cli): add version, --version, -V
- CODEMAP_VERSION from package.json (inlined at build) - Export CODEMAP_VERSION from package entry - Tests for version subcommand and flags
1 parent c660faa commit f86547c

7 files changed

Lines changed: 60 additions & 17 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ The package exposes a **`codemap`** binary, a **library** entry (`import` / `exp
3333
# Index project root (optional codemap.config.ts / codemap.config.json)
3434
codemap
3535

36+
# Version (also: codemap --version, codemap -V)
37+
codemap version
38+
3639
# Full rebuild
3740
codemap --full
3841

docs/architecture.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ A local SQLite database (`.codemap.db`) indexes the project tree and stores stru
1010

1111
## Layering
1212

13-
| Layer | Role |
14-
| -------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- |
15-
| **`cli.ts`** | Parses argv (`--root`, `--config`, `query`, `agents init`, `--files`, `--full`), wires bootstrap → `runCodemapIndex` / `printQueryResult`. |
16-
| **`api.ts`** | Public programmatic surface: `createCodemap()`, `Codemap` (`query`, `index`), re-exports `runCodemapIndex` for advanced use. |
17-
| **`application/`** | Use cases: `run-index.ts` (incremental / full / targeted orchestration), `index-engine.ts` (collect files, git diff, `indexFiles`, workers via `worker-pool.ts`). |
18-
| **`adapters/`** | `LanguageAdapter` registry; built-ins call `parser.ts` / `css-parser.ts` / `markers.ts` from `parse-worker-core`. |
19-
| **`runtime.ts` / `config.ts` / `db.ts` / …** | Config, SQLite, resolver, workers. |
13+
| Layer | Role |
14+
| -------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
15+
| **`cli.ts`** | Parses argv (`--root`, `--config`, `query`, `agents init`, `--files`, `--full`, `--help`, `version` / `--version`), wires bootstrap → `runCodemapIndex` / `printQueryResult`. |
16+
| **`api.ts`** | Public programmatic surface: `createCodemap()`, `Codemap` (`query`, `index`), re-exports `runCodemapIndex` for advanced use. |
17+
| **`application/`** | Use cases: `run-index.ts` (incremental / full / targeted orchestration), `index-engine.ts` (collect files, git diff, `indexFiles`, workers via `worker-pool.ts`). |
18+
| **`adapters/`** | `LanguageAdapter` registry; built-ins call `parser.ts` / `css-parser.ts` / `markers.ts` from `parse-worker-core`. |
19+
| **`runtime.ts` / `config.ts` / `db.ts` / …** | Config, SQLite, resolver, workers. |
2020

2121
`index.ts` is the package entry: re-exports the public API and runs `cli.ts` only when executed as the main module (Node/Bun `codemap` binary).
2222

@@ -89,7 +89,7 @@ A local SQLite database (`.codemap.db`) indexes the project tree and stores stru
8989
| File | Purpose |
9090
| ----------------- | ------------------------------------------------------------------------------------------------ |
9191
| `index.ts` | Package entry — re-exports `api` / `config`, runs CLI when main |
92-
| `cli.ts` | CLI — argv parsing, `query`, `agents init`, `--files`, index modes |
92+
| `cli.ts` | CLI — argv parsing, `query`, `agents init`, `--files`, `version`, index modes |
9393
| `api.ts` | Programmatic API — `createCodemap`, `Codemap`, `runCodemapIndex` |
9494
| `application/` | Indexing use cases and engine (`run-index`, `index-engine`, types) |
9595
| `worker-pool.ts` | Parallel parse workers (Bun / Node) |

src/cli.test.ts

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { describe, expect, test } from "bun:test";
22
import { join } from "node:path";
33

44
import { parseBootstrapArgs } from "./cli";
5+
import { CODEMAP_VERSION } from "./version";
56

67
describe("parseBootstrapArgs", () => {
78
test("passes --help through in rest after --root", () => {
@@ -11,20 +12,39 @@ describe("parseBootstrapArgs", () => {
1112
});
1213
});
1314

15+
async function runCli(
16+
args: string[],
17+
): Promise<{ exitCode: number; out: string; err: string }> {
18+
const indexTs = join(import.meta.dir, "index.ts");
19+
const proc = Bun.spawn([Bun.which("bun")!, indexTs, ...args], {
20+
cwd: join(import.meta.dir, ".."),
21+
stdout: "pipe",
22+
stderr: "pipe",
23+
});
24+
const exitCode = await proc.exited;
25+
const out = await new Response(proc.stdout).text();
26+
const err = await new Response(proc.stderr).text();
27+
return { exitCode, out, err };
28+
}
29+
1430
describe("CLI --help", () => {
1531
test("exits 0 and prints usage without touching the database", async () => {
16-
const indexTs = join(import.meta.dir, "index.ts");
17-
const proc = Bun.spawn([Bun.which("bun")!, indexTs, "--help"], {
18-
cwd: join(import.meta.dir, ".."),
19-
stdout: "pipe",
20-
stderr: "pipe",
21-
});
22-
const exitCode = await proc.exited;
23-
const out = await new Response(proc.stdout).text();
24-
const err = await new Response(proc.stderr).text();
32+
const { exitCode, out, err } = await runCli(["--help"]);
2533
expect(exitCode).toBe(0);
2634
expect(out).toContain("Usage:");
2735
expect(out).toContain("codemap query");
2836
expect(err).toBe("");
2937
});
3038
});
39+
40+
describe("CLI version", () => {
41+
test.each(["version", "--version", "-V"])(
42+
"%s prints version and exits 0",
43+
async (flag) => {
44+
const { exitCode, out, err } = await runCli([flag]);
45+
expect(exitCode).toBe(0);
46+
expect(out.trim()).toBe(CODEMAP_VERSION);
47+
expect(err).toBe("");
48+
},
49+
);
50+
});

src/cli.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { loadUserConfig, resolveCodemapConfig } from "./config";
77
import { closeDb, openDb } from "./db";
88
import { configureResolver } from "./resolver";
99
import { getProjectRoot, getTsconfigPath, initCodemap } from "./runtime";
10+
import { CODEMAP_VERSION } from "./version";
1011

1112
/** Printed for `codemap --help` / `-h` (must run before config or DB access). */
1213
export function printCliUsage(): void {
@@ -22,6 +23,10 @@ Query:
2223
Agents:
2324
codemap agents init [--force]
2425
26+
Other:
27+
codemap version
28+
codemap --version, -V
29+
2530
Environment: CODEMAP_ROOT (same as --root)
2631
2732
Options:
@@ -30,6 +35,10 @@ Options:
3035
`);
3136
}
3237

38+
export function printVersion(): void {
39+
console.log(CODEMAP_VERSION);
40+
}
41+
3342
export function parseBootstrapArgs(argv: string[]) {
3443
const envRoot = process.env.CODEMAP_ROOT ?? process.env.CODEMAP_TEST_BENCH;
3544
let root = envRoot ? resolve(envRoot) : undefined;
@@ -60,6 +69,11 @@ export async function main() {
6069
return;
6170
}
6271

72+
if (rest[0] === "--version" || rest[0] === "-V" || rest[0] === "version") {
73+
printVersion();
74+
return;
75+
}
76+
6377
if (rest[0] === "agents" && rest[1] === "init") {
6478
if (rest.includes("--help") || rest.includes("-h")) {
6579
console.log(`Usage: codemap agents init [--force]

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export {
1616
type ResolvedCodemapConfig,
1717
} from "./config";
1818
export type { ParsedFile } from "./parsed-types";
19+
export { CODEMAP_VERSION } from "./version";
1920

2021
function isMainModule(): boolean {
2122
if (

src/version.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import packageJson from "../package.json" with { type: "json" };
2+
3+
/** Package version from `package.json` (inlined at build time). */
4+
export const CODEMAP_VERSION = packageJson.version;

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"target": "ESNext",
44
"module": "ESNext",
55
"moduleResolution": "bundler",
6+
"resolveJsonModule": true,
67
"lib": ["ESNext"],
78
"types": ["bun", "node"],
89
"strict": true,

0 commit comments

Comments
 (0)