const fs = require("fs"); const path = require("path"); function extract(directoryPath) { const mail = new RegExp( "([a-z0-9_.+-]+)@([a-z0-9.-]+)\\.(([a-zA-Z0-9]|\\.){2,})", "g", ); try { let emails = []; const dirs = fs.readdirSync(directoryPath, { withFileTypes: true }); for (const dir of dirs) { if (dir.isFile()) { const data = fs.readFileSync( path.join(directoryPath, dir.name), "utf8", ); const matches = data.match(mail); if (matches) { emails = [...emails, ...matches]; } } else if (dir.isDirectory()) { emails = [ ...emails, ...extract(path.join(directoryPath, dir.name)), ]; } } return emails; } catch { throw new Error("The directory does not exist"); } } /* fzhfeklzjfh@dakljhlzekj.f fkzjefh@aaa0.fr dejkzd@djkelh.fr djhezklf0587dzedez@gmail................ xavier.login@epita.fr xavier.login@epita.fR */ module.exports = { extract, }; console.log(extract("gallery"));