Skip to content

Commit

Permalink
Pass filename as last arg to replacer function (#34)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
gregmagolan authored and adamreisnz committed Sep 14, 2017
1 parent 1f5bced commit a0045ea
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 4 deletions.
8 changes: 6 additions & 2 deletions lib/helpers/make-replacements.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion lib/helpers/replace-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});
}
Expand Down
2 changes: 1 addition & 1 deletion lib/helpers/replace-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
104 changes: 104 additions & 0 deletions lib/replace-in-file.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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'],
Expand Down Expand Up @@ -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',
Expand All @@ -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'],
Expand Down Expand Up @@ -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',
Expand All @@ -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'],
Expand Down

0 comments on commit a0045ea

Please sign in to comment.