Add verbose option

This commit is contained in:
Adam Reis
2018-03-07 08:41:45 +13:00
parent 153dc27b05
commit 60e7b82f61
7 changed files with 103 additions and 10 deletions

View File

@@ -13,7 +13,7 @@ module.exports = function combineConfig(config, argv) {
//Extract options from config
let {
from, to, files, ignore, encoding, verbose,
allowEmptyPaths, disableGlobs, isRegex,
allowEmptyPaths, disableGlobs, isRegex, dry,
} = config;
//Get from/to parameters from CLI args if not defined in options
@@ -45,10 +45,13 @@ module.exports = function combineConfig(config, argv) {
if (typeof verbose === 'undefined') {
verbose = !!argv.verbose;
}
if (typeof dry === 'undefined') {
dry = !!argv.dry;
}
//Return through parser to validate
return parseConfig({
from, to, files, ignore, encoding, verbose,
allowEmptyPaths, disableGlobs, isRegex,
allowEmptyPaths, disableGlobs, isRegex, dry,
});
};

View File

@@ -10,6 +10,7 @@ const defaults = {
allowEmptyPaths: false,
isRegex: false,
verbose: false,
dry: false,
};
/**

View File

@@ -9,7 +9,7 @@ const makeReplacements = require('./make-replacements');
/**
* Helper to replace in a single file (async)
*/
module.exports = function replaceAsync(file, from, to, enc) {
module.exports = function replaceAsync(file, from, to, enc, dry) {
return new Promise((resolve, reject) => {
fs.readFile(file, enc, (error, contents) => {
//istanbul ignore if
@@ -23,6 +23,11 @@ module.exports = function replaceAsync(file, from, to, enc) {
return resolve({file, hasChanged: false});
}
//Dry run, resolve
if (dry) {
return resolve({file, hasChanged: true});
}
//Write to file
fs.writeFile(file, newContents, enc, error => {
//istanbul ignore if

View File

@@ -9,7 +9,7 @@ const makeReplacements = require('./make-replacements');
/**
* Helper to replace in a single file (sync)
*/
module.exports = function replaceSync(file, from, to, enc) {
module.exports = function replaceSync(file, from, to, enc, dry) {
//Read contents
const contents = fs.readFileSync(file, enc);
@@ -20,6 +20,11 @@ module.exports = function replaceSync(file, from, to, enc) {
return false;
}
//Dry run?
if (dry) {
return true;
}
//Write to file
fs.writeFileSync(file, newContents, enc);
return true;

View File

@@ -3,6 +3,7 @@
/**
* Dependencies
*/
const chalk = require('chalk');
const parseConfig = require('./helpers/parse-config');
const getPathsSync = require('./helpers/get-paths-sync');
const getPathsAsync = require('./helpers/get-paths-async');
@@ -28,14 +29,20 @@ function replaceInFile(config, cb) {
//Get config
const {
files, from, to, encoding, ignore, allowEmptyPaths, disableGlobs,
dry, verbose,
} = config;
//Dry run?
if (dry && verbose) {
console.log(chalk.yellow('Dry run, not making actual changes'));
}
//Find paths
return getPathsAsync(files, ignore, disableGlobs, allowEmptyPaths)
//Make replacements
.then(paths => Promise.all(paths.map(file => {
return replaceAsync(file, from, to, encoding);
return replaceAsync(file, from, to, encoding, dry);
})))
//Convert results to array of changed files
@@ -73,13 +80,20 @@ replaceInFile.sync = function(config) {
config = parseConfig(config);
//Get config, paths, and initialize changed files
const {files, from, to, encoding, ignore, disableGlobs} = config;
const {
files, from, to, encoding, ignore, disableGlobs, dry, verbose,
} = config;
const paths = getPathsSync(files, ignore, disableGlobs);
const changedFiles = [];
//Dry run?
if (dry && verbose) {
console.log(chalk.yellow('Dry run, not making any changes'));
}
//Process synchronously
paths.forEach(path => {
if (replaceSync(path, from, to, encoding)) {
if (replaceSync(path, from, to, encoding, dry)) {
changedFiles.push(path);
}
});

View File

@@ -298,6 +298,35 @@ describe('Replace in file', () => {
done();
});
});
it('should not replace in a dry run', done => {
replace({
files: ['test1', 'test2'],
from: /re\splace/g,
to: 'b',
dry: true,
}).then(() => {
const test1 = fs.readFileSync('test1', 'utf8');
const test2 = fs.readFileSync('test2', 'utf8');
expect(test1).to.equal('a re place c');
expect(test2).to.equal('a re place c');
done();
});
});
it('should return changed files for a dry run', done => {
replace({
files: ['test1', 'test2', 'test3'],
from: /re\splace/g,
to: 'b',
dry: true,
}).then(changedFiles => {
expect(changedFiles).to.have.length(2);
expect(changedFiles).to.contain('test1');
expect(changedFiles).to.contain('test2');
done();
});
});
});
/**
@@ -871,5 +900,30 @@ describe('Replace in file', () => {
expect(test1).to.equal('a b c');
expect(test2).to.equal('a b c');
});
it('should not replace in a dry run', () => {
replace.sync({
files: ['test1', 'test2'],
from: /re\splace/g,
to: 'b',
dry: true,
});
const test1 = fs.readFileSync('test1', 'utf8');
const test2 = fs.readFileSync('test2', 'utf8');
expect(test1).to.equal('a re place c');
expect(test2).to.equal('a re place c');
});
it('should return changed files for a dry run', () => {
const changedFiles = replace.sync({
files: ['test1', 'test2', 'test3'],
from: /re\splace/g,
to: 'b',
dry: true,
});
expect(changedFiles).to.have.length(2);
expect(changedFiles).to.contain('test1');
expect(changedFiles).to.contain('test2');
});
});
});