Skip to content

Commit

Permalink
feat(cli): make all CLI options available in config
Browse files Browse the repository at this point in the history
Related #431, #437
  • Loading branch information
gregberge committed Apr 27, 2020
1 parent d0b59cd commit a23a186
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 26 deletions.
38 changes: 27 additions & 11 deletions packages/cli/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
Expand Down Expand Up @@ -117,7 +127,7 @@ async function run() {
}, [])

await Promise.all(
filenames.map(async filename => {
filenames.map(async (filename) => {
try {
await stat(filename)
} catch (error) {
Expand All @@ -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))
Expand All @@ -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(
Expand All @@ -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
})
Expand Down
8 changes: 0 additions & 8 deletions packages/core/src/__snapshots__/config.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { cosmiconfig, cosmiconfigSync } from 'cosmiconfig'

export const DEFAULT_CONFIG = {
h2xConfig: null,
dimensions: true,
expandProps: 'end',
icon: false,
Expand Down
12 changes: 6 additions & 6 deletions website/src/pages/docs/options.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <dirname>` | Only available in CLI |
| Default | CLI Override | API Override |
| ----------- | --------------------- | ------------------ |
| `undefined` | `--out-dir <dirname>` | `outDir: <string>` |

## index.js template

Expand All @@ -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: <bool>` |

0 comments on commit a23a186

Please sign in to comment.