Skip to content

Commit

Permalink
feat: add "silent" option to cli (#351)
Browse files Browse the repository at this point in the history
  • Loading branch information
everdimension authored and gregberge committed Dec 18, 2019
1 parent 441244a commit 100147f
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/cli/src/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,8 @@ export default SvgFile
"
`;

exports[`cli should suppress output when transforming a directory with a --silent option 1`] = `""`;

exports[`cli should transform a whole directory and output relative destination paths 1`] = `
"
__fixtures__/cased/PascalCase.svg -> __fixtures_build__/whole/cased/PascalCase.js
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/dirCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import path from 'path'
import outputFileSync from 'output-file-sync'
import readdir from 'recursive-readdir'
import { convertFile, stat, transformFilename, CASE } from './util'
import { convertFile, stat, transformFilename, CASE, politeWrite } from './util'

function rename(relative, ext, filenameCase) {
const relativePath = path.parse(relative)
Expand Down Expand Up @@ -32,7 +32,7 @@ async function dirCommand(
const dest = path.resolve(program.outDir, relative)
const code = await convertFile(src, options)
outputFileSync(dest, code)
process.stdout.write(`${src} -> ${path.relative(process.cwd(), dest)}\n`)
politeWrite(program, `${src} -> ${path.relative(process.cwd(), dest)}\n`)
return true
}

Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ program
parseConfig('--svgo-config'),
)
.option('--no-svgo', 'disable SVGO')
.option('--silent', 'suppress output')

program.on('--help', () => {
console.log(`
Expand Down
9 changes: 9 additions & 0 deletions packages/cli/src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ describe('cli', () => {
expect(sorted).toMatchSnapshot()
}, 10000)

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

it('should support --prettier-config as json', async () => {
const result = await cli(
`--prettier-config '{"tabWidth": 5}' __fixtures__/simple/file.svg`,
Expand Down
6 changes: 6 additions & 0 deletions packages/cli/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,9 @@ export function exitError(error) {
console.error(chalk.red(error))
process.exit(1)
}

export function politeWrite(program, data) {
if (!program.silent) {
process.stdout.write(data);
}
}

0 comments on commit 100147f

Please sign in to comment.