I get nothing in result object, because it's executed earlier than readFile.
But anyway, I want to find a way to add all properties to the global object and then add it all into the .json file.
const fs = require('fs');
const PATH = './src/features';
const featureFolders = fs.readdirSync(PATH);
const result = {};
for (const file of featureFolders) {
// console.log(file);
const allFolders = fs.readdirSync(${PATH}/${file});
for (const componentFile of allFolders) {
fs.readFile(${PATH}/${file}/${componentFile}, 'utf-8', (err, data) =\> {
const myRegex = /someregex/;
if (myRegex.test(data?.toString())) {
result[componentFile] = {};
}
});
}
}
fs.writeFile('./myJSON.json', JSON.stringify(result), error =\> {
console.log(error);
});
My attempts are described in the code snippet above.