From a0045eae5d930c116d86b15c00e93eefa531544c Mon Sep 17 00:00:00 2001 From: Greg Magolan Date: Thu, 14 Sep 2017 14:30:53 -0700 Subject: [PATCH 1/2] Pass filename as last arg to replacer function (#34) * Pass filename as last arg to replacer function * - Cleaned up replacer function override to use ES6 - Added replacer function tests to Async callback & Sync specs --- lib/helpers/make-replacements.js | 8 ++- lib/helpers/replace-async.js | 2 +- lib/helpers/replace-sync.js | 2 +- lib/replace-in-file.spec.js | 104 +++++++++++++++++++++++++++++++ 4 files changed, 112 insertions(+), 4 deletions(-) diff --git a/lib/helpers/make-replacements.js b/lib/helpers/make-replacements.js index b794298..74334c4 100644 --- a/lib/helpers/make-replacements.js +++ b/lib/helpers/make-replacements.js @@ -16,7 +16,7 @@ function getReplacement(replace, isArray, i) { /** * Helper to make replacements */ -module.exports = function makeReplacements(contents, from, to) { +module.exports = function makeReplacements(contents, from, to, file) { //Turn into array if (!Array.isArray(from)) { @@ -30,10 +30,14 @@ module.exports = function makeReplacements(contents, from, to) { from.forEach((item, i) => { //Get replacement value - const replacement = getReplacement(to, isArray, i); + let replacement = getReplacement(to, isArray, i); if (replacement === null) { return; } + if (typeof replacement === 'function') { + const original = replacement; + replacement = (...args) => original(...args, file); + } //Make replacement contents = contents.replace(item, replacement); diff --git a/lib/helpers/replace-async.js b/lib/helpers/replace-async.js index b4d426d..8ef64c9 100644 --- a/lib/helpers/replace-async.js +++ b/lib/helpers/replace-async.js @@ -18,7 +18,7 @@ module.exports = function replaceAsync(file, from, to, enc) { } //Replace contents and check if anything changed - let newContents = makeReplacements(contents, from, to); + let newContents = makeReplacements(contents, from, to, file); if (newContents === contents) { return resolve({file, hasChanged: false}); } diff --git a/lib/helpers/replace-sync.js b/lib/helpers/replace-sync.js index c97fb3a..aa298ff 100644 --- a/lib/helpers/replace-sync.js +++ b/lib/helpers/replace-sync.js @@ -15,7 +15,7 @@ module.exports = function replaceSync(file, from, to, enc) { const contents = fs.readFileSync(file, enc); //Replace contents and check if anything changed - const newContents = makeReplacements(contents, from, to); + const newContents = makeReplacements(contents, from, to, file); if (newContents === contents) { return false; } diff --git a/lib/replace-in-file.spec.js b/lib/replace-in-file.spec.js index 67a49bf..6b662ee 100644 --- a/lib/replace-in-file.spec.js +++ b/lib/replace-in-file.spec.js @@ -82,6 +82,25 @@ describe('Replace in file', () => { }); }); + it('should pass the match as first arg and file as last arg to a replacer function replace contents in a single file with regex', done => { + replace({ + files: 'test1', + from: /re\splace/g, + to: (match, ...args) => { + const file = args.pop(); + expect(match).to.equal('re place'); + expect(file).to.equal('test1'); + return 'b'; + } + }).then(() => { + const test1 = fs.readFileSync('test1', 'utf8'); + const test2 = fs.readFileSync('test2', 'utf8'); + expect(test1).to.equal('a b c'); + expect(test2).to.equal(testData); + done(); + }); + }); + it('should replace contents with a string replacement', done => { replace({ files: 'test1', @@ -94,6 +113,23 @@ describe('Replace in file', () => { }); }); + it('should pass the match as first arg and file as last arg to a replacer function and replace contents with a string replacement', done => { + replace({ + files: 'test1', + from: 're place', + to: (match, ...args) => { + const file = args.pop(); + expect(match).to.equal('re place'); + expect(file).to.equal('test1'); + return 'b'; + } + }).then(() => { + const test1 = fs.readFileSync('test1', 'utf8'); + expect(test1).to.equal('a b c'); + done(); + }); + }); + it('should replace contents in a an array of files', done => { replace({ files: ['test1', 'test2'], @@ -327,6 +363,25 @@ describe('Replace in file', () => { }); }); + it('should pass the match as first arg and file as last arg to a replacer function replace contents in a single file with regex', done => { + replace({ + files: 'test1', + from: /re\splace/g, + to: (match, ...args) => { + const file = args.pop(); + expect(match).to.equal('re place'); + expect(file).to.equal('test1'); + return 'b'; + } + }, () => { + const test1 = fs.readFileSync('test1', 'utf8'); + const test2 = fs.readFileSync('test2', 'utf8'); + expect(test1).to.equal('a b c'); + expect(test2).to.equal(testData); + done(); + }); + }); + it('should replace contents with a string replacement', done => { replace({ files: 'test1', @@ -339,6 +394,23 @@ describe('Replace in file', () => { }); }); + it('should pass the match as first arg and file as last arg to a replacer function and replace contents with a string replacement', done => { + replace({ + files: 'test1', + from: 're place', + to: (match, ...args) => { + const file = args.pop(); + expect(match).to.equal('re place'); + expect(file).to.equal('test1'); + return 'b'; + } + }, () => { + const test1 = fs.readFileSync('test1', 'utf8'); + expect(test1).to.equal('a b c'); + done(); + }); + }); + it('should replace contents in a an array of files', done => { replace({ files: ['test1', 'test2'], @@ -607,6 +679,23 @@ describe('Replace in file', () => { expect(test2).to.equal(testData); }); + it('should pass the match as first arg and file as last arg to a replacer function replace contents in a single file with regex', function() { + replace.sync({ + files: 'test1', + from: /re\splace/g, + to: (match, ...args) => { + const file = args.pop(); + expect(match).to.equal('re place'); + expect(file).to.equal('test1'); + return 'b'; + } + }); + const test1 = fs.readFileSync('test1', 'utf8'); + const test2 = fs.readFileSync('test2', 'utf8'); + expect(test1).to.equal('a b c'); + expect(test2).to.equal(testData); + }); + it('should replace contents with a string replacement', function() { replace.sync({ files: 'test1', @@ -617,6 +706,21 @@ describe('Replace in file', () => { expect(test1).to.equal('a b c'); }); + it('should pass the match as first arg and file as last arg to a replacer function and replace contents with a string replacement', function() { + replace.sync({ + files: 'test1', + from: 're place', + to: (match, ...args) => { + const file = args.pop(); + expect(match).to.equal('re place'); + expect(file).to.equal('test1'); + return 'b'; + } + }); + const test1 = fs.readFileSync('test1', 'utf8'); + expect(test1).to.equal('a b c'); + }); + it('should replace contents in a an array of files', function() { replace.sync({ files: ['test1', 'test2'], From 489fed8c03d290525f1e6bc5a8a1160ec9393ad3 Mon Sep 17 00:00:00 2001 From: Adam Reis Date: Fri, 15 Sep 2017 09:32:03 +1200 Subject: [PATCH 2/2] 3.0.0-beta.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a850931..a46f7ea 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "replace-in-file", - "version": "3.0.0-beta.1", + "version": "3.0.0-beta.2", "description": "A simple utility to quickly replace text in one or more files.", "homepage": "https://github.com/adamreisnz/replace-in-file#readme", "author": {