replace-in-file
Advanced tools
Comparing version 3.0.0-beta.1 to 3.0.0-beta.2
@@ -19,3 +19,3 @@ 'use strict'; | ||
*/ | ||
module.exports = function makeReplacements(contents, from, to) { | ||
module.exports = function makeReplacements(contents, from, to, file) { | ||
@@ -34,6 +34,10 @@ //Turn into array | ||
//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); | ||
} | ||
@@ -40,0 +44,0 @@ //Make replacement |
@@ -21,3 +21,3 @@ 'use strict'; | ||
//Replace contents and check if anything changed | ||
let newContents = makeReplacements(contents, from, to); | ||
let newContents = makeReplacements(contents, from, to, file); | ||
if (newContents === contents) { | ||
@@ -24,0 +24,0 @@ return resolve({file, hasChanged: false}); |
@@ -18,3 +18,3 @@ 'use strict'; | ||
//Replace contents and check if anything changed | ||
const newContents = makeReplacements(contents, from, to); | ||
const newContents = makeReplacements(contents, from, to, file); | ||
if (newContents === contents) { | ||
@@ -21,0 +21,0 @@ return false; |
@@ -85,2 +85,21 @@ 'use strict'; | ||
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 => { | ||
@@ -98,2 +117,19 @@ replace({ | ||
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 => { | ||
@@ -332,2 +368,21 @@ replace({ | ||
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 => { | ||
@@ -345,2 +400,19 @@ replace({ | ||
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 => { | ||
@@ -614,2 +686,19 @@ replace({ | ||
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() { | ||
@@ -625,2 +714,17 @@ replace.sync({ | ||
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() { | ||
@@ -627,0 +731,0 @@ replace.sync({ |
{ | ||
"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.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/adamreisnz/replace-in-file#readme", |
148540
1233
24