# Installation

```sh pm
pnpm add readerly
```

Requires Node 22.12 or later. readerly is ESM only and has no dependencies.

> [!NOTE]
> readerly is not on npm yet. Until it is, clone the repository, run `pnpm install` and `pnpm build` to write `dist/`, and depend on it by path, such as `"readerly": "file:../readerly"` in your `package.json`, or as a workspace package in a monorepo.

## First command

### Wrap a command with `createOut`

`createOut` takes the tool's name, the command and its `argv`. The command reports through `out` and returns `out.end()`, the exit code.

```ts [check.ts]
import { createOut } from "readerly";

export async function check(argv: string[]): Promise<number> {
  const out = createOut<{ files: number }>({
    tool: "acme",
    command: "check",
    argv,
    summary: (result) => (result.files === undefined ? undefined : `in ${result.files} files`),
  });
  for (const finding of await scan()) out.diagnostic(finding);
  out.set({ files: 212 });
  const fix = await out.confirm({
    name: "fix",
    flag: "--fix",
    default: false,
    message: "Apply 2 safe fixes?",
    reason: "applies 2 safe fixes",
  });
  if (fix) out.log("Applied 2 fixes.");
  out.next("acme explain <code>", "explains one code");
  return out.end();
}

process.exitCode = await check(process.argv.slice(2));
```

`scan()` stands for your tool's own work. Here it returns two errors and one warning.

### Run it in a terminal

A person at a terminal gets the findings, then the prompt on stderr. After Enter, the rest prints:

```text
✖ 2 errors, 1 warning in 212 files

error ACME001  src/app.ts:14:3  Route handler reads process.env at module scope
├▶ why: The value is fixed at build time
╰▶ fix: Read it inside the handler
error ACME004  src/db.ts:3:1  Top-level await in a server entry
╰▶ fix: Move the call into the plugin setup
warn  ACME010  vite.config.ts:8:5  Alias shadows a workspace package
? Apply 2 safe fixes? (y/N) 

next: acme explain <code>   (explains one code)
```

In a color terminal, `✖` and `error` are red, `warn` yellow and the tree glyphs dim.

### Run it as an agent would

Set `AI_AGENT`, as a coding agent's shell does:

```sh
AI_AGENT=claude-code acme check
```

A coding agent gets one line per finding and no prompt. The prompt it skipped becomes a `next:` line with the flag:

```text
acme check: failed. 2 errors, 1 warning in 212 files.
error ACME001 src/app.ts:14:3 Route handler reads process.env at module scope. fix: Read it inside the handler
error ACME004 src/db.ts:3:1 Top-level await in a server entry. fix: Move the call into the plugin setup
warn ACME010 vite.config.ts:8:5 Alias shadows a workspace package
next: acme check --fix   (applies 2 safe fixes)
next: acme explain <code>   (explains one code)
```

Piped with `acme check | cat`, the command prints the terminal's `text` without the prompt, and the same `next: acme check --fix` line.

### Ask for JSON

```sh
acme check --format json
```

A program gets one document on stdout:

```text
{
  "schema": "acme.check/v1",
  "status": "failed",
  "summary": {
    "error": 2,
    "warn": 1,
    "info": 0
  },
  "diagnostics": [
    {
      "severity": "error",
      "code": "ACME001",
      "message": "Route handler reads process.env at module scope",
      "why": "The value is fixed at build time",
      "fix": "Read it inside the handler",
      "file": "src/app.ts",
      "line": 14,
      "column": 3
    },
    {
      "severity": "error",
      "code": "ACME004",
      "message": "Top-level await in a server entry",
      "fix": "Move the call into the plugin setup",
      "file": "src/db.ts",
      "line": 3,
      "column": 1
    },
    {
      "severity": "warn",
      "code": "ACME010",
      "message": "Alias shadows a workspace package",
      "file": "vite.config.ts",
      "line": 8,
      "column": 5
    }
  ],
  "result": {
    "files": 212
  },
  "next": [
    {
      "command": "acme check --fix",
      "reason": "applies 2 safe fixes"
    },
    {
      "command": "acme explain <code>",
      "reason": "explains one code"
    }
  ]
}
```

Every run exits `1`, because the run has errors.

Next: [Readers](https://readerly.loadsheet.dev/guide/readers) explains how readerly told these runs apart.

<!-- vitehub-docs:section-links -->
## In this section

- [Introduction](https://readerly.loadsheet.dev/raw/guide.md)
- [Installation](https://readerly.loadsheet.dev/raw/guide/installation.md) (this page)
- [Readers](https://readerly.loadsheet.dev/raw/guide/readers.md)
- [Coding agents](https://readerly.loadsheet.dev/raw/guide/agents.md)
- [Formats](https://readerly.loadsheet.dev/raw/guide/formats.md)
- [Shared flags](https://readerly.loadsheet.dev/raw/guide/flags.md)
- [Color and glyphs](https://readerly.loadsheet.dev/raw/guide/color.md)
- [Diagnostics](https://readerly.loadsheet.dev/raw/guide/diagnostics.md)
- [Budget](https://readerly.loadsheet.dev/raw/guide/budget.md)
- [Prompts and input](https://readerly.loadsheet.dev/raw/guide/prompts.md)
- [Failures and exit codes](https://readerly.loadsheet.dev/raw/guide/failures.md)
- [JSON and JSONL](https://readerly.loadsheet.dev/raw/guide/json.md)
- [Custom renderers](https://readerly.loadsheet.dev/raw/guide/renderers.md)
- [API](https://readerly.loadsheet.dev/raw/guide/api.md)

Next: [Readers](https://readerly.loadsheet.dev/raw/guide/readers.md)

All pages: [llms.txt](https://readerly.loadsheet.dev/llms.txt)
