const fs = require('fs').promises
const path = require('path')

async function loadJSONFiles(dirPath) {
    const files = await fs.readdir(dirPath)
    const jsonFiles = files.filter(file => file.endsWith('.json'))

    const filePromises = jsonFiles.map(async file => {
        const filePath = path.join(dirPath, file)
        const data = await fs.readFile(filePath, 'utf8')
        return { crm: JSON.parse(data), path: filePath }
    })

    const jsonDataArray = await Promise.all(filePromises)  

    return jsonDataArray

}

module.exports = loadJSONFiles
