mirror of
https://github.com/zhigang1992/replace-in-file.git
synced 2026-01-12 17:43:32 +08:00
[enhance] #63 Add --quiet flag to supress console output in CLI
This commit is contained in:
10
bin/cli.js
10
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);
|
||||
|
||||
@@ -11,6 +11,7 @@ const defaults = {
|
||||
countMatches: false,
|
||||
isRegex: false,
|
||||
verbose: false,
|
||||
quiet: false,
|
||||
dry: false,
|
||||
glob: {},
|
||||
cwd: null,
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user