From 32df0adcaa86160ce85ed3713b11b8d4d24b30cd Mon Sep 17 00:00:00 2001 From: Adam Reis Date: Sun, 21 Apr 2019 12:20:07 +1200 Subject: [PATCH] [enhance] #63 Add --quiet flag to supress console output in CLI --- bin/cli.js | 10 +++++++--- lib/helpers/parse-config.js | 1 + lib/helpers/success-handler.js | 2 +- lib/replace-in-file.js | 2 +- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/bin/cli.js b/bin/cli.js index e345495..cf51deb 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -24,7 +24,7 @@ const config = loadConfig(configFile); const options = combineConfig(config, argv); //Extract settings -const {from, to, files, isRegex, verbose} = options; +const {from, to, files, isRegex, verbose, quiet} = options; //Single star globs already get expanded in the command line options.files = files.reduce((files, file) => { @@ -44,12 +44,16 @@ if (isRegex) { } //Log -console.log(`Replacing '${from}' with '${to}'`); +if (!quiet) { + console.log(`Replacing '${from}' with '${to}'`); +} //Replace try { const results = replace.sync(options); - successHandler(results, verbose); + if (!quiet) { + successHandler(results, verbose); + } } catch (error) { errorHandler(error); diff --git a/lib/helpers/parse-config.js b/lib/helpers/parse-config.js index 8316ab3..41d87a1 100644 --- a/lib/helpers/parse-config.js +++ b/lib/helpers/parse-config.js @@ -11,6 +11,7 @@ const defaults = { countMatches: false, isRegex: false, verbose: false, + quiet: false, dry: false, glob: {}, cwd: null, diff --git a/lib/helpers/success-handler.js b/lib/helpers/success-handler.js index 28ee3bc..b76e66d 100644 --- a/lib/helpers/success-handler.js +++ b/lib/helpers/success-handler.js @@ -8,7 +8,7 @@ const chalk = require('chalk'); /** * Success handler */ -module.exports = function successHandler(changes, verbose = false) { +module.exports = function successHandler(changes, verbose) { if (changes.length > 0) { console.log(chalk.green(changes.length, 'file(s) were changed')); if (verbose) { diff --git a/lib/replace-in-file.js b/lib/replace-in-file.js index e104adb..afbe503 100644 --- a/lib/replace-in-file.js +++ b/lib/replace-in-file.js @@ -77,7 +77,7 @@ replaceInFile.sync = function(config) { //Dry run? //istanbul ignore if: No need to test console logs if (dry && verbose) { - console.log(chalk.yellow('Dry run, not making any changes')); + console.log(chalk.yellow('Dry run, not making actual changes')); } //Process synchronously and return results