Last active
May 7, 2020 14:15
-
-
Save Akiyamka/82ef17ad43dd97225066a02ef5f15afa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs').promises; | |
const path = require('path'); | |
const ls = dir => fs.readdir(dir); | |
async function walk(dir) { | |
const filesList = await ls(dir); | |
const files = await Promise.all(filesList.map(async file => { | |
const filePath = path.join(dir, file); | |
const stats = await fs.stat(filePath); | |
if (stats.isDirectory()) return walk(filePath); | |
else if (stats.isFile()) return filePath; | |
})); | |
return files.reduce((all, folderContents) => all.concat(folderContents), []); | |
} | |
async function collectJSON(dir, fileName) { | |
const fileList = await walk(dir); | |
const targetFiles = fileList.filter(name => path.basename(name) === fileName); | |
const jsonFiles = await Promise.all(targetFiles.map(path => fs.readFile(path, 'utf8'))); | |
return jsonFiles.map(string => JSON.parse(string)).reduce((acc, json) => (Object.assign(acc, json))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment