# Codemods

> Learn how to use automatic migration scripts (codemods) to replace Component Library with Cimpress UI in your application.

Source: https://ui.cimpress.io/cl-migration/codemods/

:::danger[Important!]
Migrating from Component Library to Cimpress UI is a complex task. The codemods described on this page are provided on
a "best effort" basis, with the assumption that your project already follows Cimpress UI usage guidelines. If that is
not the case, migrate manually.
:::

To ease the burden on developers and speed up the migration process, Cimpress UI provides automated migration scripts.

You can use automated migration scripts in your project if:

- you're using Node >= 20
- your project is currently using Component Library >= 16
- your project uses an automated code formatting tool (like [Prettier](https://prettier.io/))

Migration scripts (transforms) are provided within the `@cimpress-ui/codemod` package. Transforms are named according to the following rules:

- Transform name consists of a version identifier, and optionally a slash separator and a transform identifier.
- Version identifier states the version of Cimpress UI that the transform migrates to.
- Transform identifier describes what the transform is migrating.
- Transforms that have names consisting of only a version identifier are group transforms - they execute all available transforms for that version.

See the [`@cimpress-ui/codemod` readme](https://gitlab.com/Cimpress-Technology/internal-open-source/component-library/cimpress-ui/-/tree/main/packages/codemod/README.md#available-transforms) for a list of available transforms.

To execute a transform, run the following command in a terminal:

**npm**

```sh
npx @cimpress-ui/codemod [options] <transform> <paths>
```

**pnpm**

```sh
pnpm dlx @cimpress-ui/codemod [options] <transform> <paths>
```

**Yarn**

```sh
yarn dlx @cimpress-ui/codemod [options] <transform> <paths>
```

To see all available options, run the command with the `--help` flag:

**npm**

```sh
npx @cimpress-ui/codemod --help
```

**pnpm**

```sh
pnpm dlx @cimpress-ui/codemod --help
```

**Yarn**

```sh
yarn dlx @cimpress-ui/codemod --help
```

## Examples

To run the `v1` transform on all supported files in the current directory and all its subdirectories:

**npm**

```sh
npx @cimpress-ui/codemod v1 .
```

**pnpm**

```sh
pnpm dlx @cimpress-ui/codemod v1 .
```

**Yarn**

```sh
yarn dlx @cimpress-ui/codemod v1 .
```

To run the `v1/demo` transform on all supported files in `src/components`, `src/utils`, and all their subdirectories:

**npm**

```sh
npx @cimpress-ui/codemod v1/demo src/components src/utils
```

**pnpm**

```sh
pnpm dlx @cimpress-ui/codemod v1/demo src/components src/utils
```

**Yarn**

```sh
yarn dlx @cimpress-ui/codemod v1/demo src/components src/utils
```

To run the `v1` transform on `src/components/MyCustomComponent.tsx`:

**npm**

```sh
npx @cimpress-ui/codemod v1 src/components/MyCustomComponent.tsx
```

**pnpm**

```sh
pnpm dlx @cimpress-ui/codemod v1 src/components/MyCustomComponent.tsx
```

**Yarn**

```sh
yarn dlx @cimpress-ui/codemod v1 src/components/MyCustomComponent.tsx
```

## Limitations

While we try to provide complete migration scripts, in some cases it won't be possible to fully migrate your code automatically. Make sure that your application builds and runs correctly after applying transforms.

### Common unsupported scenarios

Listed below are the most common cases which our migration scripts cannot handle:

- prop values stored in variables

  ```tsx
  // Example:
  // Button size "default" is renamed to "medium"

  // This will be migrated
  <Button size="default">My button</Button>;

  // This won't be migrated
  const buttonSize = 'default';
  <Button size={buttonSize}>My button</Button>;
  ```

- event handler parameters

  ```tsx
  // Example:
  // An event handler signature is changed from `({ isSelected }) => void` to `(isSelected) => void`

  // This won't be migrated
  <Checkbox onChange={onCheckboxChange}>My checkbox</Checkbox>
  <Checkbox onChange={({ isSelected }) => setIsSelected(isSelected)}>My checkbox</Checkbox>
  ```

- changes that require knowledge about the structure of data

  ```tsx
  // Example:
  // `Select` component now requires `selectedKey` prop of type `string`
  // instead of a `selectedOption` prop of type `object`

  // This won't be migrated, because we don't know the structure of `options`
  // and we don't know which property to use as the key
  <Select options={options} selectedOption={selectedOption} />
  ```

- changes that require application-specific context

  ```tsx
  // Example:
  // `Tag` components must now be wrapped with `TagGroup`

  // This won't be migrated, because we don't know which tags should be grouped together
  <Tag>Apple</Tag>
  <Tag>Banana</Tag>
  <Tag>Cherry</Tag>
  <Tag>Broccoli</Tag>
  <Tag>Carrot</Tag>
  ```

### Code formatting

This tool may cause formatting loss when applying transforms. Make sure to run your project's formatter on all transformed files.
