From 2f65be73dcc678c34c2583189e19ccd2fe4bdaeb Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Tue, 22 Aug 2017 12:17:46 -0500 Subject: [PATCH 1/3] Added ignore option --- README.md | 13 ++++++++++++- bin/cli.js | 23 +++++++++++++++++++---- lib/replace-in-file.js | 17 +++++++++++------ lib/replace-in-file.spec.js | 30 ++++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index ad968c8..e5fbfe9 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,17 @@ const options = { //Character encoding for reading/writing files (defaults to utf-8) encoding: 'utf8', + + //Single file or glob to ignore + ignore: 'path/to/ignored/file', + + //Multiple files or globs to ignore + ignore: [ + 'path/to/ignored/file', + 'path/to/other/ignored_file', + 'path/to/ignored_files/*.html', + 'another/**/*.ignore', + ] }; ``` @@ -122,7 +133,7 @@ const changedFiles = replace.sync({ ### CLI usage ```sh -replace-in-file from to some/file.js,some/**/glob.js [--isRegex] +replace-in-file from to some/file.js,some/**/glob.js ignore/files.js,ignore/**/glob.js [--isRegex] ``` The flags `allowEmptyPaths` and `encoding` are supported in the CLI. diff --git a/bin/cli.js b/bin/cli.js index e7320cf..0f8963e 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -16,7 +16,7 @@ if (argv._.length < 3 && !argv.config) { } //Prepare vars -let from, to, files; +let from, to, files, ignore; //If config is set, load config file if (argv.config) { @@ -41,19 +41,30 @@ if (argv.config) { config.files = [config.files]; } files = config.files; + + //Set ignore param + if (typeof config.ignore === 'string') { + config.ignore = [config.ignore]; + } + ignore = config.ignore; } //Get from/to parameters from CLI args if not defined in config file if (typeof from === 'undefined') { - from = argv._.shift(); + from = argv._[0]; } if (typeof to === 'undefined') { - to = argv._.shift(); + to = argv._[1]; } //Get files if (!files) { - files = argv._; + files = argv._[2]; +} + +//Get ignore +if (!ignore) { + ignore = argv._[3]; } //Validate data @@ -71,6 +82,10 @@ files = files.reduce((files, file) => { return files.concat(file.split(',')); }, []); +ignore = ignore.reduce((files, file) => { + return files.concat(file.split(',')); +}, []); + //If the isRegex flag is passed, send the from parameter as a RegExp object if (argv.isRegex) { const flags = from.replace(/.*\/([gimy]*)$/, '$1'); diff --git a/lib/replace-in-file.js b/lib/replace-in-file.js index f2e7efc..2e88af1 100644 --- a/lib/replace-in-file.js +++ b/lib/replace-in-file.js @@ -13,6 +13,7 @@ const chalk = require('chalk'); const defaults = { allowEmptyPaths: false, encoding: 'utf-8', + ignore: [] }; /** @@ -155,9 +156,9 @@ function replaceAsync(file, from, to, enc) { /** * Promise wrapper for glob */ -function globPromise(pattern, allowEmptyPaths) { +function globPromise(pattern, ignore, allowEmptyPaths) { return new Promise((resolve, reject) => { - glob(pattern, {nodir: true}, (error, files) => { + glob(pattern, {ignore: ignore, nodir: true}, (error, files) => { //istanbul ignore if: hard to make glob error if (error) { @@ -192,17 +193,19 @@ function replaceInFile(config, cb) { } //Get config and globs - // const {files, from, to, allowEmptyPaths, encoding} = config; + // const {files, from, to, allowEmptyPaths, encoding, ignore} = config; const files = config.files; const from = config.from; const to = config.to; const encoding = config.encoding; const allowEmptyPaths = config.allowEmptyPaths; + const ignore = config.ignore; const globs = Array.isArray(files) ? files : [files]; + const ignoreGlobs = Array.isArray(ignore) ? ignore : [ignore]; //Find files return Promise - .all(globs.map(pattern => globPromise(pattern, allowEmptyPaths))) + .all(globs.map(pattern => globPromise(pattern, ignoreGlobs, allowEmptyPaths))) //Flatten array .then(files => [].concat.apply([], files)) @@ -247,18 +250,20 @@ replaceInFile.sync = function(config) { config = parseConfig(config); //Get config and globs - // const {files, from, to, encoding} = config; + // const {files, from, to, encoding, ignore} = config; const files = config.files; const from = config.from; const to = config.to; const encoding = config.encoding; + const ignore = config.ignore; const globs = Array.isArray(files) ? files : [files]; + const ignoreGlobs = Array.isArray(ignore) ? ignore : [ignore]; const changedFiles = []; //Process synchronously globs.forEach(pattern => { glob - .sync(pattern, {nodir: true}) + .sync(pattern, {ignore: ignoreGlobs, nodir: true}) .forEach(file => { if (replaceSync(file, from, to, encoding)) { changedFiles.push(file); diff --git a/lib/replace-in-file.spec.js b/lib/replace-in-file.spec.js index 35f16c2..19ac518 100644 --- a/lib/replace-in-file.spec.js +++ b/lib/replace-in-file.spec.js @@ -122,6 +122,21 @@ describe('Replace in file', () => { }); }); + it('should expand globs while excluding ignored files', done => { + replace({ + files: 'test*', + ignore: 'test1', + from: /re\splace/g, + to: 'b', + }).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 b c'); + done(); + }); + }); + it('should replace substrings', done => { replace({ files: 'test1', @@ -352,6 +367,21 @@ describe('Replace in file', () => { }); }); + it('should expand globs while excluding ignored files', done => { + replace({ + files: 'test*', + ignore: 'test1', + from: /re\splace/g, + to: 'b', + }, () => { + 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 b c'); + done(); + }); + }); + it('should not return an error on success', done => { replace({ files: 'test1', From 75ffb2563ed88ecbd4bc4a0d26dea7b731af2a3d Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Thu, 24 Aug 2017 10:54:18 -0500 Subject: [PATCH 2/3] Switch ignore parameter to a flag in the CLI tool --- README.md | 2 +- bin/cli.js | 22 +++++++++------------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index e5fbfe9..ed9a3e6 100644 --- a/README.md +++ b/README.md @@ -133,7 +133,7 @@ const changedFiles = replace.sync({ ### CLI usage ```sh -replace-in-file from to some/file.js,some/**/glob.js ignore/files.js,ignore/**/glob.js [--isRegex] +replace-in-file from to some/file.js,some/**/glob.js --ignore=ignore/files.js,ignore/**/glob.js [--isRegex] ``` The flags `allowEmptyPaths` and `encoding` are supported in the CLI. diff --git a/bin/cli.js b/bin/cli.js index 0f8963e..8fb9cf2 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -51,20 +51,15 @@ if (argv.config) { //Get from/to parameters from CLI args if not defined in config file if (typeof from === 'undefined') { - from = argv._[0]; + from = argv._.shift(); } if (typeof to === 'undefined') { - to = argv._[1]; + to = argv._.shift(); } //Get files if (!files) { - files = argv._[2]; -} - -//Get ignore -if (!ignore) { - ignore = argv._[3]; + files = argv._; } //Validate data @@ -82,10 +77,6 @@ files = files.reduce((files, file) => { return files.concat(file.split(',')); }, []); -ignore = ignore.reduce((files, file) => { - return files.concat(file.split(',')); -}, []); - //If the isRegex flag is passed, send the from parameter as a RegExp object if (argv.isRegex) { const flags = from.replace(/.*\/([gimy]*)$/, '$1'); @@ -100,11 +91,16 @@ if (argv.isRegex) { } } +//Get ignored files from ignore flag +if (!ignore && typeof argv.ignore !== 'undefined') { + ignore = argv.ignore +} + //Log console.log(`Replacing '${from}' with '${to}'`); //Create options -const options = {files, from, to}; +const options = {files, from, to, ignore}; if (typeof argv.encoding !== 'undefined') { options.encoding = argv.encoding; } From 628492d945aef1d0e963c0e1c07620e4388a1545 Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Thu, 24 Aug 2017 11:00:01 -0500 Subject: [PATCH 3/3] Forgot a semicolon --- bin/cli.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/cli.js b/bin/cli.js index 8fb9cf2..46bef0e 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -93,7 +93,7 @@ if (argv.isRegex) { //Get ignored files from ignore flag if (!ignore && typeof argv.ignore !== 'undefined') { - ignore = argv.ignore + ignore = argv.ignore; } //Log