# Call a command-line tool from a station

Many jobs inside a station are best done by a tool that already exists: `qpdf` splits PDFs, `pdftotext` reads them, `unzip` unpacks archives, `ffmpeg` cuts audio. Call the tool with [execa](https://github.com/sindresorhus/execa), and put each call in a small function under `src/utils/` that your stations import.

```bash
npm install execa
```

## The wrapper

```js
// src/utils/unzip.js
import { execa } from 'execa';
import fs from 'node:fs';
import path from 'node:path';

const UNZIP = process.env.UNZIP_PATH || 'unzip';

export async function extractZip(zip_path, dest_dir) {
  fs.mkdirSync(dest_dir, { recursive: true });
  try {
    await execa(UNZIP, ['-o', '-qq', zip_path, '-d', dest_dir]);
  } catch (err) {
    throw new Error(`unzip_failed: ${path.basename(zip_path)}: ${err.stderr || err.message}`);
  }
}
```

| Rule | Why |
|---|---|
| Pass the arguments as an array, never as one shell string | A file name with spaces or quotes stays one argument, and no shell interprets it |
| Read the tool's path from an env var that defaults to its name (`UNZIP_PATH`) | The factory session can run with a different `PATH` from your terminal, for example under pm2. One env var fixes that without a code change. |
| Throw with a snake_case reason first (`unzip_failed:`), then the file's name and the tool's `stderr` | The reason groups the failures in `al-control workpieces errors`, and `stderr` usually says what went wrong |
| Pass execa a `timeout` for a tool that uses the network or can hang | A hung tool holds up the whole run |

## A missing tool fails the run; a bad file fails one workpiece

Check that the tool runs once, at the start of `execute`, before the loop over workpieces:

```js
async function assertUnzipAvailable() {
  try {
    await execa(UNZIP, ['-v']);
  } catch (err) {
    throw new Error(`unzip_missing: "${UNZIP}" did not run. Install it or set UNZIP_PATH. ${err.message}`);
  }
}

// in execute(), before the loop
await assertUnzipAvailable();
for (const workpiece_id of listWorkpieces(bin('IN1', 'input')).slice(0, config.batch_size)) {
  // processWorkpiece(...) calls extractZip() inside its body
}
```

| Where it throws | What happens |
|---|---|
| Before the loop (the tool is missing) | The run fails and the Orchestrator retries it. No workpiece moves. |
| Inside a workpiece's `body` (this file is broken) | Only that workpiece goes to `failed/`, with the tool's error. The loop goes on with the rest. |

For errors in general, see [Throw errors from a station](/docs/guides/station-errors).

Without the check, a missing tool fails every workpiece with the same error, and each one has to be requeued after the tool is installed. See [Write a station that processes workpieces](/docs/guides/process-workpieces) and [Find out why a workpiece failed](/docs/guides/handle-failures).

## Install the tool where the factory runs

A command-line tool isn't in `package.json`, so `npm install` doesn't bring it. Install it on every machine that runs the factory session, or point its env var at where it is.

## Next

- [Read, render and split PDFs](/docs/guides/work-with-pdfs): `pdfinfo`, `pdftotext`, `pdftoppm` and `qpdf`, called this way
- [Where a station keeps its data](/docs/guides/keep-station-data): where to put what a tool writes
