|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Local docs copy script - copies SDK docs to a local mintlify-docs repo. |
| 5 | + * |
| 6 | + * Usage: |
| 7 | + * node copy-to-local-docs.js [--target <path-to-mintlify-docs>] |
| 8 | + * |
| 9 | + * Options: |
| 10 | + * --target <path> Path to the mintlify-docs repo. Defaults to ../mintlify-docs |
| 11 | + * (assumes both repos are in the same parent folder) |
| 12 | + * |
| 13 | + * Examples: |
| 14 | + * node copy-to-local-docs.js |
| 15 | + * node copy-to-local-docs.js --target ~/Projects/mintlify-docs |
| 16 | + * npm run copy-docs-local |
| 17 | + * npm run copy-docs-local -- --target ~/Projects/mintlify-docs |
| 18 | + */ |
| 19 | + |
| 20 | +import fs from "fs"; |
| 21 | +import path from "path"; |
| 22 | + |
| 23 | +console.debug = () => {}; // Disable debug logging. Comment this out to enable debug logging. |
| 24 | + |
| 25 | +const DOCS_SOURCE_PATH = path.join(import.meta.dirname, "../../docs/content"); |
| 26 | +const CATEGORY_MAP_PATH = path.join(import.meta.dirname, "./category-map.json"); |
| 27 | + |
| 28 | +// Default: assume mintlify-docs is a sibling directory to javascript-sdk |
| 29 | +const SDK_ROOT = path.join(import.meta.dirname, "../.."); |
| 30 | +const DEFAULT_TARGET = path.join(SDK_ROOT, "../mintlify-docs"); |
| 31 | + |
| 32 | +function parseArgs() { |
| 33 | + const args = process.argv.slice(2); |
| 34 | + let target = DEFAULT_TARGET; |
| 35 | + |
| 36 | + for (let i = 0; i < args.length; i++) { |
| 37 | + const arg = args[i]; |
| 38 | + |
| 39 | + if ((arg === "--target" || arg === "-t") && i + 1 < args.length) { |
| 40 | + target = args[++i]; |
| 41 | + // Expand ~ to home directory |
| 42 | + if (target.startsWith("~")) { |
| 43 | + target = path.join(process.env.HOME, target.slice(1)); |
| 44 | + } |
| 45 | + // Resolve to absolute path |
| 46 | + target = path.resolve(target); |
| 47 | + } |
| 48 | + |
| 49 | + if (arg === "--help" || arg === "-h") { |
| 50 | + console.log(` |
| 51 | +Local docs copy script - copies SDK docs to a local mintlify-docs repo. |
| 52 | +
|
| 53 | +Usage: |
| 54 | + node copy-to-local-docs.js [--target <path-to-mintlify-docs>] |
| 55 | +
|
| 56 | +Options: |
| 57 | + --target, -t <path> Path to the mintlify-docs repo. |
| 58 | + Defaults to ../mintlify-docs (sibling directory) |
| 59 | + --help, -h Show this help message |
| 60 | +
|
| 61 | +Examples: |
| 62 | + node copy-to-local-docs.js |
| 63 | + node copy-to-local-docs.js --target ~/Projects/mintlify-docs |
| 64 | + npm run copy-docs-local |
| 65 | + npm run copy-docs-local -- --target ~/Projects/mintlify-docs |
| 66 | +`); |
| 67 | + process.exit(0); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + return { target }; |
| 72 | +} |
| 73 | + |
| 74 | +function scanSdkDocs(sdkDocsDir) { |
| 75 | + const result = {}; |
| 76 | + |
| 77 | + // Get a list of all the subdirectories in the sdkDocsDir |
| 78 | + const subdirectories = fs |
| 79 | + .readdirSync(sdkDocsDir) |
| 80 | + .filter((file) => fs.statSync(path.join(sdkDocsDir, file)).isDirectory()); |
| 81 | + console.log(`Subdirectories: ${subdirectories}`); |
| 82 | + |
| 83 | + for (const subdirectory of subdirectories) { |
| 84 | + const subdirectoryPath = path.join(sdkDocsDir, subdirectory); |
| 85 | + const files = fs |
| 86 | + .readdirSync(subdirectoryPath) |
| 87 | + .filter((file) => file.endsWith(".mdx")); |
| 88 | + result[subdirectory] = files.map((file) => path.basename(file, ".mdx")); |
| 89 | + } |
| 90 | + return result; |
| 91 | +} |
| 92 | + |
| 93 | +function updateDocsJson(repoDir, sdkFiles) { |
| 94 | + const docsJsonPath = path.join(repoDir, "docs.json"); |
| 95 | + let categoryMap = {}; |
| 96 | + try { |
| 97 | + categoryMap = JSON.parse(fs.readFileSync(CATEGORY_MAP_PATH, "utf8")); |
| 98 | + } catch (e) { |
| 99 | + console.error(`Error: Category map file not found: ${CATEGORY_MAP_PATH}`); |
| 100 | + process.exit(1); |
| 101 | + } |
| 102 | + |
| 103 | + console.log(`Reading docs.json from ${docsJsonPath}...`); |
| 104 | + const docsContent = fs.readFileSync(docsJsonPath, "utf8"); |
| 105 | + const docs = JSON.parse(docsContent); |
| 106 | + |
| 107 | + // Build the new SDK Reference groups |
| 108 | + const groupMap = new Map(); // group name -> pages array |
| 109 | + |
| 110 | + const addToGroup = (groupName, pages) => { |
| 111 | + if (!groupName || pages.length === 0) return; |
| 112 | + if (!groupMap.has(groupName)) { |
| 113 | + groupMap.set(groupName, []); |
| 114 | + } |
| 115 | + groupMap.get(groupName).push(...pages); |
| 116 | + }; |
| 117 | + |
| 118 | + if (sdkFiles.functions?.length > 0 && categoryMap.functions) { |
| 119 | + addToGroup( |
| 120 | + categoryMap.functions, |
| 121 | + sdkFiles.functions.map((file) => `sdk-docs/functions/${file}`) |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + if (sdkFiles.interfaces?.length > 0 && categoryMap.interfaces) { |
| 126 | + addToGroup( |
| 127 | + categoryMap.interfaces, |
| 128 | + sdkFiles.interfaces.map((file) => `sdk-docs/interfaces/${file}`) |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + if (sdkFiles.classes?.length > 0 && categoryMap.classes) { |
| 133 | + addToGroup( |
| 134 | + categoryMap.classes, |
| 135 | + sdkFiles.classes.map((file) => `sdk-docs/classes/${file}`) |
| 136 | + ); |
| 137 | + } |
| 138 | + |
| 139 | + if (sdkFiles["type-aliases"]?.length > 0 && categoryMap["type-aliases"]) { |
| 140 | + addToGroup( |
| 141 | + categoryMap["type-aliases"], |
| 142 | + sdkFiles["type-aliases"].map((file) => `sdk-docs/type-aliases/${file}`) |
| 143 | + ); |
| 144 | + } |
| 145 | + |
| 146 | + // Convert map to array of nested groups for SDK Reference |
| 147 | + const sdkReferencePages = Array.from(groupMap.entries()).map( |
| 148 | + ([groupName, pages]) => ({ |
| 149 | + group: groupName, |
| 150 | + pages: pages.sort(), // Sort pages alphabetically within each group |
| 151 | + }) |
| 152 | + ); |
| 153 | + |
| 154 | + console.debug( |
| 155 | + `SDK Reference pages: ${JSON.stringify(sdkReferencePages, null, 2)}` |
| 156 | + ); |
| 157 | + |
| 158 | + // Navigate to: Developers tab -> SDK group -> SDK Reference group |
| 159 | + const developersTab = docs.navigation.tabs.find( |
| 160 | + (tab) => tab.tab === "Developers" |
| 161 | + ); |
| 162 | + |
| 163 | + if (!developersTab) { |
| 164 | + console.error("Could not find 'Developers' tab in docs.json"); |
| 165 | + process.exit(1); |
| 166 | + } |
| 167 | + |
| 168 | + // Find the SDK group (it's a top-level group in the Developers tab) |
| 169 | + const sdkGroup = developersTab.groups.find((g) => g.group === "SDK"); |
| 170 | + |
| 171 | + if (!sdkGroup) { |
| 172 | + console.error("Could not find 'SDK' group in Developers tab"); |
| 173 | + process.exit(1); |
| 174 | + } |
| 175 | + |
| 176 | + // Find SDK Reference within SDK's pages (it's a nested group object) |
| 177 | + const sdkRefIndex = sdkGroup.pages.findIndex( |
| 178 | + (page) => typeof page === "object" && page.group === "SDK Reference" |
| 179 | + ); |
| 180 | + |
| 181 | + if (sdkRefIndex === -1) { |
| 182 | + console.error("Could not find 'SDK Reference' group in SDK"); |
| 183 | + process.exit(1); |
| 184 | + } |
| 185 | + |
| 186 | + // Update the SDK Reference pages with our generated groups |
| 187 | + sdkGroup.pages[sdkRefIndex] = { |
| 188 | + group: "SDK Reference", |
| 189 | + icon: "brackets-curly", |
| 190 | + pages: sdkReferencePages, |
| 191 | + }; |
| 192 | + |
| 193 | + // Remove the old standalone "SDK Reference" tab if it exists |
| 194 | + const oldSdkTabIndex = docs.navigation.tabs.findIndex( |
| 195 | + (tab) => tab.tab === "SDK Reference" |
| 196 | + ); |
| 197 | + if (oldSdkTabIndex !== -1) { |
| 198 | + console.log("Removing old standalone 'SDK Reference' tab..."); |
| 199 | + docs.navigation.tabs.splice(oldSdkTabIndex, 1); |
| 200 | + } |
| 201 | + |
| 202 | + // Write updated docs.json |
| 203 | + console.log(`Writing updated docs.json to ${docsJsonPath}...`); |
| 204 | + fs.writeFileSync(docsJsonPath, JSON.stringify(docs, null, 2) + "\n", "utf8"); |
| 205 | + |
| 206 | + console.log("Successfully updated docs.json"); |
| 207 | +} |
| 208 | + |
| 209 | +function main() { |
| 210 | + const { target } = parseArgs(); |
| 211 | + |
| 212 | + console.log(`Source: ${DOCS_SOURCE_PATH}`); |
| 213 | + console.log(`Target: ${target}`); |
| 214 | + |
| 215 | + // Validate source exists |
| 216 | + if ( |
| 217 | + !fs.existsSync(DOCS_SOURCE_PATH) || |
| 218 | + !fs.statSync(DOCS_SOURCE_PATH).isDirectory() |
| 219 | + ) { |
| 220 | + console.error(`Error: docs directory does not exist: ${DOCS_SOURCE_PATH}`); |
| 221 | + console.error("Have you run 'npm run create-docs' first?"); |
| 222 | + process.exit(1); |
| 223 | + } |
| 224 | + |
| 225 | + // Validate target exists and looks like a mintlify-docs repo |
| 226 | + if (!fs.existsSync(target) || !fs.statSync(target).isDirectory()) { |
| 227 | + console.error(`Error: target directory does not exist: ${target}`); |
| 228 | + process.exit(1); |
| 229 | + } |
| 230 | + |
| 231 | + const docsJsonPath = path.join(target, "docs.json"); |
| 232 | + if (!fs.existsSync(docsJsonPath)) { |
| 233 | + console.error( |
| 234 | + `Error: docs.json not found in ${target}. Is this a mintlify-docs repo?` |
| 235 | + ); |
| 236 | + process.exit(1); |
| 237 | + } |
| 238 | + |
| 239 | + try { |
| 240 | + // Remove the existing sdk-docs directory |
| 241 | + const sdkDocsTarget = path.join(target, "sdk-docs"); |
| 242 | + if (fs.existsSync(sdkDocsTarget)) { |
| 243 | + console.log(`Removing existing sdk-docs directory...`); |
| 244 | + fs.rmSync(sdkDocsTarget, { recursive: true, force: true }); |
| 245 | + } |
| 246 | + |
| 247 | + // Copy the docs directory to the target |
| 248 | + console.log(`Copying docs to ${sdkDocsTarget}...`); |
| 249 | + fs.cpSync(DOCS_SOURCE_PATH, sdkDocsTarget, { recursive: true }); |
| 250 | + |
| 251 | + // Scan the sdk-docs directory |
| 252 | + const sdkFiles = scanSdkDocs(sdkDocsTarget); |
| 253 | + console.debug(`SDK files: ${JSON.stringify(sdkFiles, null, 2)}`); |
| 254 | + |
| 255 | + // Update the docs.json file |
| 256 | + updateDocsJson(target, sdkFiles); |
| 257 | + |
| 258 | + console.log("\n✅ Successfully copied SDK docs to local mintlify-docs repo"); |
| 259 | + console.log(`\nTo preview the docs, run 'mintlify dev' in ${target}`); |
| 260 | + } catch (e) { |
| 261 | + console.error(`Error: Failed to copy docs: ${e}`); |
| 262 | + process.exit(1); |
| 263 | + } |
| 264 | +} |
| 265 | + |
| 266 | +main(); |
0 commit comments