@svgt/cli 0.0.0-stage → 0.1.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/README.md +105 -2
- package/dist/index.js +9 -0
- package/package.json +43 -3
package/README.md
CHANGED
|
@@ -1,3 +1,106 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @svgt/cli
|
|
2
2
|
|
|
3
|
-
|
|
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`, `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):
|
|
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
|
+
| Svelte | `ArrowLeft.svelte` |
|
|
96
|
+
| Vue | `ArrowLeft.vue` |
|
|
97
|
+
| Angular | `arrow-left.component.ts` |
|
|
98
|
+
| Astro | `ArrowLeft.astro` |
|
|
99
|
+
| Web Components | `arrow-left.ts` / `.js` |
|
|
100
|
+
|
|
101
|
+
- 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`.
|
|
102
|
+
- 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.
|
|
103
|
+
|
|
104
|
+
## License
|
|
105
|
+
|
|
106
|
+
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=[`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`,`
|
|
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}=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{};
|
package/package.json
CHANGED
|
@@ -1,6 +1,46 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@svgt/cli",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.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.1.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
|
}
|