I am trying to create a key value pair which looks like this
{
'static/default/js/index': './app_main/js/index.js',
'static/default/js/util': './app_util/js/util.js'
}
with path of the output file as the key and the source as the value.
The problem is when I debug the script at loop where I am iterating through the directory I see the correct key value pair as expected. But when I try to view it outside the loop it returns an empty object. Here is how my directory structure looks like
And here is my code
var fs = require('fs');
var path = [
{
type: 'js',
inputPath: [
'./app_util/js/',
'./app_main/js/'
],
outputPath: 'dist/static/default/js'
}
];
function getFilePath(path) {
var out = {};
path.forEach(obj => {
obj.inputPath.forEach(dirNname =>{
fs.readdir(dirNname, (err, fileNames) => {
if (err) {
console.log(err); // eslint-disable-line no-console
return;
}
fileNames.forEach(fileName => {
out[obj.outputPath] = dirNname+fileName;
console.log(out); // eslint-disable-line no-console
});
})
})
})
return out;
}
var x = getFilePath(path);
//this returns an empty object
console.log(x); // eslint-disable-line no-console
Any ideas what I could be doing wrong? Feels like I am missing something simple