# axle graph

Show how packages depend on each other and the order tasks would run in, as text, a picture or an interactive page.

```sh
axle graph [<task>...] [--web | --format text|dot|mermaid|json|html|svg [--output <file>]] [--filter <selector>]... [--affected] [--base <ref>] [--head <ref>]
```

Without task names, `axle graph` shows the project graph: each package and project, and what it uses in the repository.

```text
@acme/core
@acme/ui uses @acme/core
@acme/web uses @acme/ui
```

With task names, it shows the tasks that [`axle run`](https://axlers.dev/docs/reference/run.md) would run, in order, with what each waits for:

```text
@acme/core#build
@acme/ui#build after @acme/core#build
@acme/web#build after @acme/ui#build
```

## In the browser

```sh
axle graph --web
axle graph build --web
```

`--web` writes the graph as one self-contained page in `.axle/graph/` and opens it in your browser. Nothing is served and nothing leaves the machine; run the command again to see changes. In the page:

- type in the search box to highlight nodes by name, and press Enter to focus on the first one;
- click a node to focus on it, with everything it depends on and everything that depends on it, or choose to see only one of the two;
- tick **Hide the rest** to show only what's in focus, and press Escape or click the background to see everything again;
- zoom with the mouse wheel or trackpad, and drag the background to move around;
- click **Reset** to clear the search, the focus and the zoom.

With `--affected`, the page and the SVG keep every node and highlight the affected ones; tick **Only affected** in the page to show only those. `text`, `dot`, `mermaid` and `json` leave out the packages a change doesn't affect.

## Formats

| `--format` | Prints                                                                                |
| ---------- | ------------------------------------------------------------------------------------- |
| `text`     | one line per node, the default                                                        |
| `dot`      | the graph for Graphviz                                                                |
| `mermaid`  | the graph for Mermaid diagrams, which GitHub shows in Markdown                        |
| `json`     | the graph for tools and editors                                                       |
| `html`     | the same page as `--web`, to keep or share                                            |
| `svg`      | a picture of the graph, laid out as on the page, to put in a pull request or a report |

`--output <file>` writes any format to a file instead of printing it, such as `axle graph build --format svg --output build.svg`. `--web` can't be combined with `--format` or `--output`; use `--format html --output <file>` to keep the page somewhere else. Axle lays the graph out itself, so the SVG and the page always agree, and 1,000 packages take a few milliseconds.

`json` prints the project graph as `projects`, each with its `name`, `path`, `kind` and `dependencies`, and the task graph as `tasks`, each with its [`description`](https://axlers.dev/docs/reference/configuration.md#tasks) or `null`:

```json
{
    "formatVersion": 1,
    "tasks": [
        {
            "task": "@acme/core#build",
            "package": "@acme/core",
            "script": "build",
            "variant": null,
            "description": "Compiles each package into dist",
            "folder": "packages/core",
            "persistent": false,
            "waitsFor": []
        },
        {
            "task": "@acme/ui#build",
            "package": "@acme/ui",
            "script": "build",
            "variant": null,
            "description": "Compiles each package into dist",
            "folder": "packages/ui",
            "persistent": false,
            "waitsFor": ["@acme/core#build"]
        }
    ]
}
```

These are the same fields as each task of a [dry run](https://axlers.dev/docs/reference/run.md#json-events), which adds what the task would run and whether it would come from the cache. On an error it prints `{ "formatVersion": 1, "error": { … } }`, as [`axle list --json`](https://axlers.dev/docs/reference/list.md#json-output) does. A file that can't be written is [an error](https://axlers.dev/docs/messages/graph-not-written.md).

`--filter`, `--affected`, `--base` and `--head` choose packages the same way as for [`axle run`](https://axlers.dev/docs/reference/run.md#choosing-packages).
