|
| 1 | +# APIMatic CLI — Project Instructions |
| 2 | + |
| 3 | +This file provides guidance when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +APIMatic CLI (`@apimatic/cli`) — the official CLI for APIMatic, built on oclif v4 with TypeScript ESM. It provides commands for API spec validation/transformation, SDK generation, and documentation portal management. |
| 8 | + |
| 9 | +## Common Commands |
| 10 | + |
| 11 | +```bash |
| 12 | +# Build |
| 13 | +npm run build # tsc -b → outputs to lib/ |
| 14 | + |
| 15 | +# Lint |
| 16 | +npm run lint # ESLint on src/**/*.{js,ts} |
| 17 | +npm run lint:fix # ESLint with --fix --quiet |
| 18 | + |
| 19 | +# Format |
| 20 | +npm run format # Prettier write on src/**/*.{js,ts} |
| 21 | + |
| 22 | +# Test (all) |
| 23 | +npm test # tsx + mocha, runs test/**/*.test.ts |
| 24 | + |
| 25 | +# Test (single file) |
| 26 | +npx tsx node_modules/mocha/bin/_mocha "test/actions/portal/serve.test.ts" --timeout 99999 |
| 27 | + |
| 28 | +# Run CLI locally |
| 29 | +node bin/run.js <command> |
| 30 | +``` |
| 31 | + |
| 32 | +## Architecture — 5-Layer Stack |
| 33 | + |
| 34 | +``` |
| 35 | +Command → Action → Application → Prompts / Infrastructure → Types |
| 36 | +``` |
| 37 | + |
| 38 | +1. **Commands** (`src/commands/`) — oclif `Command` subclasses. Parse flags, build `CommandMetadata`, call `intro()` → `action.execute()` → `outro(result)`. No business logic. |
| 39 | +2. **Actions** (`src/actions/`) — One per command. Orchestrate use-case: validate inputs via Context objects, coordinate services, return `ActionResult<T>`. Never throw to Command. |
| 40 | +3. **Application** (`src/application/`) — Complex reusable domain algorithms (e.g., TOC generators, recipe generators). Pure transformations: data in → data out. No prompts, no API calls. |
| 41 | +4. **Prompts** (`src/prompts/`) — All terminal UI via `@clack/prompts`. One class per command mirroring `actions/`. Uses `withSpinner` for async operations. No business logic. |
| 42 | +5. **Infrastructure** (`src/infrastructure/`) — I/O adapters: `FileService`, `ZipService`, `NetworkService`, API services in `services/`. All return `Result<T, ServiceError>` (neverthrow). |
| 43 | + |
| 44 | +Supporting: **Types** (`src/types/`) for value objects, context objects, and domain events, **client-utils** for auth credential management, **utils** for pure string helpers, **config** for shared Axios instance, **hooks** (`src/hooks/`) for oclif lifecycle hooks (e.g., command-not-found suggestions), **env-info** (`src/infrastructure/env-info.ts`) singleton for CLI version, user-agent string, and base URL resolution. |
| 45 | + |
| 46 | +## Critical Code Conventions |
| 47 | + |
| 48 | +- **ESM imports with `.js` extension** — even for `.ts` source files: `import { Foo } from "../../types/file/directoryPath.js"` |
| 49 | +- **No raw string paths** — use `DirectoryPath`, `FilePath`, `FileName`, `UrlPath` value objects from `src/types/file/` |
| 50 | +- **No `console.log`** — all output through `@clack/prompts` via Prompts classes only |
| 51 | +- **Error handling**: Services return `Result<T, ServiceError>` (neverthrow); Actions return `ActionResult<T>` (success/failed/cancelled). No uncaught throws above infrastructure. |
| 52 | +- **Prompts delegation** — Actions never call `log.*` directly; every message goes through `this.prompts.*` |
| 53 | +- **Temp directories** — always use `withDirPath()` wrapper, never `tmp-promise` directly |
| 54 | +- **`authKey` typed as `string | null = null`**, not `undefined` |
| 55 | +- **Exit via `outro(result)`** — sets `process.exitCode`; never call `process.exit()` directly |
| 56 | +- **`ActionResult` variants** — `success()`, `failed()`, `cancelled()`, `stopped()` (for long-running server commands); exit codes 0 / 1 / 130 respectively |
| 57 | +- **Constructor pattern**: `private readonly` properties, `public readonly execute = async (...) => { ... }` |
| 58 | +- **`static cmdTxt`** on every Command using `format.cmd(...)` for example rendering |
| 59 | +- **Commands use `export default class`** — oclif requires default export; actions, prompts, and services use named exports (`export class`) |
| 60 | +- **Static fields use `readonly`** — `static readonly summary`, `static readonly description`, `static readonly cmdTxt` on every Command |
| 61 | +- **Topic separator is space** — `apimatic portal generate`, not `apimatic portal:generate` |
| 62 | +- **Telemetry** — After `outro(result)`, commands optionally track failures via `result.mapAll(() => {}, async () => { await new TelemetryService(configDir).trackEvent(new SomeFailedEvent(...), shell) }, () => {})`. Event classes extend `DomainEvent` (`src/types/events/`). Only the failure callback is populated; success/cancel are no-ops. |
| 63 | + |
| 64 | +## Commit Conventions |
| 65 | + |
| 66 | +Uses [Conventional Commits](https://www.conventionalcommits.org/) enforced by commitlint + husky. Pre-commit runs lint-staged (ESLint + Prettier). |
| 67 | + |
| 68 | +**Do not commit or push automatically.** Always wait for explicit instruction from the user before running `git commit` or `git push`. |
| 69 | + |
| 70 | +## Testing |
| 71 | + |
| 72 | +- **Framework**: mocha + chai (expect style) + sinon + nock + mock-fs |
| 73 | +- **Test location**: mirrors source — `test/commands/`, `test/actions/`, `test/application/` |
| 74 | +- **HTTP mocking**: nock for API calls |
| 75 | +- **Run via tsx** (not ts-node) for ESM compatibility |
| 76 | + |
| 77 | +## Skills |
| 78 | + |
| 79 | +Reference these files for scaffolding new code: |
| 80 | + |
| 81 | +- `.ai/skills/command.md` — Command + Action + Prompts conventions; scaffolding templates and checklists |
| 82 | +- `.ai/skills/action.md` — Action class conventions; standard / minimal / delegation variants |
| 83 | +- `.ai/skills/context.md` — Context object conventions; output / input / temp / pure variants |
| 84 | +- `.ai/skills/prompt.md` — Prompts class conventions; simple / standard / delegation / wizard variants |
| 85 | +- `.ai/skills/service.md` — Infrastructure Service conventions; SDK controller / axios-auth / axios-stateless variants |
| 86 | +- `.ai/skills/value-object.md` — Value object (rich class) conventions; encapsulation, boundary unwrapping, composition rules |
0 commit comments