Codemod - Reading a file in node that uses export syntax

64 views Asked by At

Working on an npm package that runs a codemod on any given source repository. As part of my codemod I am looking to replace certain values in the source repository. Before I replace those values I need to check for values in an object being exported from a specific file (let's call it constants.js) for those values to exist. The check above that I need to perform from constants.js is conditional. It is conditional to if the user of the codemod wants to look up the constants.js file. The constants.js file uses the import..export statement to export the object that I need.

Any pointers on how I can extract the object from that file? I can't really use the babel option here so I am trying to use the fs methods to get what I want. I used the createReadStream method to read the file, but parsing it to extract the default export seems very fragile. Is there a clean way I can get what I need?

In the example below, I want to be able to extract Messages so I can look up the termsOfUse for my codemod changes.

Constants.js

const Messages = {
      termsOfUse: locale => (locale === 'en' ? `TERMS OF USE` : `CONDITIONS`),
};

export default Messages;

Thanks in advance for your help.

0

There are 0 answers