[enhance] #63 Add --quiet flag to supress console output in CLI

This commit is contained in:
Adam Reis
2019-04-21 12:20:07 +12:00
parent 3eba177261
commit 32df0adcaa
4 changed files with 10 additions and 5 deletions

View File

@@ -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);

View File

@@ -11,6 +11,7 @@ const defaults = {
countMatches: false,
isRegex: false,
verbose: false,
quiet: false,
dry: false,
glob: {},
cwd: null,

View File

@@ -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) {

View File

@@ -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