1
0
mirror of https://github.com/kamranahmedse/developer-roadmap.git synced 2025-02-23 19:13:19 +01:00

145 lines
4.0 KiB
JavaScript
Raw Normal View History

2021-09-05 16:07:20 +02:00
// This is a development script executed in the build step of pages
const fs = require('fs');
const path = require('path');
const glob = require('glob');
const guides = require('../content/guides.json');
const roadmaps = require('../content/roadmaps.json');
const DOMAIN = 'https://roadmap.sh';
const PAGES_DIR = path.join(__dirname, '../pages');
const SITEMAP_PATH = path.join(__dirname, '../public/sitemap.xml');
const PAGES_PATH = path.join(__dirname, '../pages');
const ROADMAPS_PATH = path.join(__dirname, '../content/roadmaps');
const GUIDES_PATH = path.join(__dirname, '../content/guides');
// Set the header
const xmlHeader = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">`;
// Wrap all pages in <urlset> tags
2021-12-10 14:39:47 +01:00
const xmlUrlWrapper = (nodes) => `${xmlHeader}
2021-09-05 16:07:20 +02:00
${nodes}
</urlset>`;
function getSlugPriority(pageSlug) {
if (pageSlug === '/') {
return '1.0';
}
const slugPriorities = [
2021-12-10 14:39:47 +01:00
['/roadmaps', '/guides', '/watch', '/podcasts'], // 1.0
2021-09-05 16:07:20 +02:00
['/signup'], // 0.9
2021-12-10 14:39:47 +01:00
['/about'], // 0.8
2021-09-05 16:07:20 +02:00
];
2021-12-10 14:39:47 +01:00
const foundIndex = slugPriorities.findIndex((routes) =>
routes.some((route) => pageSlug.startsWith(route))
2021-09-05 16:07:20 +02:00
);
if (foundIndex !== -1) {
2021-12-10 14:39:47 +01:00
return parseFloat((10 - foundIndex) / 10).toFixed(1);
2021-09-05 16:07:20 +02:00
}
return 0.5;
}
function getPageRoutes() {
const files = glob.sync(`${PAGES_PATH}/**/*.tsx`, {
ignore: [
2021-12-10 14:39:47 +01:00
'**/_*.tsx', // private non-page files e.g. _document.js
'**/[[]*[]].tsx', // Ignore dynamic pages i.e. `page/[something].js` files
'**/[[]*[]]/*.tsx', // Ignore files inside dynamic pages i.e. `[something]/abc.js`
'**/components/*.tsx', // Ignore the component files
],
2021-09-05 16:07:20 +02:00
});
const pageRoutes = {};
2021-12-10 14:39:47 +01:00
files.forEach((file) => {
2021-09-05 16:07:20 +02:00
const pageName = file.replace(PAGES_PATH, '').replace('.tsx', '');
const pagePath = pageName.replace('/index', '') || '/';
pageRoutes[pagePath] = { page: `${pageName}` };
});
return pageRoutes;
}
2021-09-26 22:02:59 +02:00
function generateNode(nodeProps) {
const {
slug,
basePath,
fileName,
priority = null,
date = null,
2021-12-10 14:39:47 +01:00
frequency = 'monthly',
2021-09-26 22:02:59 +02:00
} = nodeProps;
2022-09-08 20:32:20 +04:00
if (slug.includes('upcoming') || slug.includes('pdfs')) {
2022-09-08 20:29:59 +04:00
return null;
}
2021-09-05 16:07:20 +02:00
const pagePath = path.join(basePath, fileName);
let pageStats = {};
try {
pageStats = fs.lstatSync(pagePath);
} catch (e) {
console.log(`File not found: ${pagePath}`);
2021-12-10 14:39:47 +01:00
pageStats = { mtime: new Date() };
2021-09-05 16:07:20 +02:00
}
return `<url>
<loc>${DOMAIN}${slug}</loc>
<changefreq>${frequency}</changefreq>
<lastmod>${date || pageStats.mtime.toISOString()}</lastmod>
<priority>${priority || getSlugPriority(slug)}</priority>
</url>`;
}
function generateSiteMap() {
const pageRoutes = getPageRoutes();
2021-12-10 14:39:47 +01:00
const pageSlugs = Object.keys(pageRoutes).filter(
(route) => !['/privacy', '/terms'].includes(route)
);
2021-09-05 16:07:20 +02:00
2021-12-10 14:39:47 +01:00
const pagesChunk = pageSlugs.map((pageSlug) => {
2021-09-05 16:07:20 +02:00
return generateNode({
basePath: PAGES_DIR,
fileName: `${pageRoutes[pageSlug].page}.tsx`,
2021-12-10 14:39:47 +01:00
slug: pageSlug,
2021-09-05 16:07:20 +02:00
});
});
2021-12-10 14:39:47 +01:00
const guidesChunk = guides.map((guide) => {
2021-09-05 16:07:20 +02:00
return generateNode({
basePath: GUIDES_PATH,
fileName: `${guide.id}.md`,
slug: `/guides/${guide.id}`,
date: guide.updatedAt,
2021-12-10 14:39:47 +01:00
priority: '1.0',
2021-09-05 16:07:20 +02:00
});
});
const roadmapsChunk = roadmaps.map((roadmap, roadmapCounter) => {
return generateNode({
basePath: ROADMAPS_PATH,
2021-09-26 22:02:59 +02:00
fileName: roadmap.metaPath.replace('/roadmaps', ''),
2021-09-05 16:07:20 +02:00
slug: `/${roadmap.id}`,
date: roadmap.updatedAt,
2021-12-10 14:39:47 +01:00
priority: '1.0',
2021-09-05 16:07:20 +02:00
});
});
2021-12-10 14:39:47 +01:00
const nodes = [...roadmapsChunk, ...guidesChunk, ...pagesChunk];
2021-09-05 16:07:20 +02:00
const sitemap = `${xmlUrlWrapper(nodes.join('\n'))}`;
fs.writeFileSync(SITEMAP_PATH, sitemap);
2021-12-10 14:39:47 +01:00
console.log(
`sitemap.xml with ${nodes.length} entries was written to ${SITEMAP_PATH}`
);
2021-09-05 16:07:20 +02:00
}
generateSiteMap();