Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions lib/replace-in-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -95,10 +95,18 @@ 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 = function() {
const args = [].slice.call(arguments);
args.splice(1, 0, file);
return original.apply(null, args);
};
}

//Make replacement
contents = contents.replace(item, replacement);
Expand All @@ -117,7 +125,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;
}
Expand All @@ -139,7 +147,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});
}
Expand Down