replace-in-file icon indicating copy to clipboard operation
replace-in-file copied to clipboard

countMatches not working with async replace & processor

Open elijaholmos opened this issue 3 years ago • 10 comments

My implementation is as follows:

import replace from "replace-in-file";

const results = await replace.replaceInFile({
    files: './src/main/resources/templates/*.html',
    countMatches: true,
    dry: true,
    processor: (input) => 
        input.split('\r\n').map(line => {
            //add th reference
            if(line.includes('<html>'))
                line = line.replace('<html>', '<html xmlns:th="http://www.thymeleaf.org">');
            //non html hrefs
            if(line.includes(`href="assets`)) {
                const path = line.split('href="').pop().split(`"`).shift();
                line = line.replace('href', 'th:href').replace(path, `@{/${path.replace('.html', '')}}`)
            }
            //src attrs
            if(line.includes(`src="assets`)) {
                const path = line.split('src="').pop().split(`"`).shift();
                line = line.replace('src', 'th:src').replace(path, `@{/${path}}`)
            }
            return line;
        }).join('\r\n'),
});

console.log(results);

I have tried both a dry and wet run, but the results array only includes objects with file and hasChanged, no numMatches or numReplacements. As a side note, I tried a synchronous operation but that failed entirely. Something related to ReferenceError: processFileSync is not defined. I can provide more info on that if requested.

Node v16.6.0 NPM v7.19.1 replace-in-file v6.31

elijaholmos avatar Oct 22 '21 05:10 elijaholmos

This is correct, processors were implemented very recently in #142 by @DVLP , and I don't believe this supports those output parameters yet.

@DVLP could you look into the ReferenceError?

adamreisnz avatar Oct 22 '21 07:10 adamreisnz

Hi @adamreisnz

Thanks for the awesome lib, I'm getting reference error as well.

I checked the source and it found this line is the culprit. https://github.com/adamreisnz/replace-in-file/blob/0fbe9e9f747d183f4c51a1977803ef378956086b/lib/replace-in-file.js#L78

It should rather be return processFile.processFileSync(config, cb) , now I'm not sure about cb argument and what it's meant for, it causes an undefined error.

I'm happy to contribute a fix

kornect avatar Oct 22 '21 13:10 kornect

processFileSync was an error. Here's a fix https://github.com/adamreisnz/replace-in-file/pull/148

DVLP avatar Oct 22 '21 14:10 DVLP

Thanks @DVLP, fix has been released as 6.1.1

adamreisnz avatar Oct 22 '21 19:10 adamreisnz

Hey, thanks for the quick responses. I cleared npm cache & clean installed 6.3.2, but there seems to be no difference in the output. My code remains the exact same as above and the results array still only includes objects with file and hasChanged properties. I also tried the synchronous version again, this time yielding a different error:

C:\[redacted]\node_modules\replace-in-file\lib\replace-in-file.js:78
    return processFile.processFileSync(config, cb)
                                               ^

ReferenceError: cb is not defined
    at Function.replaceInFileSync (C:\[redacted]\node_modules\replace-in-file\lib\replace-in-file.js:78:48)

elijaholmos avatar Oct 23 '21 16:10 elijaholmos

6.3.2 fixed the reference error, it didn't add the change count feature. That only works without custom processing for now.

On Sun, 24 Oct 2021, 05:38 elijaholmos, @.***> wrote:

Hey, thanks for the quick responses. I cleared npm cache & clean installed 6.3.2, but there seems to be no difference in the output. My code remains the exact same as above and the results array still only includes objects with file and hasChanged properties. I also tried the synchronous version again, this time yielding a different error:

C:[redacted]\node_modules\replace-in-file\lib\replace-in-file.js:78 return processFile.processFileSync(config, cb) ^ ReferenceError: cb is not defined at Function.replaceInFileSync (C:[redacted]\node_modules\replace-in-file\lib\replace-in-file.js:78:48)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/adamreisnz/replace-in-file/issues/147#issuecomment-950178358, or unsubscribe https://github.com/notifications/unsubscribe-auth/AADXYQQLOFKRU3VLAKF5BEDUILQIVANCNFSM5GPXPB7A . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

adamreisnz avatar Oct 23 '21 19:10 adamreisnz

6.3.2 have same error

https://github.com/adamreisnz/replace-in-file/blob/abea7cbbaf250a52d2488ebcc487dbeb483e24de/lib/replace-in-file.js#L78

sokolovstas avatar Oct 29 '21 09:10 sokolovstas

Sorry I don't see what is wrong with that line, other than it has a superfluous callback param.

adamreisnz avatar Oct 29 '21 18:10 adamreisnz

So I can chime in and say I am getting a ReferenceError due to the cb param. See my terminal output: Screen Shot 2021-11-04 at 12 28 31 PM

I am using the replaceInFileSync() function with the files param and the processor function param.

I am running it in a typescript project and have to tell the TS processor to ignore the processor prop or it gets angry since the processor prop doesn't exist on the ReplaceInFileConfig interface.

m4ttheweric avatar Nov 04 '21 19:11 m4ttheweric

As a follow up, if i use replaceInFile() rather than replaceInFileSync() the error does not occur.

m4ttheweric avatar Nov 04 '21 19:11 m4ttheweric

This should be fixed

adamreisnz avatar May 28 '23 10:05 adamreisnz