Skip to content

Commit 4922f7a

Browse files
authored
feat(cli): support custom filename cases (#136)
Closes #118
1 parent cbd107f commit 4922f7a

8 files changed

Lines changed: 67 additions & 22 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ Options:
8888
--config <file> specify the path of the svgr config
8989
-d, --out-dir <dirname> output files into a directory
9090
--ext <ext> specify a custom file extension (default: "js")
91+
--filename-case <case> specify filename case (pascal, kebab, camel) (default: "pascal")
9192
--icon use "1em" as width and height
9293
--native add react-native support with react-native-svg
9394
--ref add svgRef prop to svg

packages/cli/README.md

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,33 @@ npm install @svgr/cli
1212

1313
## Usage
1414

15-
```js
16-
import svgr from '@svgr/core'
17-
18-
const svgCode = `
19-
<svg xmlns="http://www.w3.org/2000/svg"
20-
xmlns:xlink="http://www.w3.org/1999/xlink">
21-
<rect x="10" y="10" height="100" width="100"
22-
style="stroke:#ff0000; fill: #0000ff"/>
23-
</svg>
24-
`
25-
26-
svgr(svgCode, { icon: true }, { componentName: 'MyComponent' }).then(jsCode => {
27-
console.log(jsCode)
28-
})
15+
```
16+
Usage: index [options] <file>
17+
18+
Options:
19+
20+
-V, --version output the version number
21+
--config <file> specify the path of the svgr config
22+
-d, --out-dir <dirname> output files into a directory
23+
--ext <ext> specify a custom file extension (default: "js")
24+
--filename-case <case> specify filename case (pascal, kebab, camel) (default: "pascal")
25+
--icon use "1em" as width and height
26+
--native add react-native support with react-native-svg
27+
--ref add svgRef prop to svg
28+
--no-dimensions remove width and height from root SVG tag
29+
--no-expand-props disable props expanding
30+
--svg-attributes <property=value> add some attributes to the svg
31+
--replace-attr-values <old=new> replace an attribute value
32+
--template <file> specify a custom template to use
33+
--title-prop create a title element linked with props
34+
--prettier-config <fileOrJson> Prettier config
35+
--no-prettier disable Prettier
36+
--svgo-config <fileOrJson> SVGO config
37+
--no-svgo disable SVGO
38+
-h, --help output usage information
39+
40+
Examples:
41+
svgr --replace-attr-values "#fff=currentColor" icon.svg
2942
```
3043

3144
## License

packages/cli/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
"chalk": "^2.4.1",
2828
"commander": "^2.16.0",
2929
"glob": "^7.1.2",
30+
"lodash": "^4.17.10",
3031
"output-file-sync": "^2.0.1",
3132
"recursive-readdir": "^2.2.2"
32-
}
33+
},
34+
"devDependencies": {}
3335
}

packages/cli/src/dirCommand.js

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,34 @@ import path from 'path'
33
import outputFileSync from 'output-file-sync'
44
import readdir from 'recursive-readdir'
55
import { pascalCase } from '@svgr/core'
6+
import camelCase from 'lodash/camelCase'
7+
import kebabCase from 'lodash/kebabCase'
68
import { stat, convertFile } from './util'
79

8-
function rename(relative, ext) {
10+
const CASE = {
11+
KEBAB: 'kebab', // kebab-case
12+
CAMEL: 'camel', // camelCase
13+
PASCAL: 'pascal', // PascalCase
14+
}
15+
16+
function transformFilename(filename, filenameCase) {
17+
switch (filenameCase) {
18+
case CASE.KEBAB:
19+
return kebabCase(filename)
20+
case CASE.CAMEL:
21+
return camelCase(filename)
22+
case CASE.PASCAL:
23+
return pascalCase(filename)
24+
default:
25+
throw new Error(`Unknown --filename-case ${filenameCase}`)
26+
}
27+
}
28+
29+
function rename(relative, ext, filenameCase) {
930
const relativePath = path.parse(relative)
1031
relativePath.ext = `.${ext}`
11-
relativePath.name = pascalCase(relativePath.name)
1232
relativePath.base = null
33+
relativePath.name = transformFilename(relativePath.name, filenameCase)
1334

1435
return path.format(relativePath)
1536
}
@@ -21,10 +42,14 @@ export function isCompilable(filename) {
2142
return COMPILABLE_EXTENSIONS.includes(ext)
2243
}
2344

24-
async function dirCommand(program, filenames, { ext = 'js', ...options }) {
45+
async function dirCommand(
46+
program,
47+
filenames,
48+
{ ext = 'js', filenameCase = CASE.PASCAL, ...options },
49+
) {
2550
async function write(src, relative) {
2651
if (!isCompilable(relative)) return false
27-
relative = rename(relative, ext)
52+
relative = rename(relative, ext, filenameCase)
2853

2954
const dest = path.join(program.outDir, relative)
3055
const code = await convertFile(src, options, { filePath: dest })

packages/cli/src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ program
4040
.option('--config <file>', 'specify the path of the svgr config')
4141
.option('-d, --out-dir <dirname>', 'output files into a directory')
4242
.option('--ext <ext>', 'specify a custom file extension (default: "js")')
43+
.option(
44+
'--filename-case <case>',
45+
'specify filename case (pascal, kebab, camel) (default: "pascal")',
46+
)
4347
.option('--icon', 'use "1em" as width and height')
4448
.option('--native', 'add react-native support with react-native-svg')
4549
.option('--ref', 'add svgRef prop to svg')

packages/core/src/config.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('config', () => {
3030
const config = await resolveConfigFile(
3131
path.join(__dirname, '__fixtures__/svgr'),
3232
)
33-
expect(config).toMatch(/__fixtures__\/svgr\/\.svgrrc$/)
33+
expect(config).toMatch(/__fixtures__(\/|\\)svgr(\/|\\)\.svgrrc$/)
3434
})
3535
})
3636
})

packages/rollup/src/index.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const compile = (plugins = [svgr()]) =>
1010
})
1111

1212
const getCode = bundler =>
13-
bundler.modules.find(({ id }) => id.includes('__fixtures__/simple/file.svg'))
13+
bundler.modules.find(({ id }) => id.includes('__fixtures__/simple/file.svg') || id.includes('__fixtures__\\simple\\file.svg'))
1414
.code
1515

1616
describe('rollup loader', () => {

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1230,7 +1230,7 @@ babel-loader@^7.1.5:
12301230
loader-utils "^1.0.2"
12311231
mkdirp "^0.5.1"
12321232

1233-
babel-loader@^8.0.0:
1233+
babel-loader@^8.0.0-beta.4:
12341234
version "8.0.0-beta.4"
12351235
resolved "https://registry.yarnpkg.com/babel-loader/-/babel-loader-8.0.0-beta.4.tgz#c3fab00696c385c70c04dbe486391f0eb996f345"
12361236
dependencies:

0 commit comments

Comments
 (0)