Skip to content

Commit

Permalink
fix(config): fix custom config & default options (#176)
Browse files Browse the repository at this point in the history
Ref #174

In this diff I fixed loading config from custom path specified in
`--config`.

Current behaviour is loading only `svg.config.js` in directory of
specified in `--config` file. This is not intuitive.

Also fixed the problem of overriding options specified in config by
defaults from cli.
  • Loading branch information
TrySound authored and gregberge committed Sep 16, 2018
1 parent 6d24230 commit 9a6c40b
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 3 deletions.
6 changes: 6 additions & 0 deletions __fixtures__/overrides.config.js
@@ -0,0 +1,6 @@
module.exports = {
expandProps: false,
dimensions: false,
svgo: false,
prettier: false,
}
31 changes: 31 additions & 0 deletions packages/cli/src/__snapshots__/index.test.js.snap
@@ -1,5 +1,36 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`cli should not override config with cli defaults 1`] = `
"import React from 'react'
const File = () => <svg viewBox=\\"0 0 48 1\\" version={1.1} xmlnsXlink=\\"http://www.w3.org/1999/xlink\\">
<title>
Rectangle 5
</title>
<desc>
Created with Sketch.
</desc>
<defs />
<g id=\\"Page-1\\" stroke=\\"none\\" strokeWidth={1} fill=\\"none\\" fillRule=\\"evenodd\\">
<g id=\\"19-Separator\\" transform=\\"translate(-129.000000, -156.000000)\\" fill=\\"#063855\\">
<g id=\\"Controls/Settings\\" transform=\\"translate(80.000000, 0.000000)\\">
<g id=\\"Content\\" transform=\\"translate(0.000000, 64.000000)\\">
<g id=\\"Group\\" transform=\\"translate(24.000000, 56.000000)\\">
<g id=\\"Group-2\\">
<rect id=\\"Rectangle-5\\" x={25} y={36} width={48} height={1} />
</g>
</g>
</g>
</g>
</g>
</g>
</svg>
export default File
"
`;

exports[`cli should support --prettier-config as file 1`] = `
"import React from 'react'
Expand Down
13 changes: 13 additions & 0 deletions packages/cli/src/index.js
Expand Up @@ -114,6 +114,19 @@ async function run() {

const config = { ...program }

if (config.expandProps === true) {
delete config.expandProps
}
if (config.dimensions === true) {
delete config.dimensions
}
if (config.svgo === true) {
delete config.svgo
}
if (config.prettier === true) {
delete config.prettier
}

if (config.template) {
try {
const template = require(path.join(process.cwd(), program.template)) // eslint-disable-line global-require, import/no-dynamic-require
Expand Down
11 changes: 11 additions & 0 deletions packages/cli/src/index.test.js
Expand Up @@ -168,4 +168,15 @@ describe('cli', () => {
},
10000,
)

it(
'should not override config with cli defaults',
async () => {
const result = await cli(
'__fixtures__/simple/file.svg --config=__fixtures__/overrides.config.js',
)
expect(result).toMatchSnapshot()
},
10000,
)
})
2 changes: 1 addition & 1 deletion packages/cli/src/util.js
Expand Up @@ -9,7 +9,7 @@ export const stat = util.promisify(fs.stat)

export async function convertFile(filePath, { config, ...options } = {}) {
const code = await readFile(filePath, 'utf-8')
const rcConfig = await resolveConfig(config || filePath)
const rcConfig = await resolveConfig(filePath, config)
return convert(code, { ...rcConfig, ...options }, { filePath })
}

Expand Down
8 changes: 6 additions & 2 deletions packages/core/src/config.js
Expand Up @@ -24,8 +24,12 @@ const explorer = cosmiconfig('svgr', {
rcExtensions: true,
})

export async function resolveConfig(filePath) {
const result = await explorer.search(filePath)
export async function resolveConfig(searchFrom, configFile) {
if (configFile == null) {
const result = await explorer.search(searchFrom)
return result ? result.config : null
}
const result = await explorer.load(configFile)
return result ? result.config : null
}

Expand Down

0 comments on commit 9a6c40b

Please sign in to comment.