@@ -3,18 +3,28 @@ import program from 'commander'
33import path from 'path'
44import glob from 'glob'
55import fs from 'fs'
6+ import { loadConfig } from '@svgr/core'
67import pkg from '../package.json'
78import fileCommand from './fileCommand'
89import dirCommand from './dirCommand'
910import { 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+
1121function parseObject ( arg , accumulation = { } ) {
1222 const [ name , value ] = arg . split ( '=' )
1323 return { ...accumulation , [ name ] : value }
1424}
1525
1626function 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 } )
0 commit comments