1+ import fs from 'fs' ;
2+ import path from 'path' ;
3+ import { fileURLToPath } from 'url' ;
4+
5+ // Get __dirname equivalent in ESM
6+ const __filename = fileURLToPath ( import . meta. url ) ;
7+ const __dirname = path . dirname ( __filename ) ;
8+
9+ // Function to ensure directory exists
10+ function ensureDirectoryExists ( dirPath ) {
11+ if ( ! fs . existsSync ( dirPath ) ) {
12+ fs . mkdirSync ( dirPath , { recursive : true } ) ;
13+ }
14+ }
15+
16+ // Function to copy markdown files
17+ function copyMarkdownFiles ( ) {
18+ const sourceDir = path . join ( __dirname , '..' , 'docs' ) ;
19+ const targetDir = path . join ( __dirname , '..' , 'build' , 'docs' , 'gatsby' , 'src' , 'docs' ) ;
20+
21+ // Ensure target directory exists
22+ ensureDirectoryExists ( targetDir ) ;
23+
24+ // Read all files from source directory
25+ const files = fs . readdirSync ( sourceDir ) ;
26+
27+ // Copy each markdown file
28+ files . forEach ( file => {
29+ if ( file . endsWith ( '.md' ) ) {
30+ const sourcePath = path . join ( sourceDir , file ) ;
31+ const targetPath = path . join ( targetDir , file ) ;
32+ fs . copyFileSync ( sourcePath , targetPath ) ;
33+ console . log ( `Copied ${ file } to ${ targetPath } ` ) ;
34+ }
35+ } ) ;
36+ }
37+
38+ // Function to update menu links
39+ function updateMenuLinks ( ) {
40+ const configPath = path . join ( __dirname , '..' , 'build' , 'docs' , 'gatsby' , 'gatsby-config.js' ) ;
41+
42+ // Read the config file
43+ let configContent = fs . readFileSync ( configPath , 'utf8' ) ;
44+
45+ // Find the menuLinks array
46+ const menuLinksRegex = / m e n u L i n k s : \s * \[ ( [ \s \S ] * ?) \] / ;
47+ const match = configContent . match ( menuLinksRegex ) ;
48+
49+ if ( match ) {
50+ // Add the new menu link
51+ const newMenuLink = ` {
52+ name: 'Contributors Guide',
53+ link: '/making-changes',
54+ ignoreNextPrev: true
55+ },` ;
56+
57+ // Remove the 'home' menu link and add the new one
58+ const menuLinksContent = match [ 1 ]
59+ . replace ( / , \s * { \s * n a m e : \s * ' h o m e ' , [ ^ } ] * } , / g, '' ) ;
60+
61+ // Insert the new menu link before the closing bracket
62+ const updatedContent = configContent . replace (
63+ menuLinksRegex ,
64+ `menuLinks: [${ newMenuLink } ${ menuLinksContent } ]`
65+ ) ;
66+
67+ // Write the updated content back to the file
68+ fs . writeFileSync ( configPath , updatedContent ) ;
69+ console . log ( 'Updated menu links in gatsby-config.js' ) ;
70+ } else {
71+ console . error ( 'Could not find menuLinks array in gatsby-config.js' ) ;
72+ }
73+ }
74+
75+ // Execute the functions
76+ try {
77+ copyMarkdownFiles ( ) ;
78+ updateMenuLinks ( ) ;
79+ console . log ( 'Documentation preparation completed successfully!' ) ;
80+ } catch ( error ) {
81+ console . error ( 'Error preparing documentation:' , error ) ;
82+ process . exit ( 1 ) ;
83+ }
0 commit comments