Skip to content

Commit aa9feb2

Browse files
authoredNov 1, 2021
feat(cli): allow all CLI options in config (#615)
Also run Prettier on index.js file. Closes #570
1 parent 10638d0 commit aa9feb2

File tree

6 files changed

+72
-70
lines changed

6 files changed

+72
-70
lines changed
 

‎__fixtures__/custom-index-template.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const path = require('path')
33
function indexTemplate(files) {
44
const exportEntries = files.map(file => {
55
const basename = path.basename(file, path.extname(file))
6-
return `export { ${basename} } from './${basename}'`
6+
return `export { ${basename} } from './${basename}';`
77
})
88
return exportEntries.join('\n')
99
}

‎packages/cli/src/__snapshots__/index.test.ts.snap

+10-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
exports[`cli should add Svg prefix to index.js exports staring with number 1`] = `
44
"export { default as Svg2File } from './2File'
5-
export { default as File } from './File'"
5+
export { default as File } from './File'
6+
"
67
`;
78

89
exports[`cli should not override config with cli defaults 1`] = `
@@ -19,7 +20,10 @@ export default SvgFile
1920
"
2021
`;
2122

22-
exports[`cli should support --index-template in cli 1`] = `"export { File } from './File'"`;
23+
exports[`cli should support --index-template in cli 1`] = `
24+
"export { File } from './File'
25+
"
26+
`;
2327

2428
exports[`cli should support --no-index 1`] = `
2529
Array [
@@ -92,7 +96,10 @@ Array [
9296
]
9397
`;
9498

95-
exports[`cli should support custom index.js with directory output 1`] = `"export { default as File } from './File'"`;
99+
exports[`cli should support custom index.js with directory output 1`] = `
100+
"export { File } from './File'
101+
"
102+
`;
96103

97104
exports[`cli should support different filename cases with directory output 1`] = `
98105
Array [

‎packages/cli/src/dirCommand.ts

+33-17
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ import { promises as fs } from 'fs'
33
import * as path from 'path'
44
import { grey, white } from 'chalk'
55
import { loadConfig, Config } from '@svgr/core'
6+
import { format, resolveConfig } from 'prettier'
67
import {
78
convertFile,
89
transformFilename,
910
politeWrite,
1011
formatExportName,
1112
} from './util'
12-
import type { SvgrCommand } from './index'
13+
import type { Options, SvgrCommand } from './index'
1314

1415
const exists = async (filepath: string) => {
1516
try {
@@ -50,28 +51,28 @@ const resolveExtension = (config: Config, ext?: string) =>
5051
ext || (config.typescript ? 'tsx' : 'js')
5152

5253
export const dirCommand: SvgrCommand = async (
53-
{
54+
opts,
55+
_,
56+
filenames,
57+
): Promise<void> => {
58+
const {
5459
ext: extOpt,
5560
filenameCase = 'pascal',
5661
ignoreExisting,
5762
silent,
58-
indexTemplate: indexTemplateOpt,
5963
configFile,
6064
outDir,
61-
},
62-
_,
63-
filenames,
64-
config,
65-
): Promise<void> => {
66-
const ext = resolveExtension(config, extOpt)
65+
} = opts
66+
67+
const ext = resolveExtension(opts, extOpt)
6768

6869
const write = async (src: string, dest: string) => {
6970
if (!isCompilable(src)) {
7071
return { transformed: false, dest: null }
7172
}
7273

7374
dest = rename(dest, ext, filenameCase)
74-
const code = await convertFile(src, config)
75+
const code = await convertFile(src, opts)
7576
const cwdRelative = path.relative(process.cwd(), dest)
7677
const logOutput = `${src} -> ${cwdRelative}\n`
7778

@@ -86,10 +87,25 @@ export const dirCommand: SvgrCommand = async (
8687
return { transformed: true, dest }
8788
}
8889

89-
const generateIndex = async (dest: string, files: string[]) => {
90-
const indexFile = path.join(dest, `index.${ext}`)
91-
const indexTemplate = indexTemplateOpt || defaultIndexTemplate
92-
await fs.writeFile(indexFile, indexTemplate(files))
90+
const generateIndex = async (
91+
dest: string,
92+
files: string[],
93+
opts: Options,
94+
) => {
95+
const filepath = path.join(dest, `index.${ext}`)
96+
const indexTemplate = opts.indexTemplate || defaultIndexTemplate
97+
const fileContent = indexTemplate(files)
98+
const prettyContent = await (async () => {
99+
if (!opts.prettier) return fileContent
100+
const prettierRcConfig = opts.runtimeConfig
101+
? await resolveConfig(filepath, { editorconfig: true })
102+
: {}
103+
return format(fileContent, {
104+
...prettierRcConfig,
105+
...opts.prettierConfig,
106+
})
107+
})()
108+
await fs.writeFile(filepath, prettyContent)
93109
}
94110

95111
async function handle(filename: string, root: string) {
@@ -114,11 +130,11 @@ export const dirCommand: SvgrCommand = async (
114130
path.relative(root, dirname),
115131
)
116132
const resolvedConfig = loadConfig.sync(
117-
{ configFile, ...config },
133+
{ configFile, ...opts },
118134
{ filePath: dest },
119-
)
135+
) as Options
120136
if (resolvedConfig.index) {
121-
await generateIndex(dest, destFiles)
137+
await generateIndex(dest, destFiles, opts)
122138
}
123139
}
124140
return { transformed: false, dest: null }

‎packages/cli/src/fileCommand.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@ export const fileCommand: SvgrCommand = async (
2121
opts,
2222
program,
2323
filenames,
24-
config,
2524
): Promise<void> => {
2625
if (opts.stdin || (filenames.length === 0 && !process.stdin.isTTY)) {
2726
const input = await readStdin()
28-
const output = convert(input, config, { filePath: opts.stdinFilepath })
27+
const output = convert(input, opts, { filePath: opts.stdinFilepath })
2928
process.stdout.write(`${output}\n`)
3029
return
3130
}
@@ -46,6 +45,6 @@ export const fileCommand: SvgrCommand = async (
4645
exitError('Directory are not supported without `--out-dir` option instead.')
4746
}
4847

49-
const output = await convertFile(filename, config)
48+
const output = await convertFile(filename, opts)
5049
process.stdout.write(`${output}\n`)
5150
}

‎packages/cli/src/index.test.ts

+19-19
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('cli', () => {
1818
it('should work with a simple file', async () => {
1919
const result = await cli('__fixtures__/simple/file.svg')
2020
expect(result).toMatchSnapshot()
21-
}, 10000)
21+
})
2222

2323
it('should not work with a directory without --out-dir option', async () => {
2424
expect.assertions(1)
@@ -29,7 +29,7 @@ describe('cli', () => {
2929
'Directory are not supported without `--out-dir` option instead',
3030
)
3131
}
32-
}, 10000)
32+
})
3333

3434
it('should not work with several files without destination', async () => {
3535
expect.assertions(1)
@@ -40,19 +40,19 @@ describe('cli', () => {
4040
'Please specify only one filename or use `--out-dir` option',
4141
)
4242
}
43-
}, 10000)
43+
})
4444

4545
it('should work with stdin', async () => {
4646
const result = await cli('< __fixtures__/simple/file.svg')
4747
expect(result).toMatchSnapshot()
48-
}, 10000)
48+
})
4949

5050
it('should support stdin filepath', async () => {
5151
const result = await cli(
5252
'--stdin-filepath __fixtures__/simple/file.svg < __fixtures__/simple/file.svg',
5353
)
5454
expect(result).toMatchSnapshot()
55-
}, 10000)
55+
})
5656

5757
it('should transform a whole directory and output relative destination paths', async () => {
5858
const result = await cli('--out-dir __fixtures_build__/whole __fixtures__')
@@ -62,7 +62,7 @@ describe('cli', () => {
6262
.map((x) => x.toLowerCase())
6363
.join('\n')
6464
expect(sorted).toMatchSnapshot()
65-
}, 10000)
65+
})
6666

6767
it('should transform a whole directory with --typescript', async () => {
6868
const result = await cli(
@@ -74,43 +74,43 @@ describe('cli', () => {
7474
.map((x) => x.toLowerCase())
7575
.join('\n')
7676
expect(sorted).toMatchSnapshot()
77-
}, 10000)
77+
})
7878

7979
it('should suppress output when transforming a directory with a --silent option', async () => {
8080
const result = await cli(
8181
'--silent --out-dir __fixtures_build__/whole __fixtures__',
8282
)
8383
const sorted = result.split(/\n/).sort().join('\n')
8484
expect(sorted).toMatchSnapshot()
85-
}, 10000)
85+
})
8686

8787
it('should support --prettier-config as json', async () => {
8888
const result = await cli(
8989
`--prettier-config '{"tabWidth": 5}' __fixtures__/simple/file.svg`,
9090
)
9191
expect(result).toMatchSnapshot()
92-
}, 10000)
92+
})
9393

9494
it('should support --prettier-config as file', async () => {
9595
const result = await cli(
9696
`--prettier-config __fixtures__/withPrettierRc/.prettierrc __fixtures__/simple/file.svg`,
9797
)
9898
expect(result).toMatchSnapshot()
99-
}, 10000)
99+
})
100100

101101
it('should support --svgo-config as json', async () => {
102102
const result = await cli(
103103
`--svgo-config '{"plugins":[{"name":"preset-default","params":{"overrides":{"removeTitle":false}}}]}' __fixtures__/simple/file.svg`,
104104
)
105105
expect(result).toMatchSnapshot()
106-
}, 10000)
106+
})
107107

108108
it('should support --svgo-config as file', async () => {
109109
const result = await cli(
110110
`--svgo-config __fixtures__/withSvgoConfig/svgo.config.js __fixtures__/simple/file.svg`,
111111
)
112112
expect(result).toMatchSnapshot()
113-
}, 10000)
113+
})
114114

115115
it.each([
116116
['--no-dimensions'],
@@ -164,22 +164,22 @@ describe('cli', () => {
164164
await del(outDir)
165165
await cli(`--ext=ts ${inDir} --out-dir=${outDir}`)
166166
expect(await fs.readdir(outDir)).toMatchSnapshot()
167-
}, 10000)
167+
})
168168

169169
it('should support "--ignore-existing"', async () => {
170170
const inDir = '__fixtures__/simple'
171171
const outDir = '__fixtures__/simple-existing'
172172
await cli(`${inDir} --out-dir=${outDir} --ignore-existing`)
173173
const content = await fs.readFile(path.join(outDir, 'File.js'), 'utf-8')
174174
expect(content).toBe('// nothing')
175-
}, 10000)
175+
})
176176

177177
it('should not override config with cli defaults', async () => {
178178
const result = await cli(
179179
'__fixtures__/simple/file.svg --config-file=__fixtures__/overrides.config.js',
180180
)
181181
expect(result).toMatchSnapshot()
182-
}, 10000)
182+
})
183183

184184
it('should add Svg prefix to index.js exports staring with number', async () => {
185185
const inDir = '__fixtures__/numeric'
@@ -188,7 +188,7 @@ describe('cli', () => {
188188
await cli(`${inDir} --out-dir=${outDir}`)
189189
const content = await fs.readFile(path.join(outDir, 'index.js'), 'utf-8')
190190
expect(content).toMatchSnapshot()
191-
}, 10000)
191+
})
192192

193193
it('should support custom index.js with directory output', async () => {
194194
const inDir = '__fixtures__/simple'
@@ -199,7 +199,7 @@ describe('cli', () => {
199199
)
200200
const content = await fs.readFile(path.join(outDir, 'index.js'), 'utf-8')
201201
expect(content).toMatchSnapshot()
202-
}, 10000)
202+
})
203203

204204
it('should support --index-template in cli', async () => {
205205
const inDir = '__fixtures__/simple'
@@ -210,13 +210,13 @@ describe('cli', () => {
210210
)
211211
const content = await fs.readFile(path.join(outDir, 'index.js'), 'utf-8')
212212
expect(content).toMatchSnapshot()
213-
}, 10000)
213+
})
214214

215215
it('should support --no-index', async () => {
216216
const inDir = '__fixtures__/simple'
217217
const outDir = `__fixtures_build__/no-index-case`
218218
await del(outDir)
219219
await cli(`--no-index ${inDir} --out-dir=${outDir}`)
220220
expect(await fs.readdir(outDir)).toMatchSnapshot()
221-
}, 10000)
221+
})
222222
})

‎packages/cli/src/index.ts

+7-27
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const parseTemplate = (name: string) => (arg: string) => {
6868
}
6969
}
7070

71-
interface ProgramOpts extends Config {
71+
export interface Options extends Config {
7272
configFile?: string
7373
runtimeConfig?: boolean
7474
outDir?: string
@@ -82,12 +82,7 @@ interface ProgramOpts extends Config {
8282
}
8383

8484
export interface SvgrCommand {
85-
(
86-
opts: ProgramOpts,
87-
program: Command,
88-
filenames: string[],
89-
config: Config,
90-
): Promise<void>
85+
(opts: Options, program: Command, filenames: string[]): Promise<void>
9186
}
9287

9388
program
@@ -193,28 +188,13 @@ async function run() {
193188
process.exit(2)
194189
}
195190

196-
const opts = noUndefinedKeys(program.opts<ProgramOpts>())
197-
198-
const config = await loadConfig(opts, { filePath: process.cwd() })
199-
200-
if (opts.dimensions === true) {
201-
delete config.dimensions
202-
}
203-
204-
if (opts.svgo === true) {
205-
delete config.svgo
206-
}
207-
208-
if (opts.prettier === true) {
209-
delete config.prettier
210-
}
211-
212-
if (opts.index === false) {
213-
delete config.index
214-
}
191+
const programOpts = noUndefinedKeys(program.opts<Options>())
192+
const opts = (await loadConfig(programOpts, {
193+
filePath: process.cwd(),
194+
})) as Options
215195

216196
const command = opts.outDir ? dirCommand : fileCommand
217-
await command(opts, program, filenames, config)
197+
await command(opts, program, filenames)
218198
}
219199

220200
run().catch((error) => {

1 commit comments

Comments
 (1)

vercel[bot] commented on Nov 1, 2021

@vercel[bot]
Please sign in to comment.