Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .cursor/rules/silverfin-cli-context.mdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
description:
alwaysApply: true
---

# silverfin-cli — agent context

Before non-trivial changes in this package:

1. Follow **AGENTS.md** (repo root) — commands (`npm test`, `npm run lint`), entry points (`bin/cli.js`, `index.js`), public API stability, auth/error pointers.
2. Use **docs/ARCHITECTURE.md** for the module map and how CLI vs `index.js` vs `lib/` connect.

Coding style: match existing code; run **ESLint** and **Prettier** settings from the repo (`npm run lint`, `package.json` `prettier` field). Do not duplicate long style lists here.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
node_modules
.env
.DS_Store
./tmp
./tmp
# Leftover from tests/bin/cli/import-reconciliation.test.js (mkdtemp) if a run is interrupted
tests/bin/cli/temp-*
49 changes: 49 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Agent / contributor guide (silverfin-cli)

Short orientation for working in this repository. End-user setup, credentials, and template **repository** layout are documented in [README.md](README.md).

## Commands

- **Full test suite:** `npm test`
- **Lint:** `npm run lint`
- **Focused Jest run:** `npx jest path/to/test.js` (or a directory under `tests/`)

Before finishing a change that touches library or CLI behavior, run **lint and the full test suite**.

## Entry points

| Layer | Path | Role |
|--------|------|------|
| CLI executable | [bin/cli.js](bin/cli.js) | [Commander](https://github.com/tj/commander.js) program: defines subcommands, parses options, calls `index` exports and helpers (`liquidTestRunner`, `liquidTestGenerator`, etc.). |
| Programmatic API | [index.js](index.js) | `require('silverfin-cli')` surface: template fetch/publish/create helpers and related utilities. See **Public API** below. |
| Core libraries | [lib/](lib/) | `api/` (HTTP + auth), `templates/` (reconciliation, shared parts, export files, account templates), `cli/` (cwd checks, completions, updater, stats), `utils/` (fs, API helpers, errors, Liquid test helpers). |

## Public API (`index.js`)

The package `main` is [index.js](index.js). Downstream tools (e.g. the [Silverfin VS Code extension](https://github.com/silverfin/silverfin-vscode)) may depend on **named exports** on `module.exports`. Treat additions as safe; **removals or signature changes** are semver-sensitive—prefer deprecation when possible.

## Invariants / do not break

- **Global vs local install:** README recommends a **global** install; some flows (e.g. `update`) assume global usage. Local `node_modules` installs can behave differently.
- **Backward compatibility:** Keep the `index.js` export object stable for extension and automation consumers unless you are intentionally shipping a major version.
- **User template repos** follow the folder layout in README (`reconciliation_texts/`, `shared_parts/`, etc.). That is **not** the same as this package’s `lib/` tree—do not confuse the two.

## Auth and networking

- **OAuth / tokens / firm storage:** [lib/api/silverfinAuthorizer.js](lib/api/silverfinAuthorizer.js) (browser flow, refresh, partner keys).
- **HTTP client and API calls:** [lib/api/sfApi.js](lib/api/sfApi.js) (uses [lib/api/axiosFactory.js](lib/api/axiosFactory.js) for authenticated instances).
- **Stored credentials / host:** [lib/api/firmCredentials.js](lib/api/firmCredentials.js).

Environment variables and scopes: see [README.md](README.md) (e.g. `SF_API_CLIENT_ID`, `SF_API_SECRET`; host via `silverfin config --set-host`).

## Errors and logging

- **Shared helpers:** [lib/utils/errorUtils.js](lib/utils/errorUtils.js) — missing IDs/config, batch reconciliation summaries, `uncaughtErrors` / `errorHandler`.
- **CLI process handlers:** [lib/cli/utils.js](lib/cli/utils.js) (`handleUncaughtErrors`) wires `uncaughtRejection` / `uncaughtException` to `errorUtils.uncaughtErrors`.
- **User-facing messages:** [consola](https://github.com/unjs/consola) and [chalk](https://github.com/chalk/chalk) (e.g. suggested fix-it commands). Prefer extending `errorUtils` for consistent messaging rather than ad hoc `console.log` in many places.

## When in doubt

1. Run `npm run lint` and `npm test`.
2. Mirror patterns in the nearest existing command or module ([bin/cli.js](bin/cli.js) + matching code in [index.js](index.js) / [lib/](lib/)).
3. For structure and data flow, see [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md).
84 changes: 84 additions & 0 deletions docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# silverfin-cli architecture

High-level map of the package. For commands to run and contributor rules, see [AGENTS.md](../AGENTS.md). For how **users** lay out a Liquid template repository on disk, see [README.md](../README.md) — that layout is **not** the same as this repo’s `lib/` tree.

## Flow overview

```mermaid
flowchart LR
subgraph cli_layer [CLI]
Bin[bin/cli.js]
end
subgraph prog [Programmatic API]
Index[index.js]
end
subgraph lib_pkg [lib]
API[api]
TPL[templates]
CLIUTIL[cli]
UTILS[utils]
LIQ[liquidTestRunner / liquidTestGenerator]
EXP[exportFileInstanceGenerator]
end
Bin --> Index
Bin --> LIQ
Bin --> EXP
Index --> API
Index --> TPL
Index --> UTILS
API --> UTILS
TPL --> UTILS
CLIUTIL --> UTILS
LIQ --> UTILS
```

- **bin/cli.js** is the Commander entry: it calls **`index.js`** for template workflows and pulls in **Liquid test** and **export-instance** modules directly where the CLI needs them.
- **index.js** orchestrates template types (reconciliations, shared parts, export files, account templates) using **`lib/api`** and **`lib/templates`**, plus filesystem helpers.

## `lib/` responsibilities

### `lib/api/`

| Module | Responsibility |
|--------|------------------|
| [sfApi.js](../lib/api/sfApi.js) | Silverfin REST calls (reconciliations, shared parts, export files, account templates, etc.). Builds axios instances via `AxiosFactory`, handles responses with `apiUtils`. |
| [silverfinAuthorizer.js](../lib/api/silverfinAuthorizer.js) | OAuth-style firm authorization (browser + code), token refresh, partner token refresh. |
| [axiosFactory.js](../lib/api/axiosFactory.js) | Configured axios instances for firm/partner contexts (base URL, auth headers, optional basic auth). |
| [firmCredentials.js](../lib/api/firmCredentials.js) | Persisted tokens, host, firm name; read/write credential store used by authorizer and API layer. |

### `lib/templates/`

Object-oriented wrappers around on-disk template folders and sync with the API:

- **reconciliationText.js** — reconciliation texts under `reconciliation_texts/`
- **sharedPart.js** — `shared_parts/`
- **exportFile.js** — `export_files/`
- **accountTemplate.js** — `account_templates/`

They work with [fsUtils.js](../lib/utils/fsUtils.js) and [templateUtils.js](../lib/utils/templateUtils.js) for paths, configs, and IDs.

### `lib/cli/`

Command-line–specific behavior (not re-exported as the main programmatic API):

- **utils.js** — option checks, default firm id, uncaught error wiring to `errorUtils`
- **cwdValidator.js** — working-directory validation for repo conventions
- **autoCompletions.js** — shell completions
- **cliUpdater.js** — CLI self-update
- **stats.js**, **changelogReader.js**, **spinner.js**, **devMode.js** — UX and maintenance helpers

### `lib/utils/`

Shared cross-cutting helpers: **errorUtils** (messages, exits, batch summaries), **fsUtils**, **apiUtils** (env checks, response handlers), **urlHandler**, **wslHandler**, **runTestUtils**, **liquidTestUtils**, etc.

### Root-level `lib/*.js` (not under a subfolder)

| Module | Responsibility |
|--------|------------------|
| [liquidTestRunner.js](../lib/liquidTestRunner.js) | Run Liquid tests from the CLI against the API. |
| [liquidTestGenerator.js](../lib/liquidTestGenerator.js) | Generate test YAML from company data. |
| [exportFileInstanceGenerator.js](../lib/exportFileInstanceGenerator.js) | Export file instance generation workflow used by CLI commands. |

## Tests

Jest tests live under [tests/](../tests/) and generally mirror `lib/`. See [AGENTS.md](../AGENTS.md) for commands; API tests often use `axios-mock-adapter`.
Loading
Loading