mirror of
https://github.com/zhigang1992/replace-in-file.git
synced 2026-01-12 17:43:32 +08:00
Merge branch '3.0.0' of github.com:adamreisnz/replace-in-file into 3.0.0
This commit is contained in:
@@ -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)) {
|
||||
@@ -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);
|
||||
|
||||
@@ -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});
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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',
|
||||
@@ -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'],
|
||||
@@ -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',
|
||||
@@ -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'],
|
||||
@@ -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',
|
||||
@@ -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'],
|
||||
|
||||
Reference in New Issue
Block a user