From a23a18675c0dd4a461d2fcbdc72a305cabd32a13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Berg=C3=A9?= Date: Mon, 27 Apr 2020 21:22:21 +0200 Subject: [PATCH] feat(cli): make all CLI options available in config Related #431, #437 --- packages/cli/src/index.js | 38 +++++++++++++------ .../src/__snapshots__/config.test.js.snap | 8 ---- packages/core/src/config.js | 1 - website/src/pages/docs/options.mdx | 12 +++--- 4 files changed, 33 insertions(+), 26 deletions(-) diff --git a/packages/cli/src/index.js b/packages/cli/src/index.js index 9d8b53b0..94777eff 100644 --- a/packages/cli/src/index.js +++ b/packages/cli/src/index.js @@ -3,18 +3,28 @@ import program from 'commander' import path from 'path' import glob from 'glob' import fs from 'fs' +import { loadConfig } from '@svgr/core' import pkg from '../package.json' import fileCommand from './fileCommand' import dirCommand from './dirCommand' import { stat, exitError } from './util' +function noUndefinedKeys(obj) { + return Object.entries(obj).reduce((obj, [key, value]) => { + if (value !== undefined) { + obj[key] = value + } + return obj + }, {}) +} + function parseObject(arg, accumulation = {}) { const [name, value] = arg.split('=') return { ...accumulation, [name]: value } } function parseObjectList(arg, accumulation = {}) { - const args = arg.split(',').map(str => str.trim()) + const args = arg.split(',').map((str) => str.trim()) return args.reduce((acc, arg) => parseObject(arg, acc), accumulation) } @@ -27,7 +37,7 @@ function isFile(filePath) { } } -const parseConfig = name => arg => { +const parseConfig = (name) => (arg) => { const json = isFile(arg) ? fs.readFileSync(arg) : arg try { return JSON.parse(json) @@ -117,7 +127,7 @@ async function run() { }, []) await Promise.all( - filenames.map(async filename => { + filenames.map(async (filename) => { try { await stat(filename) } catch (error) { @@ -131,25 +141,30 @@ async function run() { process.exit(2) } - const config = { ...program } + const opts = noUndefinedKeys(program.opts()) - if (config.expandProps === 'none') { + const config = await loadConfig(opts, { filePath: process.cwd() }) + + // Back config file + config.configFile = opts.configFile + + if (program.expandProps === 'none') { config.expandProps = false } - if (config.dimensions === true) { + if (program.dimensions === true) { delete config.dimensions } - if (config.svgo === true) { + if (program.svgo === true) { delete config.svgo } - if (config.prettier === true) { + if (program.prettier === true) { delete config.prettier } - if (config.template) { + if (program.template) { try { // eslint-disable-next-line global-require, import/no-dynamic-require const template = require(path.join(process.cwd(), program.template)) @@ -165,7 +180,7 @@ async function run() { } } - if (config.indexTemplate) { + if (program.indexTemplate) { try { // eslint-disable-next-line global-require, import/no-dynamic-require const indexTemplate = require(path.join( @@ -187,10 +202,11 @@ async function run() { } const command = program.outDir ? dirCommand : fileCommand + await command(program, filenames, config) } -run().catch(error => { +run().catch((error) => { setTimeout(() => { throw error }) diff --git a/packages/core/src/__snapshots__/config.test.js.snap b/packages/core/src/__snapshots__/config.test.js.snap index 8eeffc3c..bc962618 100644 --- a/packages/core/src/__snapshots__/config.test.js.snap +++ b/packages/core/src/__snapshots__/config.test.js.snap @@ -4,7 +4,6 @@ exports[`svgo async #loadConfig [async] should load config using filePath 1`] = Object { "dimensions": true, "expandProps": "end", - "h2xConfig": null, "icon": true, "memo": false, "native": false, @@ -33,7 +32,6 @@ exports[`svgo async #loadConfig [async] should not load config with "runtimeConf Object { "dimensions": true, "expandProps": "end", - "h2xConfig": null, "icon": true, "memo": false, "native": false, @@ -63,7 +61,6 @@ exports[`svgo async #loadConfig [async] should use default config without state. Object { "dimensions": false, "expandProps": "end", - "h2xConfig": null, "icon": false, "memo": false, "native": false, @@ -86,7 +83,6 @@ exports[`svgo async #loadConfig [async] should work with custom config path 1`] Object { "dimensions": true, "expandProps": "end", - "h2xConfig": null, "icon": true, "memo": false, "native": false, @@ -115,7 +111,6 @@ exports[`svgo sync #loadConfig [sync] should load config using filePath 1`] = ` Object { "dimensions": true, "expandProps": "end", - "h2xConfig": null, "icon": true, "memo": false, "native": false, @@ -144,7 +139,6 @@ exports[`svgo sync #loadConfig [sync] should not load config with "runtimeConfig Object { "dimensions": true, "expandProps": "end", - "h2xConfig": null, "icon": true, "memo": false, "native": false, @@ -174,7 +168,6 @@ exports[`svgo sync #loadConfig [sync] should use default config without state.fi Object { "dimensions": false, "expandProps": "end", - "h2xConfig": null, "icon": false, "memo": false, "native": false, @@ -197,7 +190,6 @@ exports[`svgo sync #loadConfig [sync] should work with custom config path 1`] = Object { "dimensions": true, "expandProps": "end", - "h2xConfig": null, "icon": true, "memo": false, "native": false, diff --git a/packages/core/src/config.js b/packages/core/src/config.js index 58429389..6c9db468 100644 --- a/packages/core/src/config.js +++ b/packages/core/src/config.js @@ -1,7 +1,6 @@ import { cosmiconfig, cosmiconfigSync } from 'cosmiconfig' export const DEFAULT_CONFIG = { - h2xConfig: null, dimensions: true, expandProps: 'end', icon: false, diff --git a/website/src/pages/docs/options.mdx b/website/src/pages/docs/options.mdx index 1f76dff2..1a97d279 100644 --- a/website/src/pages/docs/options.mdx +++ b/website/src/pages/docs/options.mdx @@ -170,9 +170,9 @@ example of template, see [the default one](https://github.com/gregberge/svgr/tre Output files into a directory. -| Default | CLI Override | API Override | -| ----------- | --------------------- | --------------------- | -| `undefined` | `--out-dir ` | Only available in CLI | +| Default | CLI Override | API Override | +| ----------- | --------------------- | ------------------ | +| `undefined` | `--out-dir ` | `outDir: ` | ## index.js template @@ -186,6 +186,6 @@ Specify a template function (API) to change default index.js output (when --out- When used with `--out-dir`, it ignores already existing files. -| Default | CLI Override | API Override | -| ------- | ------------------- | --------------------- | -| `false` | `--ignore-existing` | Only available in CLI | +| Default | CLI Override | API Override | +| ------- | ------------------- | ------------------------ | +| `false` | `--ignore-existing` | `ignoreExisting: ` |