const fs = require("fs");

module.exports = () => {
    const fileObj = {};
    const exceptions = ["auth", "_app.js", "_document.js", "_error.js",
    "AuthRouter.js",
    "Candidatures.js",
    "ConfirmAccount.js",
    "Parametres.js",
    "Reponse.js",
    "ResetPassword.js",
    "VerifyEmail.js",
    "Entreprise.js",
    "Offre.js",
    "Conseils.js",
    "index.js"
]
    const walkSync = dir => {
        // Get all files of the current directory & iterate over them
        const files = fs.readdirSync(dir);
        files.forEach(file => {

            //exclude private files etc
            if (exceptions.indexOf(file) != -1)
                return;

            // Construct whole file-path & retrieve file's stats
            const filePath = `${dir}${file}`;
            const fileStat = fs.statSync(filePath);

            if (fileStat.isDirectory()) {
                // Recurse one folder deeper
                walkSync(`${filePath}/`);
            } else {
                // Construct this file's pathname excluding the "pages" folder & its extension
                const cleanFileName = filePath
                    .substr(0, filePath.lastIndexOf("."))
                    .replace("pages/", "");

                // Add this file to `fileObj`
                fileObj[`/${cleanFileName}`] = {
                    page: `/${cleanFileName}`,
                    lastModified: fileStat.mtime
                };
            }
        });
    };

    // Start recursion to fill `fileObj`
    walkSync("pages/");

    return fileObj;
};