Skip to content

Commit bcaaffe

Browse files
committed
Update prepare script
1 parent 4e5e35b commit bcaaffe

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
"scripts": {
88
"build": "npm run build:spec",
99
"build:spec": "node scripts/build.js",
10-
"build:docs": "cd build/docs/gatsby && npm install && gatsby build --prefix-paths",
10+
"build:docs": "npm run generate-clients && npm run build:docs:gatsby && npm run copy-docs",
1111
"lint": "node scripts/build.js && node scripts/validate.js && node scripts/graphql-validate.js",
1212
"clean": "rm -rf build && mkdir -p build",
13+
"copy-docs": "node scripts/prepare-docs.js",
14+
"build:docs:gatsby": "cd build/docs/gatsby && npm install && gatsby build --prefix-paths",
1315
"generate-clients": "mkdir -p build && open-rpc-generator generate -c open-rpc-generator-config.json",
1416
"graphql:schema": "node scripts/graphql.js",
1517
"graphql:validate": "node scripts/graphql-validate.js",

scripts/prepare-docs.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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 = /menuLinks:\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*name:\s*'home',[^}]*},/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

Comments
 (0)