From 9a6c40bc9653d60473bdfb60e53b48f97a75cc86 Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Mon, 17 Sep 2018 00:52:59 +0300 Subject: [PATCH] fix(config): fix custom config & default options (#176) Ref https://github.com/smooth-code/svgr/issues/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. --- __fixtures__/overrides.config.js | 6 ++++ .../cli/src/__snapshots__/index.test.js.snap | 31 +++++++++++++++++++ packages/cli/src/index.js | 13 ++++++++ packages/cli/src/index.test.js | 11 +++++++ packages/cli/src/util.js | 2 +- packages/core/src/config.js | 8 +++-- 6 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 __fixtures__/overrides.config.js diff --git a/__fixtures__/overrides.config.js b/__fixtures__/overrides.config.js new file mode 100644 index 00000000..03ac1627 --- /dev/null +++ b/__fixtures__/overrides.config.js @@ -0,0 +1,6 @@ +module.exports = { + expandProps: false, + dimensions: false, + svgo: false, + prettier: false, +} diff --git a/packages/cli/src/__snapshots__/index.test.js.snap b/packages/cli/src/__snapshots__/index.test.js.snap index 77f54221..a009dbee 100644 --- a/packages/cli/src/__snapshots__/index.test.js.snap +++ b/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 = () => + + Rectangle 5 + + + Created with Sketch. + + + + + + + + + + + + + + + + + + +export default File +" +`; + exports[`cli should support --prettier-config as file 1`] = ` "import React from 'react' diff --git a/packages/cli/src/index.js b/packages/cli/src/index.js index 72bf4720..162c3732 100644 --- a/packages/cli/src/index.js +++ b/packages/cli/src/index.js @@ -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 diff --git a/packages/cli/src/index.test.js b/packages/cli/src/index.test.js index 90afd345..ebb3c782 100644 --- a/packages/cli/src/index.test.js +++ b/packages/cli/src/index.test.js @@ -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, + ) }) diff --git a/packages/cli/src/util.js b/packages/cli/src/util.js index 1bff67e2..da7be4dc 100644 --- a/packages/cli/src/util.js +++ b/packages/cli/src/util.js @@ -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 }) } diff --git a/packages/core/src/config.js b/packages/core/src/config.js index 34da6a46..b52680fc 100644 --- a/packages/core/src/config.js +++ b/packages/core/src/config.js @@ -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 }