# Clone a generator to change the code it writes

To change the code that a Skmtc generator writes, clone the generator. The
`skmtc clone` command copies the source of a published generator into your
project. You then edit that copy, and `skmtc generate` runs it:

```sh
skmtc clone petstore -g @skmtc/gen-zod
```

This page is for a developer, or a coding agent, who generates code with the
Skmtc CLI and wants different output. It gives the steps from a new project to
a changed file, and the errors that `skmtc clone` can give.

A generator is a package that writes code from an OpenAPI document. Do not
edit a generated file: the next `skmtc generate` writes it again. Change the
generator.

## Before you start [#before-you-start]

- Install the Skmtc CLI, version 0.17.0 or later. On macOS or Linux, run
  `curl -fsSL https://skmtc.dev/install | sh`. On Windows, in PowerShell, run
  `irm https://skmtc.dev/install.ps1 | iex`. `skmtc --version` prints the
  version.
- Have a project that generates code. If you do not have one, these three
  commands make a project named `petstore` that writes Zod schemas to
  `src/generated`:

  ```sh
  skmtc init petstore src/generated
  skmtc install @skmtc/gen-zod petstore
  skmtc generate petstore https://petstore3.swagger.io/api/v3/openapi.json --save
  ```

- Check if a setting does the change. Some generators take settings for one
  operation or one model, which Skmtc calls enrichments. If a setting does what
  you want, use it and do not clone. See
  [Configure enrichments](https://github.com/skmtc/skmtc/blob/main/deno/docs/using/how-to/configure-enrichments.md).

The examples on this page use the `petstore` project and the
`@skmtc/gen-zod` generator. Replace these two names:

- `petstore`: the name of your project.
- `@skmtc/gen-zod`: the generator that writes the code you want to change.

## Step 1: Clone the generator [#step-1-clone-the-generator]

- In the folder that has `.skmtc/`, clone the generator:

  ```sh
  skmtc clone petstore -g @skmtc/gen-zod
  ```

  The command prints the version that it cloned:

  ```
  Cloned 1 generator(s) into "petstore":
    - @skmtc/gen-zod@0.6.12
  ```

The source is now in `.skmtc/petstore/gen-zod/`. The file
`.skmtc/petstore/deno.json` points the generator at that folder, so
`skmtc generate` runs your copy and not the published package. These are the
two entries that point at the clone. The command also adds each package that
the generator imports, such as `valibot`, and the file has other entries too,
such as `@skmtc/core`. Do not remove them.

```json
{
  "imports": {
    "@skmtc/gen-zod": "./gen-zod/mod.ts"
  },
  "workspace": ["./gen-zod"]
}
```

To clone more than one generator, repeat `-g`. To clone one version, add it to
the name: `-g @skmtc/gen-zod@0.6.12`. For JSON output, add `--json`.

## Step 2: Edit the source [#step-2-edit-the-source]

Open `.skmtc/petstore/gen-zod/src/`. Two files hold most changes:

| File | What it decides |
| --- | --- |
| `src/base.ts` | The identifier of each definition, and the export path of its file. |
| `src/<Name>.ts`, one for each schema type | The code for that type, such as `ZodObject.ts` for an object. |

As an example, change the folder of the generated files from `types` to
`schemas`.

1. Open `.skmtc/petstore/gen-zod/src/base.ts` and find `toExportPath`:

   ```ts
   return `@/types/${decapitalize(name)}.generated.ts`;
   ```

2. Change `types` to `schemas`:

   ```ts
   return `@/schemas/${decapitalize(name)}.generated.ts`;
   ```

In a path, `@/` is the base path of the project: `src/generated` here.

## Step 3: Generate again [#step-3-generate-again]

- Run the generator:

  ```sh
  skmtc generate petstore
  ```

  The files are now in the new folder, and Skmtc deleted the files that it
  wrote to the old one:

  ```
  src/generated/schemas/pet.generated.ts
  src/generated/schemas/user.generated.ts
  ```

To run the generator each time you save a file of the clone, run
`skmtc dev petstore` while you edit.

Commit the folder `.skmtc/petstore/gen-zod/` to git. It is your code now.

## Get a later version of the generator [#get-a-later-version-of-the-generator]

A clone does not change when its generator gets a new published version. To
replace the clone with the newest version, add `--force`:

```sh
skmtc clone petstore -g @skmtc/gen-zod --force
```

`--force` deletes the folder and your edits, and clones again. Commit your
edits first. After the command, apply them again from `git diff`.

To go back to the published generator with no edits, remove the clone and
install the package:

```sh
skmtc remove petstore @skmtc/gen-zod
skmtc install @skmtc/gen-zod petstore
```

`skmtc remove` deletes the folder of the clone and your edits.

## Troubleshooting [#troubleshooting]

`skmtc clone` changes no file when it stops with one of these errors.

| Output | Exit code | Cause | Fix |
| --- | --- | --- | --- |
| `Error: already cloned` | 2 | The project has a clone of this generator. | Edit the clone you have. Or add `--force` to replace it, which deletes your edits. |
| `Error: folder in use` | 2 | The folder `.skmtc/<project>/<name>/` holds another generator, or files that are not a generator. | Move or delete that folder. `--force` does not replace it. |
| `Error: @skmtc/core conflict` | 2 | The generators in the project need versions of `@skmtc/core` that no one version satisfies. One project runs one version. | The message names each generator and the version it needs. Clone or install versions that agree. To move more than one clone, name each one in one `clone --force` command. |
| `Error: package version conflict` | 2 | Two generators in the project need versions of a package that no one version satisfies. One project has one version of each package. | The message names the package, each version and the generator that needs it. Clone versions of the generators that agree. |

## Clone a generator from an agent [#clone-a-generator-from-an-agent]

A coding agent runs the same commands. Give it these rules:

- Run `skmtc agent-context --json` first. It lists the projects and the
  commands of the installed CLI.
- Run `skmtc guide customize` for the steps that match the installed version.
- Add `--json` to `clone`, `install`, `remove` and `generate`, and read the
  exit code. Exit code 2 means that the command changed nothing and the
  message says what to do. `skmtc dev` takes no options.
- Edit the files in `.skmtc/<project>/<name>/src/`, and no generated file.
- Never add `--force` without a question to the user: it deletes edits.

## See also [#see-also]

- [`skmtc clone` reference](https://github.com/skmtc/skmtc/blob/main/deno/docs/reference/cli/clone.md):
  each option and each rule of the command.
- [Clone or install](https://github.com/skmtc/skmtc/blob/main/deno/docs/concepts/clone-vs-install.md):
  when to clone, and what you take on when you do.
- [Open a pull request with generated code](/docs/pull-requests): get the
  generated code of an API from Skmtc Hub as a GitHub pull request.
