-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsitemap.ts
More file actions
56 lines (44 loc) · 1.6 KB
/
sitemap.ts
File metadata and controls
56 lines (44 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { Route } from '@angular/router';
import { writeFileSync } from 'fs';
import { resolve } from 'path';
import { SitemapStream, streamToPromise } from 'sitemap';
import { Readable } from 'stream';
import { routes } from './src/app/app.routes';
// Helper to extract paths from Angular routes
function getPaths(routes: Route[], parentPath = ''): string[] {
let paths: string[] = [];
for (const route of routes) {
if (route.path === '**' || route.redirectTo) {
continue;
}
const currentPath = route.path ? `${parentPath}/${route.path}` : parentPath;
// Clean up double slashes
const cleanPath = currentPath.replace(/\/+/g, '/');
if (route.component || route.loadComponent) {
paths.push(cleanPath || '/');
}
if (route.children) {
paths = paths.concat(getPaths(route.children, cleanPath));
}
}
return paths;
}
async function generateSitemap() {
try {
const stream = new SitemapStream({ hostname: 'https://elbe-ui.dev' });
// TODO filter out routes starting with /preview
const paths = getPaths(routes).filter((path) => !path.startsWith('/preview'));
const links = paths.map((url) => ({
url,
}));
const data = await streamToPromise(Readable.from(links).pipe(stream));
const path = resolve(resolve(process.cwd(), 'public'), 'sitemap.xml');
writeFileSync(path, data.toString());
console.log('✅ Sitemap generated successfully at', path);
console.log(' Routes included:', paths.join(', '));
} catch (error) {
console.error('❌ Error generating sitemap:', error);
process.exit(1);
}
}
generateSitemap();