From dc9e4bc2c69c20fd3d5c4b162eb1fbf2347ca9a1 Mon Sep 17 00:00:00 2001 From: Greg Magolan Date: Wed, 13 Sep 2017 16:17:01 -0700 Subject: [PATCH 1/3] Pass the filename as the 2nd argument to the to function (shift remaining existing arguments by 1 to the right) --- lib/replace-in-file.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/replace-in-file.js b/lib/replace-in-file.js index f3b4439..9e5accb 100644 --- a/lib/replace-in-file.js +++ b/lib/replace-in-file.js @@ -81,7 +81,7 @@ function getReplacement(replace, isArray, i) { /** * Helper to make replacements */ -function makeReplacements(contents, from, to) { +function makeReplacements(contents, from, to, file) { //Turn into array if (!Array.isArray(from)) { @@ -101,7 +101,14 @@ function makeReplacements(contents, from, to) { } //Make replacement - contents = contents.replace(item, replacement); + contents = contents.replace(item, function() { + if (typeof replacement === 'function') { + const args = [].slice.call(arguments); + args.splice(1, 0, file); + return replacement.apply(null, args); + } + return replacement; + }); }); //Return modified contents @@ -117,7 +124,7 @@ 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; } @@ -139,7 +146,7 @@ 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}); } From 3472274dd894ed3a65352d9641c9f27fa2d51302 Mon Sep 17 00:00:00 2001 From: Greg Magolan Date: Wed, 13 Sep 2017 16:48:49 -0700 Subject: [PATCH 2/3] Fix for unit tests --- lib/replace-in-file.js | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/replace-in-file.js b/lib/replace-in-file.js index 9e5accb..4150bec 100644 --- a/lib/replace-in-file.js +++ b/lib/replace-in-file.js @@ -95,20 +95,23 @@ function makeReplacements(contents, from, to, file) { 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 = function() { + if (typeof replacement === 'function') { + const args = [].slice.call(arguments); + args.splice(1, 0, file); + return original.apply(null, args); + } + }; + } //Make replacement - contents = contents.replace(item, function() { - if (typeof replacement === 'function') { - const args = [].slice.call(arguments); - args.splice(1, 0, file); - return replacement.apply(null, args); - } - return replacement; - }); + contents = contents.replace(item, replacement); }); //Return modified contents From 5dc1d629ad003945b9935d489c815623f061cb09 Mon Sep 17 00:00:00 2001 From: Greg Magolan Date: Wed, 13 Sep 2017 17:14:15 -0700 Subject: [PATCH 3/3] Remove second unnecessary check of typeof replacement --- lib/replace-in-file.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/replace-in-file.js b/lib/replace-in-file.js index 4150bec..2300d4e 100644 --- a/lib/replace-in-file.js +++ b/lib/replace-in-file.js @@ -102,11 +102,9 @@ function makeReplacements(contents, from, to, file) { if (typeof replacement === 'function') { const original = replacement; replacement = function() { - if (typeof replacement === 'function') { - const args = [].slice.call(arguments); - args.splice(1, 0, file); - return original.apply(null, args); - } + const args = [].slice.call(arguments); + args.splice(1, 0, file); + return original.apply(null, args); }; }