# axle exec

Run any command in each package's folder, dependencies first and in parallel.

```sh
axle exec [options] [--] <command> [<arguments>...]
```

Runs one command in the folder of every package and project, or of those `--filter` and `--affected` choose. A package waits for the chosen packages it depends on, and the rest run side by side, as many at once as the machine has CPU cores. It's for commands that aren't scripts, such as a one-off type check or a clean-up:

```text
$ axle exec --filter "./apps/*" -- npx tsc --noEmit
Running npx tsc --noEmit in 2 packages with pnpm, up to 16 tasks at once.

@acme/admin#exec  Found 0 errors.
@acme/web#exec    Found 0 errors.

2 tasks passed in 3.1s
```

Each package's run is named `<package>#exec`, and `axle exec` never reads or writes the cache. Since nothing is cached, the command sees every environment variable, as it would in a terminal. The command runs through the system's shell, like a `command` in `axle.toml`, so `axle exec -- "rm -rf dist && echo cleaned"` works too. The first word goes to the shell as it is, so a program whose path has spaces needs quotes of its own, while each word after it reaches the program as one argument. Put `--` before the command so its own options aren't read as Axle's.

## Running again on changes

`--watch` runs the command in each chosen package, then waits, and runs it again in the packages a change reaches: the package whose files changed and every chosen package that depends on it, directly or through others. It watches files the way [`axle watch`](https://axlers.dev/docs/reference/watch.md#which-files-count) does, with the same `[watch]` settings, and prints labelled lines:

```text
$ axle exec --watch --filter "./packages/*" -- npx tsc --noEmit
Running npx tsc --noEmit in 3 packages with pnpm, up to 16 tasks at once.

@acme/core#exec  Found 0 errors.
@acme/ui#exec    Found 0 errors.
@acme/web#exec   Found 0 errors.

3 tasks passed in 3.4s

Waiting for changes. Press Ctrl+C to stop.

Changed packages/ui/src/button.tsx
@acme/ui#exec    Found 0 errors.
@acme/web#exec   Found 0 errors.

2 tasks passed in 2.2s
```

A change to a file outside every package, such as a root `tsconfig.json`, reaches every package, unless [`globalInputs`](https://axlers.dev/docs/reference/configuration.md#inputs) says which root files count. After a change, the command also gets two environment variables, which aren't set on the first run:

| Variable                | What it holds                                                                                  |
| ----------------------- | ---------------------------------------------------------------------------------------------- |
| `AXLE_CHANGED_PACKAGES` | the names of the changed packages: this one, the packages it depends on, or both, one per line |
| `AXLE_CHANGED_FILES`    | the changed files in those packages, and changed files outside every package, one per line     |

Files are relative to the repository's root, which `AXLE_ROOT` holds, with `/` between folders, so a script can join the two to find each file. A change while the command runs stops it and starts again with everything that changed. Ctrl+C stops watching, and Axle exits with 0.

## Options

| Option                               | What it does                                                                                       |
| ------------------------------------ | -------------------------------------------------------------------------------------------------- |
| `--filter <selector>`                | runs only in some packages, as for [axle run](https://axlers.dev/docs/reference/run.md#choosing-packages)              |
| `--affected`                         | runs only in packages a change affects, and the packages that depend on them                       |
| `--base <ref>`, `--head <ref>`       | what `--affected` compares; see [axle affected](https://axlers.dev/docs/reference/affected.md)                         |
| `--concurrency <count>`              | how many packages run it at once: a number, or a share such as `50%`                               |
| `--continue`, `--continue=always`    | runs in every package even after a failure                                                         |
| `--continue=never`                   | stops at the first failure, the default                                                            |
| `--continue=dependencies-successful` | after a failure, runs only where the packages it depends on passed                                 |
| `--log-order`, `--output-logs`       | as for [axle run](https://axlers.dev/docs/reference/run.md#options)                                                    |
| `--ui tui`, `--ui stream`            | an interactive view, the default in a terminal, or labelled lines                                  |
| `--json`                             | prints one JSON event per line, as [axle run](https://axlers.dev/docs/reference/run.md) does                           |
| `--log-file <file>`                  | also writes the output and the summary to a file, as for [axle run](https://axlers.dev/docs/reference/run.md#log-file) |
| `--watch`                            | runs the command again where a change reaches; see [above](#running-again-on-changes)              |

`--watch` can't be used with `--affected` or `--ui`. Without it, the exit code is the first failing command's, `0` when every one passed, or 130 when you stop the run with Ctrl+C. Without a command, `axle exec` stops with [no command was given](https://axlers.dev/docs/messages/no-command-given.md).
