Skip to content

Commit d800319

Browse files
committed
feat(cli): allow multiple values in "--svg-props" and "--replace-attr-values"
Closes #233
1 parent 4a7c7aa commit d800319

File tree

5 files changed

+36
-21
lines changed

5 files changed

+36
-21
lines changed

packages/cli/src/__snapshots__/index.test.js.snap

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -346,12 +346,12 @@ export default SvgFile
346346
"
347347
`;
348348

349-
exports[`cli should support various args: --svg-props "hidden={true}" 1`] = `
349+
exports[`cli should support various args: --svg-props "hidden={true},id=hello" 1`] = `
350350
"import React from 'react'
351351
352352
function SvgFile(props) {
353353
return (
354-
<svg width={48} height={1} hidden={true} {...props}>
354+
<svg width={48} height={1} hidden={true} id=\\"hello\\" {...props}>
355355
<path d=\\"M0 0h48v1H0z\\" fill=\\"#063855\\" fillRule=\\"evenodd\\" />
356356
</svg>
357357
)
@@ -382,20 +382,20 @@ export default SvgFile
382382
exports[`cli should suppress output when transforming a directory with a --silent option 1`] = `""`;
383383

384384
exports[`cli should transform a whole directory and output relative destination paths 1`] = `
385-
"[37m[39m
386-
[37m[39m[37m__fixtures__/cased/camelCase.svg -> __fixtures_build__/whole/cased/CamelCase.js[39m
387-
[37m[39m[37m__fixtures__/cased/kebab-case.svg -> __fixtures_build__/whole/cased/KebabCase.js[39m
388-
[37m[39m[37m__fixtures__/cased/multiple---dashes.svg -> __fixtures_build__/whole/cased/MultipleDashes.js[39m
389-
[37m[39m[37m__fixtures__/complex/skype.svg -> __fixtures_build__/whole/complex/Skype.js[39m
390-
[37m[39m[37m__fixtures__/complex/telegram.svg -> __fixtures_build__/whole/complex/Telegram.js[39m
391-
[37m[39m[37m__fixtures__/nesting/a/c/three.svg -> __fixtures_build__/whole/nesting/a/c/Three.js[39m
392-
[37m[39m[37m__fixtures__/nesting/a/two.svg -> __fixtures_build__/whole/nesting/a/Two.js[39m
393-
[37m[39m[37m__fixtures__/nesting/one.svg -> __fixtures_build__/whole/nesting/One.js[39m
394-
[37m[39m[37m__fixtures__/simple/file.svg -> __fixtures_build__/whole/simple/File.js[39m
395-
[37m[39m[37m__fixtures__/withPrettierRc/file.svg -> __fixtures_build__/whole/withPrettierRc/File.js[39m
396-
[37m[39m[37m__fixtures__/withSvgoYml/file.svg -> __fixtures_build__/whole/withSvgoYml/File.js[39m
397-
[37m[39m[37m__fixtures__/withSvgrRc/file.svg -> __fixtures_build__/whole/withSvgrRc/File.js[39m
398-
[37m__fixtures__/cased/PascalCase.svg -> __fixtures_build__/whole/cased/PascalCase.js[39m"
385+
"
386+
__fixtures__/cased/PascalCase.svg -> __fixtures_build__/whole/cased/PascalCase.js
387+
__fixtures__/cased/camelCase.svg -> __fixtures_build__/whole/cased/CamelCase.js
388+
__fixtures__/cased/kebab-case.svg -> __fixtures_build__/whole/cased/KebabCase.js
389+
__fixtures__/cased/multiple---dashes.svg -> __fixtures_build__/whole/cased/MultipleDashes.js
390+
__fixtures__/complex/skype.svg -> __fixtures_build__/whole/complex/Skype.js
391+
__fixtures__/complex/telegram.svg -> __fixtures_build__/whole/complex/Telegram.js
392+
__fixtures__/nesting/a/c/three.svg -> __fixtures_build__/whole/nesting/a/c/Three.js
393+
__fixtures__/nesting/a/two.svg -> __fixtures_build__/whole/nesting/a/Two.js
394+
__fixtures__/nesting/one.svg -> __fixtures_build__/whole/nesting/One.js
395+
__fixtures__/simple/file.svg -> __fixtures_build__/whole/simple/File.js
396+
__fixtures__/withPrettierRc/file.svg -> __fixtures_build__/whole/withPrettierRc/File.js
397+
__fixtures__/withSvgoYml/file.svg -> __fixtures_build__/whole/withSvgoYml/File.js
398+
__fixtures__/withSvgrRc/file.svg -> __fixtures_build__/whole/withSvgrRc/File.js"
399399
`;
400400

401401
exports[`cli should work with a simple file 1`] = `

packages/cli/src/index.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@ import fileCommand from './fileCommand'
88
import dirCommand from './dirCommand'
99
import { stat, exitError } from './util'
1010

11-
const parseObject = (arg, accumulation = {}) => {
11+
function parseObject(arg, accumulation = {}) {
1212
const [name, value] = arg.split('=')
1313
return { ...accumulation, [name]: value }
1414
}
1515

16-
const isFile = filePath => {
16+
function parseObjectList(arg, accumulation = {}) {
17+
const args = arg.split(',').map(str => str.trim())
18+
return args.reduce((acc, arg) => parseObject(arg, acc), accumulation)
19+
}
20+
21+
function isFile(filePath) {
1722
try {
1823
const stats = fs.statSync(filePath)
1924
return stats.isFile()
@@ -61,12 +66,12 @@ program
6166
.option(
6267
'--svg-props <property=value>',
6368
'add props to the svg element',
64-
parseObject,
69+
parseObjectList,
6570
)
6671
.option(
6772
'--replace-attr-values <old=new>',
6873
'replace an attribute value',
69-
parseObject,
74+
parseObjectList,
7075
)
7176
.option('--template <file>', 'specify a custom template to use')
7277
.option('--title-prop', 'create a title element linked with props')

packages/cli/src/index.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ describe('cli', () => {
107107
['--native --ref'],
108108
['--ref'],
109109
['--replace-attr-values "#063855=currentColor"'],
110-
[`--svg-props "hidden={true}"`],
110+
[`--svg-props "hidden={true},id=hello"`],
111111
['--no-svgo'],
112112
['--no-prettier'],
113113
['--title-prop'],

packages/core/src/__snapshots__/config.test.js.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Object {
66
"expandProps": "end",
77
"h2xConfig": null,
88
"icon": true,
9+
"memo": false,
910
"native": false,
1011
"noSemi": true,
1112
"plugins": null,
@@ -33,6 +34,7 @@ Object {
3334
"expandProps": "end",
3435
"h2xConfig": null,
3536
"icon": true,
37+
"memo": false,
3638
"native": false,
3739
"noSemi": true,
3840
"plugins": null,
@@ -61,6 +63,7 @@ Object {
6163
"expandProps": "end",
6264
"h2xConfig": null,
6365
"icon": false,
66+
"memo": false,
6467
"native": false,
6568
"plugins": null,
6669
"prettier": true,
@@ -82,6 +85,7 @@ Object {
8285
"expandProps": "end",
8386
"h2xConfig": null,
8487
"icon": true,
88+
"memo": false,
8589
"native": false,
8690
"noSemi": true,
8791
"plugins": null,
@@ -109,6 +113,7 @@ Object {
109113
"expandProps": "end",
110114
"h2xConfig": null,
111115
"icon": true,
116+
"memo": false,
112117
"native": false,
113118
"noSemi": true,
114119
"plugins": null,
@@ -136,6 +141,7 @@ Object {
136141
"expandProps": "end",
137142
"h2xConfig": null,
138143
"icon": true,
144+
"memo": false,
139145
"native": false,
140146
"noSemi": true,
141147
"plugins": null,
@@ -164,6 +170,7 @@ Object {
164170
"expandProps": "end",
165171
"h2xConfig": null,
166172
"icon": false,
173+
"memo": false,
167174
"native": false,
168175
"plugins": null,
169176
"prettier": true,
@@ -185,6 +192,7 @@ Object {
185192
"expandProps": "end",
186193
"h2xConfig": null,
187194
"icon": true,
195+
"memo": false,
188196
"native": false,
189197
"noSemi": true,
190198
"plugins": null,

website/src/pages/docs/cli.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ To create icons, two options are important:
6464
$ npx @svgr/cli --icon --replace-attr-values "#000=currentColor" my-icon.svg
6565
```
6666

67+
> You can replace several values using `,` as separator: `--replace-attr-values "#000=currentColor,#fff=currentColor"`
68+
6769
## Target React Native
6870

6971
It is possible to target React Native using [react-native-svg](https://github.com/react-native-community/react-native-svg).

0 commit comments

Comments
 (0)