0

I'm looping through scripts in a folder. For each script i check for regex matchings. If a matching appears i want to create an object out of it and push the object to an array. Then I want to console.log the Array but it's empty.

const { getServerFolders, getServerFolderElements } = require('./mainModules/getServerFolders.js')
var fs = require('fs');
const serverFoldersPath = "../electCon/src/scripts/"

function getFolderElements(folder) {
    var variableObjArray = []
    let folderName = folder.split('_')[0] //
    // returns the files of the folder as an array
    let serverFolderElements = getServerFolderElements(folderName); 
      serverFolderElements.forEach(element => {
        //read the script
        fs.readFile(serverFoldersPath + folderName + "/" + element , "utf-8", (err, data) => {
          // throw if error
          if(err) {return console.log(err)}
          // find the variable with the # prefix
          var scriptVariableNames = data.match(/#[^#]+#/g)
          // if there is one or more variable in the script
          if(scriptVariableNames) {
            // for each variable in the script
            scriptVariableNames.forEach(variable => {
              let variableProp = variable.split("#")[1]
              variableObject = {
                sourceScript: element,
                variableName : variable,
                variableProp : variableProp,
                variableDescription : "",
              }
              variableObjArray.push(variableObject)
    
            })
            
          }
          
        })
      
    })
    console.log(variableObjArray)
}

when i log the array oe block earlier its full (but will log many times because of the loops), but as soon as i jump out a block its empty again

I have already tried to...

... log the variableObjArray earlier, but this way it would be in the for each loop.

... declare the object before, change the properties and than push it

Here to full code:

index.js

const { getServerFolders, getServerFolderElements } = require('./mainModules/getServerFolders.js')
var fs = require('fs');
const serverFoldersPath = "../electCon/src/scripts/"

function variableFinder(folder) {
    var variableObjArray = []
    let folderName = folder.split('_')[0] //
    // returns the files of the folder as an array
    let serverFolderElements = getServerFolderElements(folderName); 
      serverFolderElements.forEach(element => {
        //read the script
        fs.readFile(serverFoldersPath + folderName + "/" + element , "utf-8", (err, data) => {
          // throw if error
          if(err) {return console.log(err)}
          // find the variable with the # prefix
          var scriptVariableNames = data.match(/#[^#]+#/g)
          // if there is one or more variable in the script
          if(scriptVariableNames) {
            // for each variable in the script
            scriptVariableNames.forEach(variable => {
              let variableProp = variable.split("#")[1]
              variableObject = {
                sourceScript: element,
                variableName : variable,
                variableProp : variableProp,
                variableDescription : "",
              }
              variableObjArray.push(variableObject)
    
            })
            
          }
          
        })
      
    })
    console.log(variableObjArray)
}

getServerFolders:

const { readdirSync } = require('fs');
const serverFoldersPath = "../electCon/src/scripts/"


function getServerFolders() {
    let serverFolders = readdirSync(serverFoldersPath)
    return serverFolders
}

function getServerFolderElements(server) {
    let serverFolderElements = readdirSync(serverFoldersPath + server)
    return serverFolderElements
}


module.exports = { getServerFolders, getServerFolderElements }

the scripts i want to loop trough:

hello mom #heeelp# #variable10# i hope stackoverflow can help me
not a variable #variablee# and some filler
Test-Connection #variable1#

I have already tried to...

... log the variableObjArray earlier, but this way it would be in the for each loop.

... declare the object before, change the properties and than push it

Note: its actually programmed in Electron and the variableFinder function is actually a ipcMain listener. I rewrote the function this way so no one will be confused. Thanks a lot :)

Lamphish
  • 11
  • 2
  • `readFile` is an asynchronous function, meaning that its callback runs sometime in the future once your main script has finished calling its functions and executing its code. That's why your log shows an empty array, as it's running before you file has been read (see the links above your question for more details, specifically answers that use promises such as [this](https://stackoverflow.com/a/44019533/5648954)). – Nick Parsons Dec 02 '22 at 09:24

0 Answers0