0

I'm using replace-in-file to find all references to "old/site/ and replace with File_Path.

File_Path is a variable that is declared globaly.

Here's is my config

let FILE_PATH;

const replace = require('replace-in-file');

const options = {

    // file to update
    files: './output.js',

    // replacement to make (string or regex) 
    from: "old/site/",
    to: "FILE_PATH+",
};

// asynchronous replacement with promises:
replace(options)
    .then(changedFiles => {
        console.log('Modified files:', changedFiles.join(', '));
    })
    .catch(error => {
        console.error('Error occurred:', error);
    });

So as you can see 'to' is set to "FILE_PATH+" which gives the result

"FILE_PATH+childurl"

The result I am looking for though is

FILE_PATH+"childurl

How do I pass a ref of the variable and not the var name as a string?

EDIT

I have a script called output.js I have a update_script.js

output.js contains this line of code

e.exports=n.p+"old/site/logo.svg"

update_script.js is running the code above which finds all references to "old/site/" and replaces it with "FILE_PATH+" so the new path looks like

What I want

e.exports=n.p+FILE_PATH+"/logo.svg"

So my question is how do I pass FILE_PATH without passing it as a string. Right now when I pass "FILE_PATH+" the url get update to

What I get

e.exports=n.p+"+FILE_PATH+/logo.svg"
10
  • Change to: "FILE_PATH+" to to: FILE_PATH + "+"? Commented Apr 8, 2019 at 11:42
  • this will add the value of FILE_PATH to the string. I want to reference the variable itself e.g "www."+FILE_PATH+"childurl.com" Commented Apr 8, 2019 at 11:44
  • Ah, I see. You can get the matched part by using a callback function. See my answer. Commented Apr 8, 2019 at 11:48
  • The last part of the url should be a string Commented Apr 8, 2019 at 13:03
  • So what is the value of FILE_PATH? Can you give an example of its value and what effect that has on the replacement? Commented Apr 8, 2019 at 13:06

1 Answer 1

3

You can use the callback value for to. Its argument will be the matched string:

const options = {
  files: './pgReactBuild/static/js/main.80e5f778.chunk.js',
  from: "old/site/",
  to: (childurl) => FILE_PATH + childurl,
};
Sign up to request clarification or add additional context in comments.

3 Comments

This doesnt work. It still passes the value of FILE_PATH and not "FILE_PATH"
I don't understand. I thought you said you wanted a ref to the variable, not the literal string. But now you say you want "FILE_PATH", which is a string literal?
No, I don't understand. It seems from the question that "FILE_PATH" is what you don't want. But here you say that is what you do want. I am confused.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.