Skip to content

Commit

Permalink
feat(cli): support custom filename cases (#136)
Browse files Browse the repository at this point in the history
Closes #118
  • Loading branch information
gregberge committed Jul 8, 2018
1 parent cbd107f commit 4922f7a
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 22 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -88,6 +88,7 @@ Options:
--config <file> specify the path of the svgr config
-d, --out-dir <dirname> output files into a directory
--ext <ext> specify a custom file extension (default: "js")
--filename-case <case> specify filename case (pascal, kebab, camel) (default: "pascal")
--icon use "1em" as width and height
--native add react-native support with react-native-svg
--ref add svgRef prop to svg
Expand Down
41 changes: 27 additions & 14 deletions packages/cli/README.md
Expand Up @@ -12,20 +12,33 @@ npm install @svgr/cli

## Usage

```js
import svgr from '@svgr/core'

const svgCode = `
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink">
<rect x="10" y="10" height="100" width="100"
style="stroke:#ff0000; fill: #0000ff"/>
</svg>
`

svgr(svgCode, { icon: true }, { componentName: 'MyComponent' }).then(jsCode => {
console.log(jsCode)
})
```
Usage: index [options] <file>
Options:
-V, --version output the version number
--config <file> specify the path of the svgr config
-d, --out-dir <dirname> output files into a directory
--ext <ext> specify a custom file extension (default: "js")
--filename-case <case> specify filename case (pascal, kebab, camel) (default: "pascal")
--icon use "1em" as width and height
--native add react-native support with react-native-svg
--ref add svgRef prop to svg
--no-dimensions remove width and height from root SVG tag
--no-expand-props disable props expanding
--svg-attributes <property=value> add some attributes to the svg
--replace-attr-values <old=new> replace an attribute value
--template <file> specify a custom template to use
--title-prop create a title element linked with props
--prettier-config <fileOrJson> Prettier config
--no-prettier disable Prettier
--svgo-config <fileOrJson> SVGO config
--no-svgo disable SVGO
-h, --help output usage information
Examples:
svgr --replace-attr-values "#fff=currentColor" icon.svg
```

## License
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/package.json
Expand Up @@ -27,7 +27,9 @@
"chalk": "^2.4.1",
"commander": "^2.16.0",
"glob": "^7.1.2",
"lodash": "^4.17.10",
"output-file-sync": "^2.0.1",
"recursive-readdir": "^2.2.2"
}
},
"devDependencies": {}
}
33 changes: 29 additions & 4 deletions packages/cli/src/dirCommand.js
Expand Up @@ -3,13 +3,34 @@ import path from 'path'
import outputFileSync from 'output-file-sync'
import readdir from 'recursive-readdir'
import { pascalCase } from '@svgr/core'
import camelCase from 'lodash/camelCase'
import kebabCase from 'lodash/kebabCase'
import { stat, convertFile } from './util'

function rename(relative, ext) {
const CASE = {
KEBAB: 'kebab', // kebab-case
CAMEL: 'camel', // camelCase
PASCAL: 'pascal', // PascalCase
}

function transformFilename(filename, filenameCase) {
switch (filenameCase) {
case CASE.KEBAB:
return kebabCase(filename)
case CASE.CAMEL:
return camelCase(filename)
case CASE.PASCAL:
return pascalCase(filename)
default:
throw new Error(`Unknown --filename-case ${filenameCase}`)
}
}

function rename(relative, ext, filenameCase) {
const relativePath = path.parse(relative)
relativePath.ext = `.${ext}`
relativePath.name = pascalCase(relativePath.name)
relativePath.base = null
relativePath.name = transformFilename(relativePath.name, filenameCase)

return path.format(relativePath)
}
Expand All @@ -21,10 +42,14 @@ export function isCompilable(filename) {
return COMPILABLE_EXTENSIONS.includes(ext)
}

async function dirCommand(program, filenames, { ext = 'js', ...options }) {
async function dirCommand(
program,
filenames,
{ ext = 'js', filenameCase = CASE.PASCAL, ...options },
) {
async function write(src, relative) {
if (!isCompilable(relative)) return false
relative = rename(relative, ext)
relative = rename(relative, ext, filenameCase)

const dest = path.join(program.outDir, relative)
const code = await convertFile(src, options, { filePath: dest })
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/src/index.js
Expand Up @@ -40,6 +40,10 @@ program
.option('--config <file>', 'specify the path of the svgr config')
.option('-d, --out-dir <dirname>', 'output files into a directory')
.option('--ext <ext>', 'specify a custom file extension (default: "js")')
.option(
'--filename-case <case>',
'specify filename case (pascal, kebab, camel) (default: "pascal")',
)
.option('--icon', 'use "1em" as width and height')
.option('--native', 'add react-native support with react-native-svg')
.option('--ref', 'add svgRef prop to svg')
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/config.test.js
Expand Up @@ -30,7 +30,7 @@ describe('config', () => {
const config = await resolveConfigFile(
path.join(__dirname, '__fixtures__/svgr'),
)
expect(config).toMatch(/__fixtures__\/svgr\/\.svgrrc$/)
expect(config).toMatch(/__fixtures__(\/|\\)svgr(\/|\\)\.svgrrc$/)
})
})
})
2 changes: 1 addition & 1 deletion packages/rollup/src/index.test.js
Expand Up @@ -10,7 +10,7 @@ const compile = (plugins = [svgr()]) =>
})

const getCode = bundler =>
bundler.modules.find(({ id }) => id.includes('__fixtures__/simple/file.svg'))
bundler.modules.find(({ id }) => id.includes('__fixtures__/simple/file.svg') || id.includes('__fixtures__\\simple\\file.svg'))
.code

describe('rollup loader', () => {
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Expand Up @@ -1230,7 +1230,7 @@ babel-loader@^7.1.5:
loader-utils "^1.0.2"
mkdirp "^0.5.1"

babel-loader@^8.0.0:
babel-loader@^8.0.0-beta.4:
version "8.0.0-beta.4"
resolved "https://registry.yarnpkg.com/babel-loader/-/babel-loader-8.0.0-beta.4.tgz#c3fab00696c385c70c04dbe486391f0eb996f345"
dependencies:
Expand Down

0 comments on commit 4922f7a

Please sign in to comment.