Skip to content

Commit a23a186

Browse files
committed
feat(cli): make all CLI options available in config
Related #431, #437
1 parent d0b59cd commit a23a186

File tree

4 files changed

+33
-26
lines changed

4 files changed

+33
-26
lines changed

packages/cli/src/index.js

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,28 @@ import program from 'commander'
33
import path from 'path'
44
import glob from 'glob'
55
import fs from 'fs'
6+
import { loadConfig } from '@svgr/core'
67
import pkg from '../package.json'
78
import fileCommand from './fileCommand'
89
import dirCommand from './dirCommand'
910
import { stat, exitError } from './util'
1011

12+
function noUndefinedKeys(obj) {
13+
return Object.entries(obj).reduce((obj, [key, value]) => {
14+
if (value !== undefined) {
15+
obj[key] = value
16+
}
17+
return obj
18+
}, {})
19+
}
20+
1121
function parseObject(arg, accumulation = {}) {
1222
const [name, value] = arg.split('=')
1323
return { ...accumulation, [name]: value }
1424
}
1525

1626
function parseObjectList(arg, accumulation = {}) {
17-
const args = arg.split(',').map(str => str.trim())
27+
const args = arg.split(',').map((str) => str.trim())
1828
return args.reduce((acc, arg) => parseObject(arg, acc), accumulation)
1929
}
2030

@@ -27,7 +37,7 @@ function isFile(filePath) {
2737
}
2838
}
2939

30-
const parseConfig = name => arg => {
40+
const parseConfig = (name) => (arg) => {
3141
const json = isFile(arg) ? fs.readFileSync(arg) : arg
3242
try {
3343
return JSON.parse(json)
@@ -117,7 +127,7 @@ async function run() {
117127
}, [])
118128

119129
await Promise.all(
120-
filenames.map(async filename => {
130+
filenames.map(async (filename) => {
121131
try {
122132
await stat(filename)
123133
} catch (error) {
@@ -131,25 +141,30 @@ async function run() {
131141
process.exit(2)
132142
}
133143

134-
const config = { ...program }
144+
const opts = noUndefinedKeys(program.opts())
135145

136-
if (config.expandProps === 'none') {
146+
const config = await loadConfig(opts, { filePath: process.cwd() })
147+
148+
// Back config file
149+
config.configFile = opts.configFile
150+
151+
if (program.expandProps === 'none') {
137152
config.expandProps = false
138153
}
139154

140-
if (config.dimensions === true) {
155+
if (program.dimensions === true) {
141156
delete config.dimensions
142157
}
143158

144-
if (config.svgo === true) {
159+
if (program.svgo === true) {
145160
delete config.svgo
146161
}
147162

148-
if (config.prettier === true) {
163+
if (program.prettier === true) {
149164
delete config.prettier
150165
}
151166

152-
if (config.template) {
167+
if (program.template) {
153168
try {
154169
// eslint-disable-next-line global-require, import/no-dynamic-require
155170
const template = require(path.join(process.cwd(), program.template))
@@ -165,7 +180,7 @@ async function run() {
165180
}
166181
}
167182

168-
if (config.indexTemplate) {
183+
if (program.indexTemplate) {
169184
try {
170185
// eslint-disable-next-line global-require, import/no-dynamic-require
171186
const indexTemplate = require(path.join(
@@ -187,10 +202,11 @@ async function run() {
187202
}
188203

189204
const command = program.outDir ? dirCommand : fileCommand
205+
190206
await command(program, filenames, config)
191207
}
192208

193-
run().catch(error => {
209+
run().catch((error) => {
194210
setTimeout(() => {
195211
throw error
196212
})

packages/core/src/__snapshots__/config.test.js.snap

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ exports[`svgo async #loadConfig [async] should load config using filePath 1`] =
44
Object {
55
"dimensions": true,
66
"expandProps": "end",
7-
"h2xConfig": null,
87
"icon": true,
98
"memo": false,
109
"native": false,
@@ -33,7 +32,6 @@ exports[`svgo async #loadConfig [async] should not load config with "runtimeConf
3332
Object {
3433
"dimensions": true,
3534
"expandProps": "end",
36-
"h2xConfig": null,
3735
"icon": true,
3836
"memo": false,
3937
"native": false,
@@ -63,7 +61,6 @@ exports[`svgo async #loadConfig [async] should use default config without state.
6361
Object {
6462
"dimensions": false,
6563
"expandProps": "end",
66-
"h2xConfig": null,
6764
"icon": false,
6865
"memo": false,
6966
"native": false,
@@ -86,7 +83,6 @@ exports[`svgo async #loadConfig [async] should work with custom config path 1`]
8683
Object {
8784
"dimensions": true,
8885
"expandProps": "end",
89-
"h2xConfig": null,
9086
"icon": true,
9187
"memo": false,
9288
"native": false,
@@ -115,7 +111,6 @@ exports[`svgo sync #loadConfig [sync] should load config using filePath 1`] = `
115111
Object {
116112
"dimensions": true,
117113
"expandProps": "end",
118-
"h2xConfig": null,
119114
"icon": true,
120115
"memo": false,
121116
"native": false,
@@ -144,7 +139,6 @@ exports[`svgo sync #loadConfig [sync] should not load config with "runtimeConfig
144139
Object {
145140
"dimensions": true,
146141
"expandProps": "end",
147-
"h2xConfig": null,
148142
"icon": true,
149143
"memo": false,
150144
"native": false,
@@ -174,7 +168,6 @@ exports[`svgo sync #loadConfig [sync] should use default config without state.fi
174168
Object {
175169
"dimensions": false,
176170
"expandProps": "end",
177-
"h2xConfig": null,
178171
"icon": false,
179172
"memo": false,
180173
"native": false,
@@ -197,7 +190,6 @@ exports[`svgo sync #loadConfig [sync] should work with custom config path 1`] =
197190
Object {
198191
"dimensions": true,
199192
"expandProps": "end",
200-
"h2xConfig": null,
201193
"icon": true,
202194
"memo": false,
203195
"native": false,

packages/core/src/config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { cosmiconfig, cosmiconfigSync } from 'cosmiconfig'
22

33
export const DEFAULT_CONFIG = {
4-
h2xConfig: null,
54
dimensions: true,
65
expandProps: 'end',
76
icon: false,

website/src/pages/docs/options.mdx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,9 @@ example of template, see [the default one](https://github.com/gregberge/svgr/tre
170170

171171
Output files into a directory.
172172

173-
| Default | CLI Override | API Override |
174-
| ----------- | --------------------- | --------------------- |
175-
| `undefined` | `--out-dir <dirname>` | Only available in CLI |
173+
| Default | CLI Override | API Override |
174+
| ----------- | --------------------- | ------------------ |
175+
| `undefined` | `--out-dir <dirname>` | `outDir: <string>` |
176176

177177
## index.js template
178178

@@ -186,6 +186,6 @@ Specify a template function (API) to change default index.js output (when --out-
186186

187187
When used with `--out-dir`, it ignores already existing files.
188188

189-
| Default | CLI Override | API Override |
190-
| ------- | ------------------- | --------------------- |
191-
| `false` | `--ignore-existing` | Only available in CLI |
189+
| Default | CLI Override | API Override |
190+
| ------- | ------------------- | ------------------------ |
191+
| `false` | `--ignore-existing` | `ignoreExisting: <bool>` |

0 commit comments

Comments
 (0)