1+ #!/usr/bin/env node
2+
3+ /**
4+ * Pre-build script for Docusaurus
5+ * Handles dynamic configuration based on environment variables
6+ *
7+ * Usage:
8+ * node prebuild.js - Setup for build/start
9+ * node prebuild.js clean - Clean generated files
10+ */
11+
12+ const fs = require ( 'node:fs' ) ;
13+ const path = require ( 'node:path' ) ;
14+
15+ // Check if running in clean mode
16+ const isCleanMode = process . argv [ 2 ] === 'clean' ;
17+
18+ // Get the route base path from environment variable
19+ const routeBasePath = process . env . DOCUSAURUS_ROUTE_BASE_PATH || '/' ;
20+
21+ const indexPagePath = path . join ( __dirname , '../src/pages/index.tsx' ) ;
22+ const pagesDir = path . join ( __dirname , '../src/pages' ) ;
23+
24+ // Helper function to remove the index page
25+ function removeIndexPage ( ) {
26+ if ( fs . existsSync ( indexPagePath ) ) {
27+ fs . unlinkSync ( indexPagePath ) ;
28+ console . log ( 'Removed index.tsx' ) ;
29+ return true ;
30+ }
31+ return false ;
32+ }
33+
34+ if ( isCleanMode ) {
35+ // Clean mode - remove all generated files
36+ console . log ( 'Cleaning generated files...' ) ;
37+ removeIndexPage ( ) ;
38+ process . exit ( 0 ) ;
39+ }
40+
41+ console . log ( 'Running pre-build setup...' ) ;
42+ console . log ( `Route base path: ${ routeBasePath } ` ) ;
43+
44+ // Setup index redirect page based on route configuration
45+ if ( routeBasePath === '/' ) {
46+ // If docs are at root, remove the index redirect page
47+ console . log ( 'Docs are at root (/), removing index redirect page, if it exists...' ) ;
48+ removeIndexPage ( ) ;
49+ } else {
50+ // If docs are not at root, ensure the index redirect page exists
51+ console . log ( `Docs are at ${ routeBasePath } , creating index redirect page...` ) ;
52+
53+ // Create pages directory if it doesn't exist
54+ if ( ! fs . existsSync ( pagesDir ) ) {
55+ fs . mkdirSync ( pagesDir , { recursive : true } ) ;
56+ }
57+
58+ // Create the redirect page
59+ const redirectContent = `import React from 'react';
60+ import { Redirect } from '@docusaurus/router';
61+
62+ export default function Home(): JSX.Element {
63+ // Redirect to the docs location
64+ return <Redirect to="${ routeBasePath } " />;
65+ }
66+ ` ;
67+
68+ fs . writeFileSync ( indexPagePath , redirectContent ) ;
69+ console . log ( `Created index redirect to ${ routeBasePath } ` ) ;
70+ }
0 commit comments