Skip to content

Commit 100147f

Browse files
everdimensiongregberge
authored andcommitted
feat: add "silent" option to cli (#351)
1 parent 441244a commit 100147f

File tree

5 files changed

+20
-2
lines changed

5 files changed

+20
-2
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,8 @@ export default SvgFile
330330
"
331331
`;
332332

333+
exports[`cli should suppress output when transforming a directory with a --silent option 1`] = `""`;
334+
333335
exports[`cli should transform a whole directory and output relative destination paths 1`] = `
334336
"
335337
__fixtures__/cased/PascalCase.svg -> __fixtures_build__/whole/cased/PascalCase.js

packages/cli/src/dirCommand.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import path from 'path'
33
import outputFileSync from 'output-file-sync'
44
import readdir from 'recursive-readdir'
5-
import { convertFile, stat, transformFilename, CASE } from './util'
5+
import { convertFile, stat, transformFilename, CASE, politeWrite } from './util'
66

77
function rename(relative, ext, filenameCase) {
88
const relativePath = path.parse(relative)
@@ -32,7 +32,7 @@ async function dirCommand(
3232
const dest = path.resolve(program.outDir, relative)
3333
const code = await convertFile(src, options)
3434
outputFileSync(dest, code)
35-
process.stdout.write(`${src} -> ${path.relative(process.cwd(), dest)}\n`)
35+
politeWrite(program, `${src} -> ${path.relative(process.cwd(), dest)}\n`)
3636
return true
3737
}
3838

packages/cli/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ program
8080
parseConfig('--svgo-config'),
8181
)
8282
.option('--no-svgo', 'disable SVGO')
83+
.option('--silent', 'suppress output')
8384

8485
program.on('--help', () => {
8586
console.log(`

packages/cli/src/index.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ describe('cli', () => {
5757
expect(sorted).toMatchSnapshot()
5858
}, 10000)
5959

60+
it('should suppress output when transforming a directory with a --silent option', async () => {
61+
const result = await cli('--silent --out-dir __fixtures_build__/whole __fixtures__')
62+
const sorted = result
63+
.split(/\n/)
64+
.sort()
65+
.join('\n')
66+
expect(sorted).toMatchSnapshot()
67+
}, 10000)
68+
6069
it('should support --prettier-config as json', async () => {
6170
const result = await cli(
6271
`--prettier-config '{"tabWidth": 5}' __fixtures__/simple/file.svg`,

packages/cli/src/util.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,9 @@ export function exitError(error) {
5050
console.error(chalk.red(error))
5151
process.exit(1)
5252
}
53+
54+
export function politeWrite(program, data) {
55+
if (!program.silent) {
56+
process.stdout.write(data);
57+
}
58+
}

0 commit comments

Comments
 (0)