diff --git a/packages/cli/src/__snapshots__/index.test.js.snap b/packages/cli/src/__snapshots__/index.test.js.snap index 16e7c5bb..37332e11 100644 --- a/packages/cli/src/__snapshots__/index.test.js.snap +++ b/packages/cli/src/__snapshots__/index.test.js.snap @@ -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 diff --git a/packages/cli/src/dirCommand.js b/packages/cli/src/dirCommand.js index d9298419..f9e77da2 100644 --- a/packages/cli/src/dirCommand.js +++ b/packages/cli/src/dirCommand.js @@ -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) @@ -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 } diff --git a/packages/cli/src/index.js b/packages/cli/src/index.js index 23600848..8e7606ac 100644 --- a/packages/cli/src/index.js +++ b/packages/cli/src/index.js @@ -80,6 +80,7 @@ program parseConfig('--svgo-config'), ) .option('--no-svgo', 'disable SVGO') + .option('--silent', 'suppress output') program.on('--help', () => { console.log(` diff --git a/packages/cli/src/index.test.js b/packages/cli/src/index.test.js index d17808ba..56cdade0 100644 --- a/packages/cli/src/index.test.js +++ b/packages/cli/src/index.test.js @@ -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`, diff --git a/packages/cli/src/util.js b/packages/cli/src/util.js index 6df80bba..14a4e275 100644 --- a/packages/cli/src/util.js +++ b/packages/cli/src/util.js @@ -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); + } +}