From ff89d44361fabce6b7b6e890f7abe3dd13e216f0 Mon Sep 17 00:00:00 2001 From: Kevin Sommer Date: Wed, 7 Feb 2024 22:33:20 +0100 Subject: [PATCH 1/7] Custom processor with additional argument which receives the file being processed --- README.md | 8 ++++++++ lib/helpers/run-processors.js | 3 +++ lib/process-file.spec.js | 19 +++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/README.md b/README.md index ddc3e55..94b466f 100644 --- a/README.md +++ b/README.md @@ -205,6 +205,14 @@ const results = replace.sync({ processor: (input) => input.replace(/foo/g, 'bar'), }); ``` +It's also possible to use a custom processor with two arguments. In this case the first argument of the callback is still the content to be processed. The second one receives the file being processed. + +```js +const results = replace.sync({ + files: 'path/to/files/*.html', + processor: (input, file) => input.replace(/foo/g, 'bar') + file, +}); +``` ### Array of custom processors diff --git a/lib/helpers/run-processors.js b/lib/helpers/run-processors.js index d87d635..3fd993f 100644 --- a/lib/helpers/run-processors.js +++ b/lib/helpers/run-processors.js @@ -6,6 +6,9 @@ module.exports = function runProcessors(contents, processor, file) { const result = {file}; const newContents = processors.reduce((contents, processor) => { + if (processor.length === 2) { + return processor(contents, file); + } return processor(contents); }, contents); diff --git a/lib/process-file.spec.js b/lib/process-file.spec.js index 99505db..da8fb37 100644 --- a/lib/process-file.spec.js +++ b/lib/process-file.spec.js @@ -42,6 +42,13 @@ describe('Process a file', () => { return config; } + function appendFileProcessor(config) { + config.processor = (content, file) => { + return content + file; + }; + return config; + } + /** * Async with promises */ @@ -754,6 +761,18 @@ describe('Process a file', () => { expect(results[2].hasChanged).to.equal(false); }); + it('should pass filename to processor as second parameter', () => { + transform.sync(appendFileProcessor({ + files: 'test*', + })); + const test1 = fs.readFileSync('test1', 'utf8'); + const test2 = fs.readFileSync('test2', 'utf8'); + const test3 = fs.readFileSync('test3', 'utf8'); + expect(test1).to.equal(testData + 'test1'); + expect(test2).to.equal(testData + 'test2'); + expect(test3).to.equal( 'nopetest3'); + }); + describe('fs', () => { it('reads and writes using a custom fs when provided', done => { const before = 'a'; From 275fc719a5d1eeb920b746fa0bc159f0f64acaf5 Mon Sep 17 00:00:00 2001 From: ITDancer13 Date: Tue, 13 Feb 2024 08:37:28 +0100 Subject: [PATCH 2/7] Update lib/process-file.spec.js Co-authored-by: Adam Reis --- lib/process-file.spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/process-file.spec.js b/lib/process-file.spec.js index da8fb37..d760aa7 100644 --- a/lib/process-file.spec.js +++ b/lib/process-file.spec.js @@ -768,8 +768,8 @@ describe('Process a file', () => { const test1 = fs.readFileSync('test1', 'utf8'); const test2 = fs.readFileSync('test2', 'utf8'); const test3 = fs.readFileSync('test3', 'utf8'); - expect(test1).to.equal(testData + 'test1'); - expect(test2).to.equal(testData + 'test2'); + expect(test1).to.equal(`${testData}test1`); + expect(test2).to.equal(`${testData}test2`); expect(test3).to.equal( 'nopetest3'); }); From c3c7f356668b4c3b1313ad03e62fece8286aaa24 Mon Sep 17 00:00:00 2001 From: ITDancer13 Date: Tue, 13 Feb 2024 08:37:50 +0100 Subject: [PATCH 3/7] Update lib/process-file.spec.js Co-authored-by: Adam Reis --- lib/process-file.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/process-file.spec.js b/lib/process-file.spec.js index d760aa7..2245eb7 100644 --- a/lib/process-file.spec.js +++ b/lib/process-file.spec.js @@ -44,7 +44,7 @@ describe('Process a file', () => { function appendFileProcessor(config) { config.processor = (content, file) => { - return content + file; + return `${content}${file}`; }; return config; } From 27eff716494d309b6d8cee91788def906dc1d1ba Mon Sep 17 00:00:00 2001 From: ITDancer13 Date: Tue, 13 Feb 2024 08:39:27 +0100 Subject: [PATCH 4/7] Update lib/helpers/run-processors.js Co-authored-by: Adam Reis --- lib/helpers/run-processors.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/helpers/run-processors.js b/lib/helpers/run-processors.js index 3fd993f..cde5196 100644 --- a/lib/helpers/run-processors.js +++ b/lib/helpers/run-processors.js @@ -6,10 +6,7 @@ module.exports = function runProcessors(contents, processor, file) { const result = {file}; const newContents = processors.reduce((contents, processor) => { - if (processor.length === 2) { - return processor(contents, file); - } - return processor(contents); + return processor(contents, file); }, contents); result.hasChanged = (newContents !== contents); From 2a2f4c5411f9dc56051b44c52f4828abcd69aa2e Mon Sep 17 00:00:00 2001 From: ITDancer13 Date: Tue, 13 Feb 2024 08:39:50 +0100 Subject: [PATCH 5/7] Update README.md Co-authored-by: Adam Reis --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 94b466f..ab3a8c7 100644 --- a/README.md +++ b/README.md @@ -205,7 +205,7 @@ const results = replace.sync({ processor: (input) => input.replace(/foo/g, 'bar'), }); ``` -It's also possible to use a custom processor with two arguments. In this case the first argument of the callback is still the content to be processed. The second one receives the file being processed. +The custom processor will receive the path of the file being processed as a second parameter: ```js const results = replace.sync({ From dcf6d728051ad1f4bc2b034ef02c38bc8f630f17 Mon Sep 17 00:00:00 2001 From: ITDancer13 Date: Tue, 13 Feb 2024 08:39:56 +0100 Subject: [PATCH 6/7] Update README.md Co-authored-by: Adam Reis --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ab3a8c7..3327c0f 100644 --- a/README.md +++ b/README.md @@ -210,7 +210,7 @@ The custom processor will receive the path of the file being processed as a seco ```js const results = replace.sync({ files: 'path/to/files/*.html', - processor: (input, file) => input.replace(/foo/g, 'bar') + file, + processor: (input, file) => input.replace(/foo/g, file), }); ``` From bb33bdb938e2924e7996c63587935c88518a086d Mon Sep 17 00:00:00 2001 From: ITDancer13 Date: Tue, 13 Feb 2024 08:44:47 +0100 Subject: [PATCH 7/7] cleanup --- lib/process-file.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/process-file.spec.js b/lib/process-file.spec.js index 2245eb7..70220e4 100644 --- a/lib/process-file.spec.js +++ b/lib/process-file.spec.js @@ -770,7 +770,7 @@ describe('Process a file', () => { const test3 = fs.readFileSync('test3', 'utf8'); expect(test1).to.equal(`${testData}test1`); expect(test2).to.equal(`${testData}test2`); - expect(test3).to.equal( 'nopetest3'); + expect(test3).to.equal('nopetest3'); }); describe('fs', () => {