forked from stack-auth/stack-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-platform-navigation.js
More file actions
143 lines (118 loc) · 4.77 KB
/
generate-platform-navigation.js
File metadata and controls
143 lines (118 loc) · 4.77 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env node
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { parse } from 'yaml';
/**
* Generate platform-navigation.ts from docs-platform.yml
* This ensures we have a single source of truth for platform pages
*/
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const DOCS_PLATFORM_YML_PATH = path.join(__dirname, '..', 'docs-platform.yml');
const OUTPUT_PATH = path.join(__dirname, '..', 'src', 'lib', 'platform-navigation.ts');
function generatePlatformNavigation() {
try {
// Read and parse docs-platform.yml
const yamlContent = fs.readFileSync(DOCS_PLATFORM_YML_PATH, 'utf8');
const config = parse(yamlContent);
if (!config.pages || !Array.isArray(config.pages)) {
throw new Error('Invalid docs-platform.yml format: missing pages array');
}
// Validate all platform values
const validPlatforms = new Set(['next', 'react', 'js', 'python']); // Actual platforms used in docs
config.pages.forEach((page, idx) => {
if (!Array.isArray(page.platforms)) {
throw new Error(`Invalid platforms array at page index ${idx}`);
}
page.platforms.forEach(platform => {
if (!validPlatforms.has(platform)) {
throw new Error(`Invalid platform "${platform}" at page index ${idx}`);
}
});
});
// Generate TypeScript content
const tsContent = `// THIS FILE IS AUTO-GENERATED
// Do not edit manually - update docs-platform.yml instead
// Generated on: ${new Date().toISOString()}
import { type Platform } from './platform-utils';
// Platform pages data auto-generated from docs-platform.yml
const PLATFORM_PAGES: { path: string, platforms: Platform[] }[] = [
${config.pages.map(page => {
const platformsStr = JSON.stringify(page.platforms);
return ` { path: ${JSON.stringify(page.path)}, platforms: ${platformsStr} },`;
}).join('\n')}
];
/**
* Safely join URL path segments
*/
function joinUrlPath(...segments: string[]): string {
return segments
.filter(segment => segment && segment.length > 0)
.join('/')
.replace(/\\/+/g, '/'); // Remove duplicate slashes
}
/**
* Check if a specific page exists for a given platform
*/
export function pageExistsForPlatform(path: string, platform: Platform): boolean {
// Normalize path - remove leading slash and ensure .mdx extension
const normalizedPath = path.replace(/^\\//, '');
const pathWithExt = normalizedPath.endsWith('.mdx') ? normalizedPath : \`\${normalizedPath}.mdx\`;
// First try to find exact match
let page = PLATFORM_PAGES.find(p => p.path === pathWithExt);
// If not found and path doesn't end with index, try appending /index.mdx
if (!page && !pathWithExt.includes('/index.mdx')) {
const indexPath = normalizedPath.endsWith('.mdx')
? normalizedPath.replace('.mdx', '/index.mdx')
: \`\${normalizedPath}/index.mdx\`;
page = PLATFORM_PAGES.find(p => p.path === indexPath);
}
return page?.platforms.includes(platform) ?? false;
}
/**
* Get smart redirect URL for platform switching
* If the current page doesn't exist for the target platform, redirect to overview
*/
export function getSmartPlatformRedirect(currentPath: string, targetPlatform: Platform): string {
// Remove /docs/{platform}/ prefix and normalize
const pathWithoutPlatform = currentPath.replace(/^\\/docs\\/[^\\/]+\\//, '');
// If the exact same page exists for target platform, use it
if (pageExistsForPlatform(pathWithoutPlatform, targetPlatform)) {
const cleanPath = pathWithoutPlatform.replace(/\\.mdx$/, '');
return joinUrlPath('/docs', targetPlatform, cleanPath);
}
// Otherwise, redirect to overview
return joinUrlPath('/docs', targetPlatform, 'overview');
}
/**
* Get all pages available for a specific platform
*/
export function getPagesForPlatform(platform: Platform): string[] {
return PLATFORM_PAGES
.filter(page => page.platforms.includes(platform))
.map(page => page.path.replace(/\\.mdx$/, ''));
}
`;
// Write the generated file
fs.writeFileSync(OUTPUT_PATH, tsContent, 'utf8');
console.log('✅ Generated platform-navigation.ts from docs-platform.yml');
console.log(`📄 ${config.pages.length} pages configured for platforms`);
// Show platform distribution
const platformCounts = {};
config.pages.forEach(page => {
page.platforms.forEach(platform => {
platformCounts[platform] = (platformCounts[platform] || 0) + 1;
});
});
console.log('📊 Platform distribution:');
Object.entries(platformCounts).forEach(([platform, count]) => {
console.log(` ${platform}: ${count} pages`);
});
} catch (error) {
console.error('❌ Error generating platform-navigation.ts:', error.message);
process.exit(1);
}
}
// Run the generator
generatePlatformNavigation();