# @svgt/cli

A CLI to transform SVG files into frontend components.

## Installation

> It requires Node.js v22.13 or later.

Run it without installing:

```bash
npx @svgt/cli arrow-left.svg --framework react
```

Or add it globally to your system:

```bash
npm i -g @svgt/cli
```

## Usage

```bash
svgt <files or folders...> --framework <framework> [options]
cat icon.svg | svgt --framework <framework> [options]
```

- Convert a single SVG file. This writes `ArrowLeft.tsx` to the current folder:

```bash
svgt arrow-left.svg --framework react
```

- Write it to another folder:

```bash
svgt arrow-left.svg --framework react --out src/components
```

- Convert every SVG file in a folder, including its subfolders. Hidden folders, `node_modules`, `dist` and `build` are skipped:

```bash
svgt icons/ --framework svelte --out src/lib/icons
```

- Pick the files from a list. When you pass no files or folders and stdin is a terminal, the CLI lists every SVG under the current folder:

```bash
svgt --framework react
```

| Key              | Action                 |
| ---------------- | ---------------------- |
| <kbd>Space</kbd> | Select or clear a file |
| <kbd>a</kbd>     | Select all             |
| <kbd>i</kbd>     | Invert the selection   |
| <kbd>Enter</kbd> | Convert the selection  |
| <kbd>Esc</kbd>   | Cancel                 |

- Read the SVG from stdin. The component is named `SvgIcon` unless you pass `--name`:

```bash
cat icon.svg | svgt --framework vue --name ArrowLeft
```

## Options

| Option                     | Description                                                                                                                                     |
| -------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
| `-f`, `--framework <name>` | Required. `react`, `svelte`, `vue`, `angular`, `astro` or `web-components`.                                                                     |
| `-o`, `--out <folder>`     | Folder to write the components to. Defaults to the current folder.                                                                              |
| `-n`, `--name <name>`      | Component name, for a single SVG only. Defaults to the file name in PascalCase, or `SvgIcon` from stdin.                                        |
| `--js`                     | Output JavaScript instead of TypeScript. Fails with Vue and Angular, which have no TypeScript option.                                           |
| `--no-dimensions`          | Remove `width` and `height` from the root `<svg>`.                                                                                              |
| `--svg-prop <name=value>`  | Set an attribute on the root `<svg>`. Repeat it for more attributes. A bare `name` removes the attribute.                                       |
| `--expand-props <pos>`     | Where to spread the props on the root `<svg>`: `start`, `end` or `none`. Defaults to `end`.                                                     |
| `--export-type <type>`     | `default` or `directly` (`export const` / `export class`). Defaults to `default`. Fails with any framework other than React and Web Components. |
| `--force`                  | Overwrite existing files without asking.                                                                                                        |
| `-h`, `--help`             | Show the help.                                                                                                                                  |
| `-v`, `--version`          | Show the version.                                                                                                                               |

Each option maps to a `transform()` option, documented in the [`@svgt/core` README](../core/README.md#options):

```bash
svgt icon.svg -f react --svg-prop aria-hidden=true --svg-prop class --expand-props start
```

## Output files

Files are named after each framework's convention:

| Framework      | File                      |
| -------------- | ------------------------- |
| React          | `ArrowLeft.tsx` / `.jsx`  |
| Svelte         | `ArrowLeft.svelte`        |
| Vue            | `ArrowLeft.vue`           |
| Angular        | `arrow-left.component.ts` |
| Astro          | `ArrowLeft.astro`         |
| Web Components | `arrow-left.ts` / `.js`   |

- When a file already exists, the CLI asks before overwriting it: `y` for yes, `n` for no (the default when you press <kbd>Enter</kbd>), `a` to overwrite it and every file after it. Without a terminal to ask in, or when the SVG comes from stdin, existing files are skipped unless you pass `--force`.
- If an SVG fails to parse, that SVG is skipped. If two SVGs would write the same file, such as `arrow-left.svg` and `ArrowLeft.svg`, the first is written and the second is skipped. In both cases the other SVGs are still written and the command exits with code 1.

## License

MIT
