Skip to content

Commit a0045ea

Browse files
gregmagolanadamreisnz
authored andcommitted
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
1 parent 1f5bced commit a0045ea

File tree

4 files changed

+112
-4
lines changed

4 files changed

+112
-4
lines changed

lib/helpers/make-replacements.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function getReplacement(replace, isArray, i) {
1616
/**
1717
* Helper to make replacements
1818
*/
19-
module.exports = function makeReplacements(contents, from, to) {
19+
module.exports = function makeReplacements(contents, from, to, file) {
2020

2121
//Turn into array
2222
if (!Array.isArray(from)) {
@@ -30,10 +30,14 @@ module.exports = function makeReplacements(contents, from, to) {
3030
from.forEach((item, i) => {
3131

3232
//Get replacement value
33-
const replacement = getReplacement(to, isArray, i);
33+
let replacement = getReplacement(to, isArray, i);
3434
if (replacement === null) {
3535
return;
3636
}
37+
if (typeof replacement === 'function') {
38+
const original = replacement;
39+
replacement = (...args) => original(...args, file);
40+
}
3741

3842
//Make replacement
3943
contents = contents.replace(item, replacement);

lib/helpers/replace-async.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module.exports = function replaceAsync(file, from, to, enc) {
1818
}
1919

2020
//Replace contents and check if anything changed
21-
let newContents = makeReplacements(contents, from, to);
21+
let newContents = makeReplacements(contents, from, to, file);
2222
if (newContents === contents) {
2323
return resolve({file, hasChanged: false});
2424
}

lib/helpers/replace-sync.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module.exports = function replaceSync(file, from, to, enc) {
1515
const contents = fs.readFileSync(file, enc);
1616

1717
//Replace contents and check if anything changed
18-
const newContents = makeReplacements(contents, from, to);
18+
const newContents = makeReplacements(contents, from, to, file);
1919
if (newContents === contents) {
2020
return false;
2121
}

lib/replace-in-file.spec.js

+104
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,25 @@ describe('Replace in file', () => {
8282
});
8383
});
8484

85+
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 => {
86+
replace({
87+
files: 'test1',
88+
from: /re\splace/g,
89+
to: (match, ...args) => {
90+
const file = args.pop();
91+
expect(match).to.equal('re place');
92+
expect(file).to.equal('test1');
93+
return 'b';
94+
}
95+
}).then(() => {
96+
const test1 = fs.readFileSync('test1', 'utf8');
97+
const test2 = fs.readFileSync('test2', 'utf8');
98+
expect(test1).to.equal('a b c');
99+
expect(test2).to.equal(testData);
100+
done();
101+
});
102+
});
103+
85104
it('should replace contents with a string replacement', done => {
86105
replace({
87106
files: 'test1',
@@ -94,6 +113,23 @@ describe('Replace in file', () => {
94113
});
95114
});
96115

116+
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 => {
117+
replace({
118+
files: 'test1',
119+
from: 're place',
120+
to: (match, ...args) => {
121+
const file = args.pop();
122+
expect(match).to.equal('re place');
123+
expect(file).to.equal('test1');
124+
return 'b';
125+
}
126+
}).then(() => {
127+
const test1 = fs.readFileSync('test1', 'utf8');
128+
expect(test1).to.equal('a b c');
129+
done();
130+
});
131+
});
132+
97133
it('should replace contents in a an array of files', done => {
98134
replace({
99135
files: ['test1', 'test2'],
@@ -327,6 +363,25 @@ describe('Replace in file', () => {
327363
});
328364
});
329365

366+
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 => {
367+
replace({
368+
files: 'test1',
369+
from: /re\splace/g,
370+
to: (match, ...args) => {
371+
const file = args.pop();
372+
expect(match).to.equal('re place');
373+
expect(file).to.equal('test1');
374+
return 'b';
375+
}
376+
}, () => {
377+
const test1 = fs.readFileSync('test1', 'utf8');
378+
const test2 = fs.readFileSync('test2', 'utf8');
379+
expect(test1).to.equal('a b c');
380+
expect(test2).to.equal(testData);
381+
done();
382+
});
383+
});
384+
330385
it('should replace contents with a string replacement', done => {
331386
replace({
332387
files: 'test1',
@@ -339,6 +394,23 @@ describe('Replace in file', () => {
339394
});
340395
});
341396

397+
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 => {
398+
replace({
399+
files: 'test1',
400+
from: 're place',
401+
to: (match, ...args) => {
402+
const file = args.pop();
403+
expect(match).to.equal('re place');
404+
expect(file).to.equal('test1');
405+
return 'b';
406+
}
407+
}, () => {
408+
const test1 = fs.readFileSync('test1', 'utf8');
409+
expect(test1).to.equal('a b c');
410+
done();
411+
});
412+
});
413+
342414
it('should replace contents in a an array of files', done => {
343415
replace({
344416
files: ['test1', 'test2'],
@@ -607,6 +679,23 @@ describe('Replace in file', () => {
607679
expect(test2).to.equal(testData);
608680
});
609681

682+
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() {
683+
replace.sync({
684+
files: 'test1',
685+
from: /re\splace/g,
686+
to: (match, ...args) => {
687+
const file = args.pop();
688+
expect(match).to.equal('re place');
689+
expect(file).to.equal('test1');
690+
return 'b';
691+
}
692+
});
693+
const test1 = fs.readFileSync('test1', 'utf8');
694+
const test2 = fs.readFileSync('test2', 'utf8');
695+
expect(test1).to.equal('a b c');
696+
expect(test2).to.equal(testData);
697+
});
698+
610699
it('should replace contents with a string replacement', function() {
611700
replace.sync({
612701
files: 'test1',
@@ -617,6 +706,21 @@ describe('Replace in file', () => {
617706
expect(test1).to.equal('a b c');
618707
});
619708

709+
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() {
710+
replace.sync({
711+
files: 'test1',
712+
from: 're place',
713+
to: (match, ...args) => {
714+
const file = args.pop();
715+
expect(match).to.equal('re place');
716+
expect(file).to.equal('test1');
717+
return 'b';
718+
}
719+
});
720+
const test1 = fs.readFileSync('test1', 'utf8');
721+
expect(test1).to.equal('a b c');
722+
});
723+
620724
it('should replace contents in a an array of files', function() {
621725
replace.sync({
622726
files: ['test1', 'test2'],

0 commit comments

Comments
 (0)