const { readdirSync, statSync, writeFileSync } = require("fs"); const { extname, join } = require("path"); const { author } = require("../package.json"); const xmlUrls = []; const buildAppSitemap = (path) => { readdirSync(path).forEach((entry) => { if (statSync(join(path, entry)).isDirectory()) { xmlUrls.push( `${author.url}/?app=${entry.replace(/-/g, "")}` ); } }); }; const buildFileSitemap = (path, excludePaths, callback) => { const publicPath = join("public/", path); readdirSync(publicPath).forEach((entry) => { const entryPath = join(path, entry); const urlEntryPath = entryPath.replace(/\\/g, "/"); const stats = statSync(join(publicPath, entry)); if (stats.isDirectory()) { if (!excludePaths.includes(urlEntryPath)) { buildFileSitemap(entryPath, excludePaths, callback); } } else if (![".ini", ".url"].includes(extname(entry).toLowerCase())) { const encodedUrlEntryPath = encodeURI(urlEntryPath); xmlUrls.push( callback( `${author.url}/?url=${encodedUrlEntryPath}`, stats.mtime.toISOString().substring(0, 10), `${author.url}/${encodedUrlEntryPath}` ) ); } }); }; buildAppSitemap("components/apps"); buildFileSitemap( "Users/Public/Documents", [], (path, mtime) => `${path}${mtime}` ); buildFileSitemap( "Users/Public/Pictures", ["Users/Public/Pictures/Blog"], (path, mtime, url) => `${path}${url}${mtime}` ); writeFileSync( "public/sitemap.xml", `${xmlUrls.join( "" )}`, { flag: "w", } ); writeFileSync( "public/robots.txt", `User-agent: *\nAllow: /\n\nSitemap: ${author.url}/sitemap.xml\n`, { flag: "w", } );