Skip to content

Instantly share code, notes, and snippets.

@zbinlin
Created June 8, 2019 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zbinlin/3d8605394cda60adc8d624f45c855ca4 to your computer and use it in GitHub Desktop.
Save zbinlin/3d8605394cda60adc8d624f45c855ca4 to your computer and use it in GitHub Desktop.
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