-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.mjs
450 lines (383 loc) · 14.5 KB
/
build.mjs
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#!/usr/bin/env node
import { execSync } from 'child_process';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Load repository configuration from JSON file
let REPOS = [];
try {
const configPath = path.join(__dirname, 'repos-config.json');
const configData = JSON.parse(fs.readFileSync(configPath, 'utf8'));
REPOS = configData.repositories;
console.log(`Loaded ${REPOS.length} repositories from configuration file`);
} catch (error) {
console.error(`Error loading repository configuration: ${error.message}`);
process.exit(1);
}
/**
* Parse command line arguments to override repository branches
*/
function parseBranchArgs() {
const args = process.argv.slice(2);
// Process arguments in the format: repo=branch (e.g., engine=dev)
for (const arg of args) {
const match = arg.match(/^([^=]+)=(.+)$/);
if (match) {
const [, repoName, branchName] = match;
const repo = REPOS.find(r => r.name === repoName);
if (repo) {
console.log(`Setting custom branch for ${repoName}: ${branchName}`);
repo.branch = branchName;
} else {
console.warn(`Warning: Unknown repository '${repoName}' specified in arguments`);
}
}
}
}
/**
* Execute shell command and display the output
*/
function runCommand(command, options = {}) {
console.log(`Running: ${command}`);
return execSync(command, { stdio: 'inherit', ...options });
}
/**
* Delete directory if it exists
*/
function deleteDir(dir) {
if (fs.existsSync(dir)) {
console.log(`Removing existing directory: ${dir}`);
fs.rmSync(dir, { recursive: true, force: true });
}
}
/**
* Create directory if it doesn't exist
*/
function ensureDir(dir) {
if (!fs.existsSync(dir)) {
console.log(`Creating directory: ${dir}`);
fs.mkdirSync(dir, { recursive: true });
}
}
/**
* Copy directory contents recursively
*/
function copyDirContents(src, dest) {
if (!fs.existsSync(src)) {
console.log(`Source directory doesn't exist: ${src}`);
return;
}
ensureDir(dest);
const entries = fs.readdirSync(src, { withFileTypes: true });
for (const entry of entries) {
const srcPath = path.join(src, entry.name);
const destPath = path.join(dest, entry.name);
if (entry.isDirectory()) {
copyDirContents(srcPath, destPath);
} else {
fs.copyFileSync(srcPath, destPath);
}
}
}
/**
* Combine sitemap.xml files from all repositories
*/
function combineSitemaps() {
const sitemaps = [];
let siteUrl = 'https://api.playcanvas.com';
// Check the package.json for homepage URL
try {
const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8'));
if (packageJson.homepage) {
siteUrl = packageJson.homepage;
}
} catch (error) {
console.warn('Warning: Could not read package.json to determine site URL.');
}
// Remove trailing slash if present
siteUrl = siteUrl.replace(/\/$/, '');
console.log(`Using site URL: ${siteUrl}`);
// Collect URLs from each repository's sitemap
for (const repo of REPOS) {
// Get the actual target folder name (editor-api -> editor)
const targetFolderName = repo.name === 'editor-api' ? 'editor' : repo.name;
// Look for the sitemap in the target folder, not necessarily the repo name folder
const sitemapPath = path.join('docs', targetFolderName, 'sitemap.xml');
if (fs.existsSync(sitemapPath)) {
try {
const sitemapContent = fs.readFileSync(sitemapPath, 'utf8');
console.log(`Processing sitemap for ${repo.name} (${sitemapPath})`);
// Extract URLs and lastmod times using regex
const urlRegex = /<url>[\s\S]*?<loc>(.*?)<\/loc>(?:[\s\S]*?<lastmod>(.*?)<\/lastmod>)?[\s\S]*?<\/url>/g;
let match;
let count = 0;
while ((match = urlRegex.exec(sitemapContent)) !== null) {
let url = match[1];
const lastmod = match[2] || '';
let finalUrl = '';
// Make relative to site root if needed
if (url.startsWith('http')) {
// Extract the path portion from the URL
const urlPath = new URL(url).pathname;
// Remove leading slash for consistency in checks
const normalizedPath = urlPath.replace(/^\//, '');
const normalizedRepoName = repo.name.replace(/^\//, '').replace(/\/$/, '');
// Special handling for engine-v1 repository
if (repo.name === 'engine-v1' && normalizedPath.startsWith('engine/')) {
// For engine-v1, remove the 'engine/' prefix from paths
const pathWithoutEngine = normalizedPath.replace(/^engine\//, '');
finalUrl = `${siteUrl}/${targetFolderName}/${pathWithoutEngine}`;
}
// Special handling for editor-api repository
else if (repo.name === 'editor-api' && normalizedPath.startsWith('editor/')) {
// For editor-api, remove the 'editor/' prefix from paths
const pathWithoutEditor = normalizedPath.replace(/^editor\//, '');
finalUrl = `${siteUrl}/${targetFolderName}/${pathWithoutEditor}`;
}
// Regular handling for other repositories
else if (normalizedPath.startsWith(`${normalizedRepoName}/`)) {
// Replace repository name in path with target folder name
const adjustedPath = urlPath.replace(normalizedRepoName, targetFolderName);
finalUrl = `${siteUrl}${adjustedPath}`;
} else {
finalUrl = `${siteUrl}/${targetFolderName}/${normalizedPath}`;
}
} else {
// For relative URLs, normalize paths
const normalizedUrl = url.replace(/^\//, '');
const normalizedRepoName = repo.name.replace(/^\//, '').replace(/\/$/, '');
// Special handling for engine-v1 repository
if (repo.name === 'engine-v1' && normalizedUrl.startsWith('engine/')) {
// For engine-v1, remove the 'engine/' prefix from paths
const urlWithoutEngine = normalizedUrl.replace(/^engine\//, '');
finalUrl = `${siteUrl}/${targetFolderName}/${urlWithoutEngine}`;
}
// Special handling for editor-api repository
else if (repo.name === 'editor-api' && normalizedUrl.startsWith('editor/')) {
// For editor-api, remove the 'editor/' prefix from paths
const pathWithoutEditor = normalizedUrl.replace(/^editor\//, '');
finalUrl = `${siteUrl}/${targetFolderName}/${pathWithoutEditor}`;
}
// Regular handling for other repositories
else if (normalizedUrl.startsWith(`${normalizedRepoName}/`)) {
// Replace repository name in path with target folder name
const adjustedUrl = normalizedUrl.replace(normalizedRepoName, targetFolderName);
finalUrl = `${siteUrl}/${adjustedUrl}`;
} else {
finalUrl = `${siteUrl}/${targetFolderName}/${normalizedUrl}`;
}
}
let urlEntry = `
<url>
<loc>${finalUrl}</loc>`;
// Add lastmod if available
if (lastmod) {
urlEntry += `
<lastmod>${lastmod}</lastmod>`;
}
urlEntry += `
</url>`;
sitemaps.push(urlEntry);
count++;
}
console.log(`Extracted ${count} URLs from ${repo.name} sitemap`);
} catch (error) {
console.warn(`Warning: Could not process sitemap for ${repo.name}: ${error.message}`);
}
} else {
console.warn(`Warning: No sitemap found for ${repo.name} at ${sitemapPath}`);
}
}
// Add root page to sitemap with current date as lastmod
const today = new Date().toISOString().split('T')[0];
sitemaps.unshift(`
<url>
<loc>${siteUrl}/</loc>
<lastmod>${today}</lastmod>
</url>`);
// Create combined sitemap
const combinedSitemap = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">${sitemaps.join('')}
</urlset>`;
// Write to the docs folder
fs.writeFileSync(path.join('docs', 'sitemap.xml'), combinedSitemap);
console.log(`Combined sitemap created successfully with ${sitemaps.length} URLs.`);
}
/**
* Generate redirect files for old URL patterns to new structure
*/
function generateRedirects() {
console.log('Generating redirects for old URL structure...');
// Define the patterns to match and their redirects
const patterns = [
{
oldDir: 'classes',
pattern: /^Engine\.(.+)\.html$/,
newPath: '/engine/classes/$1.html'
},
{
oldDir: 'interfaces',
pattern: /^Engine\.(.+)\.html$/,
newPath: '/engine/interfaces/$1.html'
},
{
oldDir: 'types',
pattern: /^Engine\.(.+)\.html$/,
newPath: '/engine/types/$1.html'
},
{
oldDir: 'modules',
pattern: /^Engine\.(.+)\.html$/,
newPath: '/engine/modules/$1.html'
},
{
oldDir: 'functions',
pattern: /^Engine\.(.+)\.html$/,
newPath: '/engine/functions/$1.html'
}
];
// Create all the necessary directories and redirect files
for (const { oldDir, pattern, newPath } of patterns) {
const dirPath = path.join('docs', oldDir);
ensureDir(dirPath);
// Create a catch-all index.html in the directory for redirecting
const indexPath = path.join(dirPath, 'index.html');
const indexContent = `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Redirecting...</title>
<script>
(function() {
var path = window.location.pathname;
var filename = path.split('/').pop();
if (filename) {
var match = filename.match(${pattern.toString()});
if (match && match[1]) {
var newUrl = "${newPath}".replace('$1', match[1]);
window.location.href = newUrl;
return;
}
}
// If no match or no filename, redirect to homepage
window.location.href = '/';
})();
</script>
</head>
<body>
<p>Redirecting to the new API reference structure...</p>
</body>
</html>`;
fs.writeFileSync(indexPath, indexContent);
console.log(`Created redirect for /${oldDir}/* pattern`);
}
// Create a 404 page that attempts to handle redirects as well
const notFoundPath = path.join('docs', '404.html');
const notFoundContent = `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Page Not Found</title>
<script>
(function() {
const path = window.location.pathname;
const segments = path.split('/');
const filename = segments.pop();
const dirType = segments.pop();
// Check if this matches our old URL pattern
if (dirType && filename && ['classes', 'functions', 'interfaces', 'modules', 'types', 'variables'].includes(dirType)) {
const match = filename.match(/^Engine\.(.+)\.html$/);
if (match && match[1]) {
const newUrl = "/engine/" + dirType + "/" + match[1] + ".html";
window.location.href = newUrl;
return;
}
}
// Default fallback - go to homepage
window.location.href = '/';
})();
</script>
</head>
<body>
<h1>Page Not Found</h1>
<p>Redirecting to the new URL structure...</p>
</body>
</html>`;
fs.writeFileSync(notFoundPath, notFoundContent);
console.log('Created 404 page with redirection logic');
console.log('Redirect generation complete.');
}
/**
* Main function to build the documentation
*/
async function buildDocs() {
try {
// Parse command line arguments for branch overrides
parseBranchArgs();
// Create docs directory if it doesn't exist
ensureDir('docs');
// Create .nojekyll file to prevent GitHub Pages from using Jekyll
console.log('Creating .nojekyll file...');
fs.writeFileSync(path.join('docs', '.nojekyll'), '');
// Remove existing repos directory and create a new one
deleteDir('repos');
ensureDir('repos');
// Process each repository
for (const repo of REPOS) {
console.log(`\n========== Processing ${repo.name} (branch: ${repo.branch}) ==========`);
// Change to repos directory
process.chdir(path.join(process.cwd(), 'repos'));
// Clone the repository with specified branch
runCommand(`git clone -b ${repo.branch} ${repo.url} ${repo.name}`);
// Change to repository directory
process.chdir(path.join(process.cwd(), repo.name));
// Install dependencies
runCommand('npm install');
// Build documentation
runCommand('npm run docs');
// Copy docs to the main docs directory if they exist
const docsDir = path.join(process.cwd(), 'docs');
if (fs.existsSync(docsDir)) {
// Use "editor" folder for editor-api repository
const targetFolderName = repo.name === 'editor-api' ? 'editor' : repo.name;
const targetDir = path.join(process.cwd(), '..', '..', 'docs', targetFolderName);
console.log(`Copying docs from ${docsDir} to ${targetDir}`);
ensureDir(targetDir);
copyDirContents(docsDir, targetDir);
}
// Return to root directory
process.chdir(path.join(process.cwd(), '..', '..'));
console.log(`Completed processing ${repo.name}`);
}
// Copy the index.html to the docs directory
console.log('\nCopying index.html file...');
const sourceIndexPath = path.join(__dirname, 'index.html');
if (!fs.existsSync(sourceIndexPath)) {
throw new Error(`Source index.html not found: ${sourceIndexPath}`);
}
fs.copyFileSync(sourceIndexPath, path.join('docs', 'index.html'));
// Copy favicon
console.log('Copying favicon...');
fs.copyFileSync('favicon.ico', path.join('docs', 'favicon.ico'));
// Copy assets directory if it exists
if (fs.existsSync('assets')) {
console.log('Copying assets directory...');
ensureDir(path.join('docs', 'assets'));
copyDirContents('assets', path.join('docs', 'assets'));
}
// Generate combined sitemap
console.log('\nGenerating combined sitemap...');
combineSitemaps();
// Generate redirects for old URLs
console.log('\nGenerating redirects for old URL structure...');
generateRedirects();
console.log('\nDocumentation build complete. Run "npm run serve" to view it.');
} catch (error) {
console.error(`\nError: ${error.message}`);
process.exit(1);
}
}
// Run the build process
buildDocs();