|
| 1 | +#!/usr/bin/env tsx |
| 2 | + |
| 3 | +/** |
| 4 | + * This script collapses the folder structure in src/content/docs/pages/tutorials |
| 5 | + * by moving index.mdx files to the parent directory with the folder name as the filename. |
| 6 | + * |
| 7 | + * Usage: |
| 8 | + * npx tsx bin/collapse-tutorials.ts |
| 9 | + */ |
| 10 | + |
| 11 | +import fs from "fs/promises"; |
| 12 | +import path from "path"; |
| 13 | +import globby from "fast-glob"; |
| 14 | + |
| 15 | +// Explicitly set the path to pages/tutorials |
| 16 | +const TUTORIALS_DIR = path.join( |
| 17 | + process.cwd(), |
| 18 | + "src/content/docs/workers/tutorials", |
| 19 | +); |
| 20 | + |
| 21 | +/** |
| 22 | + * Main function |
| 23 | + */ |
| 24 | +async function main() { |
| 25 | + try { |
| 26 | + // Find all subdirectories in the tutorials directory |
| 27 | + const directories = await fs.readdir(TUTORIALS_DIR, { |
| 28 | + withFileTypes: true, |
| 29 | + }); |
| 30 | + const subdirs = directories |
| 31 | + .filter((dirent) => dirent.isDirectory()) |
| 32 | + .map((dirent) => dirent.name); |
| 33 | + |
| 34 | + console.log(`Found ${subdirs.length} subdirectories in ${TUTORIALS_DIR}`); |
| 35 | + |
| 36 | + let successCount = 0; |
| 37 | + let skipCount = 0; |
| 38 | + let errorCount = 0; |
| 39 | + |
| 40 | + // Process each subdirectory |
| 41 | + for (const subdir of subdirs) { |
| 42 | + const subdirPath = path.join(TUTORIALS_DIR, subdir); |
| 43 | + const indexPath = path.join(subdirPath, "index.mdx"); |
| 44 | + const newPath = path.join(TUTORIALS_DIR, `${subdir}.mdx`); |
| 45 | + |
| 46 | + try { |
| 47 | + // Check if index.mdx exists |
| 48 | + try { |
| 49 | + await fs.access(indexPath); |
| 50 | + } catch (error) { |
| 51 | + console.warn(`⚠️ No index.mdx found in ${subdir}/`); |
| 52 | + skipCount++; |
| 53 | + continue; |
| 54 | + } |
| 55 | + |
| 56 | + // Check if target file already exists |
| 57 | + try { |
| 58 | + await fs.access(newPath); |
| 59 | + console.warn(`⚠️ Target file ${subdir}.mdx already exists, skipping`); |
| 60 | + skipCount++; |
| 61 | + continue; |
| 62 | + } catch (error) { |
| 63 | + // File doesn't exist, which is what we want |
| 64 | + } |
| 65 | + |
| 66 | + // Read the content of index.mdx |
| 67 | + const content = await fs.readFile(indexPath, "utf-8"); |
| 68 | + |
| 69 | + // Write content to new file |
| 70 | + await fs.writeFile(newPath, content, "utf-8"); |
| 71 | + |
| 72 | + // Remove the original index.mdx file |
| 73 | + await fs.unlink(indexPath); |
| 74 | + |
| 75 | + console.log( |
| 76 | + `✅ Moved ${subdir}/index.mdx to ${subdir}.mdx and removed original`, |
| 77 | + ); |
| 78 | + successCount++; |
| 79 | + } catch (error) { |
| 80 | + console.error(`❌ Error processing ${subdir}:`, error); |
| 81 | + errorCount++; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + console.log("\n--- Summary ---"); |
| 86 | + console.log(`Total subdirectories: ${subdirs.length}`); |
| 87 | + console.log(`Successfully moved: ${successCount}`); |
| 88 | + console.log(`Skipped: ${skipCount}`); |
| 89 | + console.log(`Errors: ${errorCount}`); |
| 90 | + } catch (error) { |
| 91 | + console.error("Error:", error); |
| 92 | + process.exit(1); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +main(); |
0 commit comments