> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# CLI tutorial

We provide `@rsdoctor/cli` for you to use Rsdoctor's features locally through the CLI program.

:::tip
Use the same version of `@rsdoctor/cli` and `@rsdoctor/core`.
:::

## Install @rsdoctor/cli

:::tip

- You can also use the non-installation method by using the `npx @rsdoctor/cli <command> [options]` command.

:::


```sh [npm]
npm add @rsdoctor/cli -D
```

```sh [yarn]
yarn add @rsdoctor/cli -D
```

```sh [pnpm]
pnpm add @rsdoctor/cli -D
```

```sh [bun]
bun add @rsdoctor/cli -D
```

```sh [deno]
deno add npm:@rsdoctor/cli -D
```

## Command usage

```bash

rsdoctor <command> [options]

```

`@rsdoctor/cli` currently provides the following commands for different functionalities:

### analyze command

The `analyze` command is mainly used to load the [manifest.json](/config/options/term.md) file locally and start Rsdoctor's analysis report page without the need to rebuild.

```bash
rsdoctor analyze --profile <manifestFile>
```

**Parameter Definition**

- `manifestFile` is the path to the [manifest.json](/config/options/term.md) file (supports local path)

**Usage Example**

```bash
rsdoctor analyze --profile "./dist/.rsdoctor/manifest.json"
```

### `bundle-diff` command

The `bundle-diff` command loads **two** [manifest.json](/config/options/term.md#manifestjson) files **locally** and compares their build bundles. By default, it opens the Rsdoctor [Bundle Diff](/guide/usage/bundle-diff.md) page in the browser. Use `--json [path]` to generate a JSON report without starting the report server or opening the browser.

```bash
rsdoctor bundle-diff --baseline <baselineManifestJsonPath> --current <currentManifestJsonPath>
```

**Parameter definitions**

- `--baseline <path>`: Path or online URL of the [manifest.json](/config/options/term.md#manifestjson) used as the **baseline**.
- `--current <path>`: Path or online URL of the [manifest.json](/config/options/term.md#manifestjson) used as the **current** build.
- `--json [path]`: Generate a JSON diff report. The default output path is `rsdoctor-diff.json`.

**Open the report in the browser**

```bash
rsdoctor bundle-diff --baseline="baseline/.rsdoctor/manifest.json" --current="current/.rsdoctor/manifest.json"
```

**Generate a JSON report**

```bash
rsdoctor bundle-diff \
  --baseline="baseline/.rsdoctor/manifest.json" \
  --current="current/.rsdoctor/manifest.json" \
  --json="result.json"
```

The JSON report contains diff data for assets, modules, and packages. You can consume it in CI workflows or use it to generate pull request comments.

## Node API

We provide a Node.js API in `@rsdoctor/cli` that allows you to make calls during runtime in Node.js.

**Importing the module**

```js
import { execute } from '@rsdoctor/cli';
```

**execute()**

The `execute` asynchronous function is the execution function of Rsdoctor CLI. By calling the `execute` function, it will automatically parse [process.argv](https://nodejs.org/dist/latest-v22.x/docs/api/process.html#processargv) and invoke different commands.

**execute('analyze', \{...})**

If you need to directly execute the [analyze command](#analyze-command) through the Node.js API, you can call it as follows:

```ts
execute('analyze', {
  profile: 'input the manifest.json path or url',
}).then((sdk) => {
  console.log('execute "analyze" command success');
  // you can stop the Rsdoctor's dev-server by calling the sdk'api below:
  // sdk.dispose();
});
```
