Created
June 8, 2019 16:30
-
-
Save zbinlin/3d8605394cda60adc8d624f45c855ca4 to your computer and use it in GitHub Desktop.
This file contains 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'); | |
async function main() { | |
const cache = {}; | |
await readdir('.', cache); | |
console.log(JSON.stringify(cache)); | |
} | |
async function readdir(dir, cache) { | |
dir = path.resolve(dir); | |
const dirents = await fs.readdir(dir, { | |
withFileTypes: true, | |
}); | |
for (const dirent of dirents) { | |
if (dirent.isDirectory()) { | |
await readdir(path.join(dir, dirent.name), cache[dirent.name] = {}); | |
} else if (dirent.isFile()) { | |
await read(path.join(dir, dirent.name), cache); | |
} | |
} | |
} | |
async function read(filePath, cache) { | |
const fileExt = path.extname(filePath); | |
if (fileExt.toLowerCase() !== '.json') { | |
return; | |
} | |
const basename = path.basename(filePath, fileExt); | |
cache[basename] = require(filePath); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment