Skip to content

Commit 64f9ec1

Browse files
committed
feat(cli): show help without any option
Closes #349
1 parent 4a0135b commit 64f9ec1

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

packages/cli/src/__snapshots__/index.test.js.snap

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,22 @@ Array [
124124
]
125125
`;
126126

127+
exports[`cli should support stdin filepath 1`] = `
128+
"import React from 'react'
129+
130+
function SvgFile(props) {
131+
return (
132+
<svg width={48} height={1} {...props}>
133+
<path d=\\"M0 0h48v1H0z\\" fill=\\"#063855\\" fillRule=\\"evenodd\\" />
134+
</svg>
135+
)
136+
}
137+
138+
export default SvgFile
139+
140+
"
141+
`;
142+
127143
exports[`cli should support various args: --expand-props none 1`] = `
128144
"import React from 'react'
129145

packages/cli/src/fileCommand.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,21 @@ async function fileCommand(program, filenames, config) {
1717
})
1818

1919
process.stdin.on('end', () => {
20-
output(convert(code, config))
20+
output(convert(code, config, { filePath: program.stdinFilepath }))
2121
})
2222
}
2323

24-
if (filenames.length === 0) {
24+
if (program.stdin || (filenames.length === 0 && !process.stdin.isTTY)) {
2525
stdin()
2626
return
2727
}
2828

29+
if (filenames.length === 0) {
30+
// eslint-disable-next-line no-console
31+
console.log(program.helpInformation())
32+
return
33+
}
34+
2935
if (filenames.length > 1) {
3036
exitError('Please specify only one filename or use `--out-dir` option.')
3137
return

packages/cli/src/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ program
8888
)
8989
.option('--no-svgo', 'disable SVGO')
9090
.option('--silent', 'suppress output')
91+
.option('--stdin', 'force reading input from stdin')
92+
.option(
93+
'--stdin-filepath',
94+
'path to the file to pretend that stdin comes from',
95+
)
9196

9297
program.on('--help', () => {
9398
console.log(`

packages/cli/src/index.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ describe('cli', () => {
4848
expect(result).toMatchSnapshot()
4949
}, 10000)
5050

51+
it('should support stdin filepath', async () => {
52+
const result = await cli(
53+
'--stdin-filepath __fixtures__/simple/file.svg < __fixtures__/simple/file.svg',
54+
)
55+
expect(result).toMatchSnapshot()
56+
}, 10000)
57+
5158
it('should transform a whole directory and output relative destination paths', async () => {
5259
const result = await cli('--out-dir __fixtures_build__/whole __fixtures__')
5360
const sorted = result

0 commit comments

Comments
 (0)