# axle generate

Create, move or remove a package or an app, or create files from the repository's own templates and the generators plugins ship.

```sh
axle generate
axle generate package ui-kit
axle generate app web --with react
axle generate ci --set provider=github
axle generate move ui-kit --folder libs/ui-kit
axle generate remove old-admin
axle generate component Button --set package=web
```

`axle generate` creates new files from a generator: the built-in [`package`](#new-packages), [`app`](#new-apps), [`ci`](#ci-workflows), [`move`](#moving-a-package) and [`remove`](#removing-a-package) generators, the repository's own, listed in `axle.toml`, or one a plugin ships. [Writing generators](https://axlers.dev/docs/crafting-your-repository/writing-generators.md) shows how to make your own.

## For editors and scripts

`--json` prints JSON and asks nothing. Without a generator, it lists each one with its `variables`, each with its `prompt`, `default`, `choices` and `pattern`, whether it takes a `folder` (`required`, `optional` or `none`), and for `app` and `package` the `starters` that `--with` takes. With a generator, it prints what was written, or with `--dry-run` what would be: each file's `diff` under `changes`, the command or template that starts an app, or where `move` moves a package. The [editor extensions](https://axlers.dev/docs/getting-started/editor-integration.md) build their generator form from it.

## Listing generators

`axle generate` with no generator lists them all, with where each comes from:

```text
package      A new package in the workspace, ready to build                             (built in)
app          A new app, from a framework's own tool, a copy, or empty                   (built in)
ci           A CI workflow that runs what each change affects                           (built in)
move         Move a package to another folder, and update the paths that point into it  (built in)
remove       Remove a package that nothing depends on                                   (built in)
component    A React component with a test                                              (this repository)
kit:service  A service the kit plugin runs                                              (plugin kit)

Run axle generate <generator> to use one.
```

A plugin's generators are named after the plugin, as `<plugin>:<generator>`.

## Giving values

A generator asks for values, such as a component's name. Give them on the command line:

| Option                     | What it does                                                                                       |
| -------------------------- | -------------------------------------------------------------------------------------------------- |
| `<NAME>`                   | the value of the generator's `name` variable, so `axle generate component Button` sets `name`      |
| `--set <variable>=<value>` | the value of any variable; repeat it for each one                                                  |
| `--folder <folder>`        | writes the files into this folder, relative to the repository root, instead of the generator's own |
| `--dry-run`                | shows the files as a diff, and writes nothing                                                      |

In a terminal, Axle asks for each value you didn't give, offering the generator's choices as a numbered list and its default in parentheses. Anywhere else, such as in CI or from a script, a value with no default is the [error](https://axlers.dev/docs/messages/generator-variable-missing.md) `generator-variable-missing`. A value that doesn't match the generator's pattern or choices is [refused](https://axlers.dev/docs/messages/generator-variable-invalid.md) before anything is written, and so is a value for a variable the generator doesn't have.

## What it writes

A generator that creates files never changes one that's already there; only [`move`](#moving-a-package) and [`remove`](#removing-a-package) edit existing files. When any file it would write exists, it stops with [`generated-file-exists`](https://axlers.dev/docs/messages/generated-file-exists.md) and writes nothing, so a generator can be run again with other values but never overwrites your work. Every file stays inside the repository, and nothing is written into `.git`.

It writes all of its files or none, and prints each one:

```text
Created packages/web/src/components/button/button.test.tsx
Created packages/web/src/components/button/button.tsx
```

## New packages

```sh
axle generate package @acme/ui-kit
axle generate package admin --folder apps/admin
```

The `package` generator creates a package in `packages/` when your workspace lists `packages/*`, and otherwise in the first folder it lists, named after the last part of the package's name: `@acme/ui-kit` goes in `packages/ui-kit`. In a terminal it asks where it should go, with that folder as the answer to accept; elsewhere, give `--folder` to put it somewhere else. A repository whose workspace lists no such folder needs it. The folder has to be one the workspace lists, such as `packages/admin` for `packages/*`, since your package manager wouldn't see a package anywhere else; add a new folder to the workspace first.

| File                  | What's in it                                                                               |
| --------------------- | ------------------------------------------------------------------------------------------ |
| `package.json`        | the name, version `0.0.0`, `private`, `"type": "module"`, its exports and a `build` script |
| `src/index.ts`        | an export to start from, or `src/index.js` when the repository doesn't use TypeScript      |
| `tsconfig.json`       | with TypeScript only: compiles `src` into `dist` with declarations, and `build` runs `tsc` |
| `oxlint.config.mts`   | with an `oxlint-config` package: extends it                                                |
| `eslint.config.mjs`   | with an `eslint-config` package: exports it                                                |
| `prettier.config.mjs` | with a `prettier-config` package: exports it                                               |
| `oxfmt.config.mts`    | with an `oxfmt-config` package: extends it                                                 |
| `biome.json`          | with a `biome-config` package: extends it                                                  |

For a library that's built with tsdown and tested with Vitest, as in the `library` [template](https://axlers.dev/docs/getting-started/templates.md), give `--with library`: the package is copied from that template and linked to the config packages as [an app](#new-apps) is. `--copy` copies any package, as it does for apps. In a terminal, without either, it asks which of the two it should be.

The package uses TypeScript when the workspace depends on `typescript`, and then on the same version. Without it, `build` checks the source with `node --check`, ready to be replaced by your real build.

It follows the [config packages](https://axlers.dev/docs/getting-started/templates.md#shared-settings) the workspace has, found by their names, such as `@repo/eslint-config`. The new package depends on each one, `tsconfig.json` extends `typescript-config`'s `base.json`, and it gets the `lint` and `format` scripts that run them, with the versions of `oxlint`, `eslint`, `prettier`, `oxfmt` or `@biomejs/biome` the workspace already uses. Add rules to its own files to override the shared ones. The name must be a valid npm package name that isn't in the workspace yet.

Then install, so the package manager links the new package into the workspace:

```sh
pnpm install                                    # pnpm
npm install                                     # npm
yarn install                                    # Yarn
bun install                                     # Bun
nub install                                     # nub
aube install                                    # aube
```

## New apps

```sh
axle generate app web --with react
axle generate app admin --copy web
axle generate app docs --copy https://github.com/owner/repository/tree/main/apps/docs
```

The `app` generator creates an app in `apps/` when your workspace lists `apps/*`, and otherwise in the first folder it lists, named after the last part of its name. Like `package`, it asks where it should go in a terminal, and takes `--folder` anywhere. It starts the app one of three ways:

| Option             | What it does                                                                                                                             |
| ------------------ | ---------------------------------------------------------------------------------------------------------------------------------------- |
| `--with FRAMEWORK` | runs the framework's own tool, with its default answers, so the app is exactly what that tool makes                                      |
| `--copy SOURCE`    | copies a workspace package, given by its name or folder, or a folder on GitHub, leaving out `node_modules`, build output and Git's files |
| neither            | in a terminal, asks for the framework; anywhere else, writes a `package.json` and a source file, as [`package`](#new-packages) does      |

| Framework      | Its tool                                                                      |
| -------------- | ----------------------------------------------------------------------------- |
| `next`         | `create-next-app`                                                             |
| `react`        | `create-vite` with its `react-ts` template                                    |
| `react-router` | `create-react-router`                                                         |
| `vue`          | `create-vue`, with TypeScript, Vue Router, Pinia and Vitest                   |
| `nuxt`         | `create-nuxt`, with its `minimal` template                                    |
| `sveltekit`    | `sv create`, with its `minimal` template in TypeScript                        |
| `astro`        | `create-astro`, with its `minimal` template                                   |
| `angular`      | the Angular CLI's `ng new`                                                    |
| `express`      | the Express API of the `express` [template](https://axlers.dev/docs/getting-started/templates.md) |
| `nestjs`       | the Nest CLI's `nest new`                                                     |
| `expo`         | `create-expo-app`                                                             |

The names are the same as the [templates'](https://axlers.dev/docs/getting-started/templates.md), so `--template sveltekit` and `--with sveltekit` start the same app. The tool runs in the folder the app goes in, such as `apps`, through your package manager, such as `pnpm dlx create-vite@latest`, so it's always the latest version. `express` is copied from the templates of the Axle version you run, since Express has no tool of its own that writes a current app. `--dry-run` shows the command or the copy, and writes nothing.

Then the app is linked to the workspace's [config packages](https://axlers.dev/docs/getting-started/templates.md#shared-settings):

- Its name is set, and the lockfiles, workspace files and `.git` folder the tool may have made inside it are removed.
- Every `tsconfig.json` with `compilerOptions` extends `typescript-config`'s `base.json`, ahead of what it already extends.
- It depends on the config packages and gets their files, `lint` and `format` scripts and tools, as a [new package](#new-packages) does.
- When it uses Next.js, Nuxt, SvelteKit, Angular, Expo, Astro or React Router, its config files extend that framework's export of the config package, such as `@repo/eslint-config/svelte-kit`, which Axle adds the first time an app needs it. The export ignores the framework's build output, and holds the rules its starter needs, such as allowing `require()` for Expo's images.
- The `.gitignore` the tool wrote is merged into the repository's own and removed, so there's one `.gitignore`, at the root. Its paths are rewritten to point into the app, and a Prettier `format` script reads it with `--ignore-path`.
- A config file the framework wrote itself, such as `create-next-app`'s `eslint.config.mjs`, is kept, since it holds the framework's rules. Axle says which, so you can extend the shared package there.

Then install, so the package manager links the new app into the workspace.

## Moving a package

```sh
axle generate move @acme/ui --folder libs/ui
```

The `move` generator moves a package's folder, and rewrites every path that pointed into it or, from inside it, out of it, so the repository keeps building:

- the paths in each `tsconfig*.json` and `jsconfig*.json`, such as `references`, `paths` and `extends`, at the root and in every package;
- `file:` and `link:` dependencies and other paths in each `package.json`;
- the paths in the root's and each package's `axle.toml`;
- relative imports, in the package's own source files and in other packages' that reach into it.

Imports by the package's name don't change, since its name stays the same. The new folder must be one the workspace lists, such as `libs/ui` with `libs/*` among its folders, and empty or not there yet. In a terminal, it asks for the package and the folder; anywhere else, give both. `--dry-run` shows every edit as a diff and moves nothing. Then install, so the lockfile follows the move.

## Removing a package

```sh
axle generate remove old-admin
```

The `remove` generator deletes a package's folder, and takes it out of the `references` and `paths` of the `tsconfig*.json` files that point into it. It [refuses](https://axlers.dev/docs/messages/package-has-dependents.md) while another package depends on it, and names them, so nothing is left importing a package that's gone. When `axle.toml` still mentions the package, it says so, for you to take it out there. `--dry-run` shows the edits and removes nothing. Then install, so the lockfile forgets it.

## CI workflows

```sh
axle generate ci --set provider=github
axle generate ci --set provider=gitlab --set tasks="build test"
axle generate ci --set provider=github --set jobs=4
```

The `ci` generator writes a workflow that runs the tasks a change affects, on every pull request and every push to `main`, or on GitLab to the default branch:

| `provider`  | File                         |
| ----------- | ---------------------------- |
| `github`    | `.github/workflows/axle.yml` |
| `gitlab`    | `.gitlab-ci.yml`             |
| `bitbucket` | `bitbucket-pipelines.yml`    |

`tasks` is the tasks to run, separated by spaces. Left out, it's whichever of `build`, `test` and `lint` the repository's packages have, or `build` when they have none.

`jobs` is how many parallel jobs share the tasks, `1` by default. With more, each job runs its [shard](https://axlers.dev/docs/reference/run.md#splitting-a-run-across-ci-jobs) through `AXLE_SHARD`: a matrix on GitHub, `parallel` on GitLab and parallel steps on Bitbucket, each job keeping its own cache.

The workflow:

- fetches the whole history, so [`--affected`](https://axlers.dev/docs/reference/affected.md) can compare with the branch a pull request goes into, or with the commit before a push. When there is no such commit, as on the first push of a branch, after a force push or in a scheduled pipeline, it runs every task instead, from the cache where it can. Bitbucket doesn't say which commit came before a push, so a push to `main` always runs every task;
- sets up Node.js and the repository's package manager, and installs with its lockfile, such as `pnpm install --frozen-lockfile`, `npm ci` or `aube ci`;
- keeps Axle's [local cache](https://axlers.dev/docs/reference/cache.md) between runs with the service's own cache, in `.axle/cache` through `AXLE_CACHE_DIR`;
- runs Axle from the repository when the root `package.json` lists `@axle-rs/axle`, and otherwise installs it first;
- with a [remote cache](https://axlers.dev/docs/reference/remote-cache.md) in `axle.toml`, gives Axle its token and keys, the `AWS_*` keys of a bucket or the `AZURE_STORAGE_KEY` of a container, from the service's secrets. Axle says which secrets to add.

It's a starting point to commit and change: the branch is `main` on GitHub and Bitbucket, and the Node.js version is the latest LTS.
