Skip to content

Instantly share code, notes, and snippets.

@Nipodemos
Created May 10, 2021 21:02
Show Gist options
  • Select an option

  • Save Nipodemos/559f6bbea2d69322e0d2a1205c95566f to your computer and use it in GitHub Desktop.

Select an option

Save Nipodemos/559f6bbea2d69322e0d2a1205c95566f to your computer and use it in GitHub Desktop.
const fs = require("fs").promises;
async function readFolder(path) {
const dir = await fs.readdir(path);
const json = {};
for (arquivo of dir) {
const currentItemPath = path + "/" + arquivo;
const detalhesDoArquivo = await fs.stat(currentItemPath);
if (detalhesDoArquivo.isDirectory()) {
// é uma pasta
json[arquivo] = [await readFolder(currentItemPath)];
console.log("é uma pasta: " + arquivo);
} else if (detalhesDoArquivo.isFile()) {
// é um arquivo
if (!json[arquivo]) {
json[arquivo] = {};
}
console.log("arquivo :>> ", arquivo);
json[arquivo] = {
name: arquivo,
};
console.log("é um arquivo: " + arquivo);
}
}
return json;
}
readFolder("./ARQUIVOS").then((result) => {
console.log("result :>> ", result);
console.log("CURITIBA FILES ");
result.CURITIBA[0].forEach((item) => {
console.log("item :>> ", item);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment