|
1 | | -# wiki-cli |
| 1 | +# Wiki CLI (`apps/wiki-cli`) |
2 | 2 |
|
3 | | -## `src/index.ts` |
| 3 | +`wiki-cli` is the centralized command-line interface application for the LLM Wiki Knowledge Monorepo. Built as a single bundled Node.js application, it exposes workspace maintenance tasks—manifest generation, index generation, tag distribution validation, and directory scaffolding—as native Nx targets. |
4 | 4 |
|
5 | | -CLI entry point. Reads the subcommand from `process.argv`, resolves the workspace root, and dispatches to the matching command module: |
| 5 | +--- |
6 | 6 |
|
7 | | -- `generate-manifest` → `runGenerateManifest` |
8 | | -- `generate-index` → `runGenerateIndex` |
9 | | -- `validate-tags` → `runValidateTags` |
10 | | -- `init` → `runInit` |
| 7 | +## Architectural Role |
11 | 8 |
|
12 | | -Sets `process.exitCode` to `1` for unknown commands or unhandled errors. |
| 9 | +`wiki-cli` serves as the **Presentation / Driver Layer** in the system's Clean (Hexagonal) Architecture: |
13 | 10 |
|
14 | | -## `src/commands` |
| 11 | +```text |
| 12 | + ┌────────────────────────┐ |
| 13 | + │ apps/wiki-cli │ (Driver / Presentation Layer) |
| 14 | + │ - src/index.ts │ Command CLI Router |
| 15 | + │ - src/commands/*.ts │ Composition Roots |
| 16 | + └───────────┬────────────┘ |
| 17 | + │ Wires & Injects |
| 18 | + ▼ |
| 19 | +┌──────────────────────────────────────┐ |
| 20 | +│ libs/application-* │ (Application Layer) |
| 21 | +│ - application-index-manager │ Use Cases (Pure Domain Logic) |
| 22 | +│ - application-tag-validation │ |
| 23 | +│ - application-scaffolding │ |
| 24 | +└──────────────────▲───────────────────┘ |
| 25 | + │ Depends on Interfaces |
| 26 | +┌──────────────────┴───────────────────┐ |
| 27 | +│ libs/infrastructure-* │ (Infrastructure Layer) |
| 28 | +│ - infrastructure-filesystem │ Adapters (FS, YAML, Markdown) |
| 29 | +│ - infrastructure-frontmatter │ |
| 30 | +│ - infrastructure-markdown │ |
| 31 | +└──────────────────────────────────────┘ |
| 32 | +``` |
15 | 33 |
|
16 | | -Each file is a thin composition root: it wires Infrastructure adapters into an Application use case and prints the result. No business logic lives here. |
| 34 | +- **Composition Roots Only**: Code inside `wiki-cli` contains no business logic. Its sole responsibility is parsing CLI arguments, instantiating concrete Infrastructure adapters, passing them to Application use cases, and rendering formatted outputs to `stdout`. |
| 35 | +- **Decoupled Business Rules**: Features such as threshold validation, markdown index formatting, manifest scanning, and directory scaffolding live entirely within pure `@wiki/application-*` libraries. |
17 | 36 |
|
18 | | -- **`generate-manifest.command.ts`** Wires `FileSystemAdapter` into `GenerateManifestUseCase`. |
19 | | -<br> Writes `wiki/manifest.json` and prints the list of files it contains. |
20 | | -- **`generate-index.command.ts`** Wires `FileSystemAdapter`, `FrontmatterAdapter`, and `MarkdownAdapter` into `GenerateIndexUseCase`. |
21 | | -<br> Regenerates `wiki/index.md` and prints the entity/concept/source counts. |
22 | | -- **`validate-tags.command.ts`** Wires `FileSystemAdapter` and `FrontmatterAdapter` into `ValidateTagDistributionUseCase`. |
23 | | -<br> Prints a formatted tag frequency table plus any threshold violations (60% max), and returns `0` (pass) or `1` (fail) as the process exit code. |
24 | | -- **`init.command.ts`** Checks for an existing Angular project (informational only) then wires `FileSystemAdapter` into `ScaffoldWikiUseCase` to create the initial `wiki/`/`raw/` directory structure, logging created vs. existing directories. |
| 37 | +--- |
| 38 | + |
| 39 | +## Workspace Subcommands & Nx Targets |
| 40 | + |
| 41 | +You can execute commands via Nx targets from the workspace root: |
| 42 | + |
| 43 | +| Nx Target | Subcommand | Description | |
| 44 | +|---|---|---| |
| 45 | +| `nx run wiki-cli:generate-manifest` | `generate-manifest` | Scans `wiki/` pages and generates `wiki/manifest.json`. | |
| 46 | +| `nx run wiki-cli:generate-index` | `generate-index` | Re-indexes entities, concepts, and sources into `wiki/index.md`. | |
| 47 | +| `nx run wiki-cli:validate-tags` | `validate-tags` | Validates tag frequency distribution against the 60% threshold. Returns exit code 0 or 1. | |
| 48 | +| `nx run wiki-cli:init` | `init` | Scaffolds the initial `wiki/` and `raw/` directory structure. | |
| 49 | + |
| 50 | +### Direct Binary Execution |
| 51 | + |
| 52 | +After running `nx run wiki-cli:build`, the compiled bundle resides at `dist/apps/wiki-cli/index.cjs` and can be invoked directly: |
| 53 | + |
| 54 | +```bash |
| 55 | +node dist/apps/wiki-cli/index.cjs generate-manifest |
| 56 | +node dist/apps/wiki-cli/index.cjs generate-index |
| 57 | +node dist/apps/wiki-cli/index.cjs validate-tags |
| 58 | +node dist/apps/wiki-cli/index.cjs init |
| 59 | +``` |
| 60 | + |
| 61 | +--- |
| 62 | + |
| 63 | +## Directory Structure |
| 64 | + |
| 65 | +```text |
| 66 | +apps/wiki-cli/ |
| 67 | +├── project.json # Nx project configuration & targets |
| 68 | +├── package.json # App package definition |
| 69 | +├── tsconfig.json # TypeScript base configuration |
| 70 | +├── tsconfig.app.json # Build-specific TS configuration |
| 71 | +├── tsconfig.spec.json # Test TS configuration |
| 72 | +├── vitest.config.ts # Vitest test runner setup |
| 73 | +├── README.md # Module documentation |
| 74 | +└── src/ |
| 75 | + ├── index.ts # CLI Entry Point & command router |
| 76 | + ├── wrappers.smoke.spec.ts # Smoke tests for CLI binary & contract |
| 77 | + └── commands/ # Subcommand composition roots |
| 78 | + ├── generate-manifest.command.ts |
| 79 | + ├── generate-index.command.ts |
| 80 | + ├── validate-tags.command.ts |
| 81 | + ├── init.command.ts |
| 82 | + └── README.md # Commands documentation |
| 83 | +``` |
| 84 | + |
| 85 | +--- |
| 86 | + |
| 87 | +## Entry Point & Command Dispatcher (`src/index.ts`) |
| 88 | + |
| 89 | +- **Workspace Root Resolution**: Resolves the monorepo root dynamically using `path.resolve(__dirname, '..', '..', '..')` so paths remain consistent regardless of working directory. |
| 90 | +- **CLI Dispatching**: Extracts `process.argv[2]`, matches it to handler functions in `src/commands/`, and sets `process.exitCode` appropriately. |
| 91 | +- **Exit Code Contract**: |
| 92 | + - `0`: Successful execution or passing validation. |
| 93 | + - `1`: Unknown command, thrown error, or failed validation (e.g., tag frequency > 60%). |
| 94 | + |
| 95 | +--- |
| 96 | + |
| 97 | +## Build & Test |
| 98 | + |
| 99 | +### Building |
| 100 | + |
| 101 | +The build target uses `@nx/esbuild:esbuild` to bundle `wiki-cli` into a single CommonJS executable (`dist/apps/wiki-cli/index.cjs`) with a Node hashbang banner (`#!/usr/bin/env node`): |
| 102 | + |
| 103 | +```bash |
| 104 | +npx nx run wiki-cli:build |
| 105 | +``` |
| 106 | + |
| 107 | +### Testing |
| 108 | + |
| 109 | +Smoke tests verify that the compiled binary executes properly and adheres to output and exit-code contracts: |
| 110 | + |
| 111 | +```bash |
| 112 | +npx nx run wiki-cli:test |
| 113 | +``` |
0 commit comments