@svgt/cli 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Pablo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,108 @@
1
- # Temporary Holding Version
1
+ # @svgt/cli
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A CLI to transform SVG files into frontend components.
4
+
5
+ ## Installation
6
+
7
+ > It requires Node.js v22.13 or later.
8
+
9
+ Run it without installing:
10
+
11
+ ```bash
12
+ npx @svgt/cli arrow-left.svg --framework react
13
+ ```
14
+
15
+ Or add it globally to your system:
16
+
17
+ ```bash
18
+ npm i -g @svgt/cli
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ ```bash
24
+ svgt <files or folders...> --framework <framework> [options]
25
+ cat icon.svg | svgt --framework <framework> [options]
26
+ ```
27
+
28
+ - Convert a single SVG file. This writes `ArrowLeft.tsx` to the current folder:
29
+
30
+ ```bash
31
+ svgt arrow-left.svg --framework react
32
+ ```
33
+
34
+ - Write it to another folder:
35
+
36
+ ```bash
37
+ svgt arrow-left.svg --framework react --out src/components
38
+ ```
39
+
40
+ - Convert every SVG file in a folder, including its subfolders. Hidden folders, `node_modules`, `dist` and `build` are skipped:
41
+
42
+ ```bash
43
+ svgt icons/ --framework svelte --out src/lib/icons
44
+ ```
45
+
46
+ - 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:
47
+
48
+ ```bash
49
+ svgt --framework react
50
+ ```
51
+
52
+ | Key | Action |
53
+ | ---------------- | ---------------------- |
54
+ | <kbd>Space</kbd> | Select or clear a file |
55
+ | <kbd>a</kbd> | Select all |
56
+ | <kbd>i</kbd> | Invert the selection |
57
+ | <kbd>Enter</kbd> | Convert the selection |
58
+ | <kbd>Esc</kbd> | Cancel |
59
+
60
+ - Read the SVG from stdin. The component is named `SvgIcon` unless you pass `--name`:
61
+
62
+ ```bash
63
+ cat icon.svg | svgt --framework vue --name ArrowLeft
64
+ ```
65
+
66
+ ## Options
67
+
68
+ | Option | Description |
69
+ | -------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
70
+ | `-f`, `--framework <name>` | Required. `react`, `preact`, `solid`, `svelte`, `vue`, `angular`, `astro` or `web-components`. |
71
+ | `-o`, `--out <folder>` | Folder to write the components to. Defaults to the current folder. |
72
+ | `-n`, `--name <name>` | Component name, for a single SVG only. Defaults to the file name in PascalCase, or `SvgIcon` from stdin. |
73
+ | `--js` | Output JavaScript instead of TypeScript. Fails with Vue and Angular, which have no TypeScript option. |
74
+ | `--no-dimensions` | Remove `width` and `height` from the root `<svg>`. |
75
+ | `--svg-prop <name=value>` | Set an attribute on the root `<svg>`. Repeat it for more attributes. A bare `name` removes the attribute. |
76
+ | `--expand-props <pos>` | Where to spread the props on the root `<svg>`: `start`, `end` or `none`. Defaults to `end`. Fails with any framework other than React, Preact, Solid, Svelte and Astro. |
77
+ | `--export-type <type>` | `default` or `directly` (`export const` / `export class`). Defaults to `default`. Fails with any framework other than React, Preact, Solid and Web Components. |
78
+ | `--force` | Overwrite existing files without asking. |
79
+ | `-h`, `--help` | Show the help. |
80
+ | `-v`, `--version` | Show the version. |
81
+
82
+ Each option maps to a `transform()` option, documented in the [`@svgt/core` README](https://github.com/pheralb/svgt/tree/main/packages/core#options). This command adds `aria-hidden="true"` to the root `<svg>`, removes its `class`, and spreads the props first, so the SVG's own attributes win over the ones passed to the component:
83
+
84
+ ```bash
85
+ svgt icon.svg -f react --svg-prop aria-hidden=true --svg-prop class --expand-props start
86
+ ```
87
+
88
+ ## Output files
89
+
90
+ Files are named after each framework's convention:
91
+
92
+ | Framework | File |
93
+ | -------------- | ------------------------- |
94
+ | React | `ArrowLeft.tsx` / `.jsx` |
95
+ | Preact | `ArrowLeft.tsx` / `.jsx` |
96
+ | Solid | `ArrowLeft.tsx` / `.jsx` |
97
+ | Svelte | `ArrowLeft.svelte` |
98
+ | Vue | `ArrowLeft.vue` |
99
+ | Angular | `arrow-left.component.ts` |
100
+ | Astro | `ArrowLeft.astro` |
101
+ | Web Components | `arrow-left.ts` / `.js` |
102
+
103
+ - 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`.
104
+ - 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.
105
+
106
+ ## License
107
+
108
+ MIT
package/dist/index.js ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env node
2
+ import{createInterface as e}from"node:readline/promises";import{Command as t,Option as n}from"commander";import{FRAMEWORKS as r,FRAMEWORK_IDS as i,fileName as a}from"@svgt/core/frameworks";import{access as o,mkdir as s,readFile as c,readdir as l,stat as u,writeFile as d}from"node:fs/promises";import{basename as f,extname as p,join as m,relative as h,resolve as g}from"node:path";import{transform as _}from"@svgt/core";import v from"@inquirer/checkbox";import{styleText as y}from"node:util";const b=`[svgt]`,x=(()=>{let{env:e}=process;return`NO_COLOR`in e&&e.NO_COLOR!==``?!1:`FORCE_COLOR`in e?e.FORCE_COLOR!==`0`:!!process.stderr.isTTY&&e.TERM!==`dumb`})(),S=(e,t)=>n=>x?`\x1b[${e}m${n}\x1b[${t}m`:n,C={bold:S(1,22),dim:S(2,22),red:S(31,39),green:S(32,39),yellow:S(33,39),cyan:S(36,39)},w=e=>e.startsWith(`${b} `)?e.slice(7):e,T=e=>w(e instanceof Error?e.message:String(e)),E=(e,t,n)=>{process.stderr.write(`${C.dim(b)} ${t(C.bold(e))} ${n}\n`)},D={info:e=>E(`info`,C.cyan,e),success:e=>E(`done`,C.green,e),warn:e=>E(`warn`,C.yellow,e),error:(e,t)=>E(`error`,C.red,t?`${C.bold(t)}: ${T(e)}`:T(e)),fail:e=>{D.error(e),process.exit(1)},ask:async t=>{let n=e({input:process.stdin,output:process.stderr});n.once(`SIGINT`,()=>{process.stderr.write(`
3
+ `),process.exit(130)});try{return(await n.question(`${C.dim(b)} ${C.cyan(C.bold(`?`))} ${t} `)).trim().toLowerCase()}finally{n.close()}}},O=(e,t)=>[...t,e],k=(e={})=>Object.entries(e).map(([e,t])=>t===null?`${e} removed`:`${e}="${t}"`),A=(e,t)=>[...k(t),...e.js?[`JavaScript`]:[],...e.dimensions?[]:[`no dimensions`],...e.expandProps===`start`?[`props at start`]:[],...e.expandProps===`none`?[`no props`]:[],...e.exportType===`directly`?[`export directly`]:[]],j={start:`start`,end:`end`,none:!1},M=e=>{if(e.length===0)return;let t={};for(let n of e){let e=n.indexOf(`=`),r=(e===-1?n:n.slice(0,e)).trim();t[r]=e===-1?null:n.slice(e+1)}return t},N=e=>{let{framework:t}=e,{label:n,typescriptOption:i,exportTypeOption:a,expandPropsOption:o}=r[t];if(e.js&&!i)throw Error(`--js has no effect with ${n}.`);if(e.exportType!=="default"&&!a)throw Error(`--export-type has no effect with ${n}.`);if(e.expandProps!==`end`&&!o)throw Error(`--expand-props has no effect with ${n}.`);let s=!e.js,c=M(e.svgProp),l={dimensions:e.dimensions,svgProps:c,expandProps:j[e.expandProps],exportType:e.exportType},u=(e,n)=>{let r={output:{...l,name:n}};return i&&(r.typescript=s),_(e,t,r)};return u(`<svg></svg>`,`SvgIcon`),{framework:t,typescript:s,render:u,details:A(e,c)}},P=/* @__PURE__ */ new Set([`node_modules`,`dist`,`build`]),F=e=>p(e).toLowerCase()===`.svg`,I=e=>e.startsWith(`.`)||P.has(e),L=async e=>{let t=[],n=await l(e,{withFileTypes:!0});for(let r of n){let n=m(e,r.name);r.isDirectory()&&!I(r.name)?t.push(...await L(n)):r.isFile()&&F(r.name)&&t.push(n)}return t.sort()},R=/* @__PURE__ */ new Set([`ExitPromptError`,`AbortPromptError`]),z=e=>e instanceof Error&&R.has(e.name),B=()=>{let e=new AbortController,t=(t,n)=>{n?.name===`escape`&&e.abort()};return process.stdin.on(`keypress`,t),{signal:e.signal,dispose:()=>process.stdin.off(`keypress`,t)}},V=e=>[...e,[`esc`,`cancel`]].map(([e,t])=>`${y(`bold`,e)} ${y(`dim`,t)}`).join(y(`dim`,` • `)),H=async()=>{let e=await L(`.`);if(e.length===0)throw Error(`No .svg files found in this folder. Pass a file or folder to convert.`);let{signal:t,dispose:n}=B();try{return await v({message:`Which SVG files do you want to convert?`,choices:e.map(e=>({value:e,name:h(`.`,e)})),pageSize:15,required:!0,theme:{style:{keysHelpTip:V}}},{output:process.stderr,signal:t})}finally{n()}},U=async e=>{let t=[];for(let n of e)(await u(n).catch(()=>{throw Error(`Not found: ${n}`)})).isDirectory()?t.push(...await L(n)):t.push(n);return t},W=async()=>{let e=[];for await(let t of process.stdin)e.push(t);return Buffer.concat(e).toString(`utf8`)},G=async e=>{let t=await W();return[{label:`stdin`,name:e??`SvgIcon`,read:()=>Promise.resolve(t)}]},K=(e,t)=>{if(t&&e.length>1)throw Error(`--name can only be used with a single input.`);return e.map(e=>({label:e,name:t??f(e),read:()=>c(e,`utf8`)}))},q=async(e,t)=>{if(e.length>0){let n=await U(e);if(n.length===0)throw Error(`No .svg files found.`);return K(n,t)}return process.stdin.isTTY?K(await H(),t):G(t)},J=e=>o(e).then(()=>!0,()=>!1),Y=e=>{let t=!!(process.stdin.isTTY&&process.stderr.isTTY),n=e,r=async e=>{if(n)return!0;if(!t)return D.warn(`Skipped ${e}: it already exists. Use --force to overwrite it.`),!1;let r=await D.ask(`${e} already exists. Overwrite it? (y)es, (n)o, (a)ll [n]`);return(r===`a`||r===`all`)&&(n=!0),n||r===`y`||r===`yes`};return async(e,t,n)=>{let i=await J(e);return i&&!await r(t)?`skipped`:(await d(e,`${n}\n`),i?`overwrote`:`created`)}},X=e=>h(process.cwd(),e)||`.`,Z=(e,t)=>`${e} ${t}${e===1?``:`s`}`,Q=new t(`svgt`).description(`Transform SVG files into frontend components from the terminal.`).version(`0.2.0`,`-v, --version`,`Show the version.`).helpOption(`-h, --help`,`Show this help.`).argument(`[inputs...]`,`SVG files or folders. Without them, reads stdin or lets you pick files.`).addOption(new n(`-f, --framework <name>`,`Framework to output.`).choices(i).makeOptionMandatory()).option(`-o, --out <folder>`,`Folder to write the components to.`,`.`).option(`-n, --name <name>`,`Component name, for a single input. Defaults to the file name in PascalCase.`).option(`--js`,`Output JavaScript instead of TypeScript.`).option(`--no-dimensions`,`Remove width and height from the root <svg>.`).option(`--svg-prop <name=value>`,`Set an attribute on the root <svg>. Repeatable. Without "=value", removes it.`,O,[]).addOption(new n(`--expand-props <position>`,`Where to spread the props on the root <svg>.`).choices([`start`,`end`,`none`]).default(`end`)).addOption(new n(`--export-type <type>`,`How to export the component. React and Web Components only.`).choices([`default`,`directly`]).default(`default`)).option(`--force`,`Overwrite existing files without asking.`).addHelpText(`after`,`
4
+ Examples:
5
+ svgt arrow-left.svg -f react
6
+ svgt icons/ -f svelte -o src/lib/icons
7
+ svgt icon.svg -f react --svg-prop aria-hidden=true --svg-prop class
8
+ cat icon.svg | svgt -f vue -n ArrowLeft
9
+ `).showHelpAfterError(`(run svgt --help for usage)`).action(async(e,t)=>{let{framework:n,typescript:r,render:i,details:o}=N(t),c=o.length?` ${C.dim(`(${o.join(`, `)})`)}`:``,l=await q(e,t.name),u=g(t.out);await s(u,{recursive:!0});let d=Y(!!t.force),f=/* @__PURE__ */ new Map,p=0,h=0;for(let e of l){let t=m(u,a(n,e.name,r)),o=X(t),s=f.get(t);if(s)D.warn(`Skipped ${e.label}: same output as ${s}`),p++;else try{let n=await d(t,o,i(await e.read(),e.name));if(n===`skipped`){h++;continue}f.set(t,e.label),D.success(`${n===`overwrote`?`Overwrote`:`Created`} ${o}${c}`)}catch(t){D.error(t,e.label),p++}}if(l.length>1){let e=[p&&`${p} failed`,h&&`${h} skipped`].filter(Boolean),t=`${Z(f.size,`component`)} written to ${X(u)}`+(e.length?` (${e.join(`, `)})`:``);p?D.warn(t):D.info(t)}p&&(process.exitCode=1)});process.argv.length<=2&&process.stdin.isTTY&&Q.help(),Q.parseAsync().catch(e=>{z(e)&&(D.info(`Cancelled.`),process.exit(130)),D.fail(e)});export{};
package/package.json CHANGED
@@ -1,6 +1,46 @@
1
1
  {
2
2
  "name": "@svgt/cli",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.2.0",
4
+ "description": "Transform SVG files into frontend components from the terminal.",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/pheralb/svgt.git",
10
+ "directory": "packages/cli"
11
+ },
12
+ "homepage": "https://github.com/pheralb/svgt#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/pheralb/svgt/issues"
15
+ },
16
+ "files": [
17
+ "dist"
18
+ ],
19
+ "bin": {
20
+ "svgt": "./dist/index.js"
21
+ },
22
+ "keywords": [
23
+ "svg",
24
+ "cli",
25
+ "component",
26
+ "frontend"
27
+ ],
28
+ "dependencies": {
29
+ "@inquirer/checkbox": "5.3.1",
30
+ "commander": "15.1.0",
31
+ "@svgt/core": "0.2.0"
32
+ },
33
+ "devDependencies": {
34
+ "@types/node": "24.19.2",
35
+ "tsdown": "0.23.7",
36
+ "typescript": "6.0.3"
37
+ },
38
+ "engines": {
39
+ "node": ">=22.13"
40
+ },
41
+ "scripts": {
42
+ "build": "tsdown",
43
+ "dev": "tsdown --watch --no-clean",
44
+ "check": "tsc --noEmit"
45
+ }
6
46
  }