@svgt/cli 0.1.0 → 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
@@ -65,21 +65,21 @@ cat icon.svg | svgt --framework vue --name ArrowLeft
65
65
 
66
66
  ## Options
67
67
 
68
- | Option | Description |
69
- | -------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
70
- | `-f`, `--framework <name>` | Required. `react`, `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`. |
77
- | `--export-type <type>` | `default` or `directly` (`export const` / `export class`). Defaults to `default`. Fails with any framework other than React 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](../core/README.md#options):
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
83
 
84
84
  ```bash
85
85
  svgt icon.svg -f react --svg-prop aria-hidden=true --svg-prop class --expand-props start
@@ -92,6 +92,8 @@ Files are named after each framework's convention:
92
92
  | Framework | File |
93
93
  | -------------- | ------------------------- |
94
94
  | React | `ArrowLeft.tsx` / `.jsx` |
95
+ | Preact | `ArrowLeft.tsx` / `.jsx` |
96
+ | Solid | `ArrowLeft.tsx` / `.jsx` |
95
97
  | Svelte | `ArrowLeft.svelte` |
96
98
  | Vue | `ArrowLeft.vue` |
97
99
  | Angular | `arrow-left.component.ts` |
package/dist/index.js CHANGED
@@ -1,9 +1,9 @@
1
1
  #!/usr/bin/env node
2
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=[`react`,`web-components`],M={start:`start`,end:`end`,none:!1},N=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},P=e=>{let{framework:t}=e,{label:n,typescriptOption:i}=r[t];if(e.js&&!i)throw Error(`--js has no effect with ${n}.`);if(e.exportType!=="default"&&!j.includes(t))throw Error(`--export-type has no effect with ${n}.`);let a=!e.js,o=N(e.svgProp),s={dimensions:e.dimensions,svgProps:o,expandProps:M[e.expandProps],exportType:e.exportType},c=(e,n)=>{let r={output:{...s,name:n}};return i&&(r.typescript=a),_(e,t,r)};return c(`<svg></svg>`,`SvgIcon`),{framework:t,typescript:a,render:c,details:A(e,o)}},F=/* @__PURE__ */ new Set([`node_modules`,`dist`,`build`]),I=e=>p(e).toLowerCase()===`.svg`,L=e=>e.startsWith(`.`)||F.has(e),R=async e=>{let t=[],n=await l(e,{withFileTypes:!0});for(let r of n){let n=m(e,r.name);r.isDirectory()&&!L(r.name)?t.push(...await R(n)):r.isFile()&&I(r.name)&&t.push(n)}return t.sort()},z=/* @__PURE__ */ new Set([`ExitPromptError`,`AbortPromptError`]),B=e=>e instanceof Error&&z.has(e.name),V=()=>{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)}},H=e=>[...e,[`esc`,`cancel`]].map(([e,t])=>`${y(`bold`,e)} ${y(`dim`,t)}`).join(y(`dim`,` • `)),U=async()=>{let e=await R(`.`);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}=V();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:H}}},{output:process.stderr,signal:t})}finally{n()}},W=async e=>{let t=[];for(let n of e)(await u(n).catch(()=>{throw Error(`Not found: ${n}`)})).isDirectory()?t.push(...await R(n)):t.push(n);return t},G=async()=>{let e=[];for await(let t of process.stdin)e.push(t);return Buffer.concat(e).toString(`utf8`)},K=async e=>{let t=await G();return[{label:`stdin`,name:e??`SvgIcon`,read:()=>Promise.resolve(t)}]},q=(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`)}))},J=async(e,t)=>{if(e.length>0){let n=await W(e);if(n.length===0)throw Error(`No .svg files found.`);return q(n,t)}return process.stdin.isTTY?q(await U(),t):K(t)},Y=e=>o(e).then(()=>!0,()=>!1),X=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 Y(e);return i&&!await r(t)?`skipped`:(await d(e,`${n}\n`),i?`overwrote`:`created`)}},Z=e=>h(process.cwd(),e)||`.`,Q=(e,t)=>`${e} ${t}${e===1?``:`s`}`,$=new t(`svgt`).description(`Transform SVG files into frontend components from the terminal.`).version(`0.1.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`,`
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
4
  Examples:
5
5
  svgt arrow-left.svg -f react
6
6
  svgt icons/ -f svelte -o src/lib/icons
7
7
  svgt icon.svg -f react --svg-prop aria-hidden=true --svg-prop class
8
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}=P(t),c=o.length?` ${C.dim(`(${o.join(`, `)})`)}`:``,l=await J(e,t.name),u=g(t.out);await s(u,{recursive:!0});let d=X(!!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=Z(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=`${Q(f.size,`component`)} written to ${Z(u)}`+(e.length?` (${e.join(`, `)})`:``);p?D.warn(t):D.info(t)}p&&(process.exitCode=1)});process.argv.length<=2&&process.stdin.isTTY&&$.help(),$.parseAsync().catch(e=>{B(e)&&(D.info(`Cancelled.`),process.exit(130)),D.fail(e)});export{};
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,6 @@
1
1
  {
2
2
  "name": "@svgt/cli",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Transform SVG files into frontend components from the terminal.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -28,7 +28,7 @@
28
28
  "dependencies": {
29
29
  "@inquirer/checkbox": "5.3.1",
30
30
  "commander": "15.1.0",
31
- "@svgt/core": "0.1.0"
31
+ "@svgt/core": "0.2.0"
32
32
  },
33
33
  "devDependencies": {
34
34
  "@types/node": "24.19.2",