|
| 1 | +#!/usr/bin/env node |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +import fs from 'fs'; |
| 5 | +import path from 'path'; |
| 6 | +import {parse} from '@babel/parser'; |
| 7 | +import {fileURLToPath} from 'url'; |
| 8 | + |
| 9 | +const __filename = fileURLToPath(import.meta.url); |
| 10 | +const __dirname = path.dirname(__filename); |
| 11 | +const ROOT = path.resolve(__dirname, '..'); |
| 12 | +const CONTENT_DIR = path.join(ROOT, 'src', 'content'); |
| 13 | +const DRY_RUN = process.argv.includes('--dry-run'); |
| 14 | + |
| 15 | +// --------------------------------------------------------------------------- |
| 16 | +// JSX detection via Babel AST |
| 17 | +// --------------------------------------------------------------------------- |
| 18 | + |
| 19 | +function containsJSX(code) { |
| 20 | + try { |
| 21 | + const ast = parse(code, { |
| 22 | + sourceType: 'module', |
| 23 | + plugins: ['jsx'], |
| 24 | + errorRecovery: true, |
| 25 | + }); |
| 26 | + return walkForJSX(ast); |
| 27 | + } catch { |
| 28 | + return /<[A-Z]|<>|<\/>/.test(code); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +function walkForJSX(node, seen = new WeakSet()) { |
| 33 | + if (!node || typeof node !== 'object' || seen.has(node)) return false; |
| 34 | + seen.add(node); |
| 35 | + if (node.type === 'JSXElement' || node.type === 'JSXFragment') return true; |
| 36 | + for (const val of Object.values(node)) { |
| 37 | + if (Array.isArray(val)) { |
| 38 | + for (const item of val) { |
| 39 | + if (item && typeof item === 'object' && walkForJSX(item, seen)) |
| 40 | + return true; |
| 41 | + } |
| 42 | + } else if (val && typeof val === 'object') { |
| 43 | + if (walkForJSX(val, seen)) return true; |
| 44 | + } |
| 45 | + } |
| 46 | + return false; |
| 47 | +} |
| 48 | + |
| 49 | +// --------------------------------------------------------------------------- |
| 50 | +// File discovery |
| 51 | +// --------------------------------------------------------------------------- |
| 52 | + |
| 53 | +function collectMDX(dir) { |
| 54 | + const out = []; |
| 55 | + for (const ent of fs.readdirSync(dir, {withFileTypes: true})) { |
| 56 | + const full = path.join(dir, ent.name); |
| 57 | + if (ent.isDirectory()) out.push(...collectMDX(full)); |
| 58 | + else if (/\.mdx?$/.test(ent.name)) out.push(full); |
| 59 | + } |
| 60 | + return out; |
| 61 | +} |
| 62 | + |
| 63 | +// --------------------------------------------------------------------------- |
| 64 | +// Helpers |
| 65 | +// --------------------------------------------------------------------------- |
| 66 | + |
| 67 | +function escapeRe(s) { |
| 68 | + return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); |
| 69 | +} |
| 70 | + |
| 71 | +// --------------------------------------------------------------------------- |
| 72 | +// Process one MDX file |
| 73 | +// --------------------------------------------------------------------------- |
| 74 | + |
| 75 | +function processFile(filePath) { |
| 76 | + const src = fs.readFileSync(filePath, 'utf8'); |
| 77 | + if (!/<Sandpack|<SandpackRSC/.test(src)) return null; |
| 78 | + |
| 79 | + const lines = src.split('\n'); |
| 80 | + let changed = false; |
| 81 | + const renames = []; |
| 82 | + |
| 83 | + // 1. Find Sandpack / SandpackRSC block ranges |
| 84 | + const blocks = []; |
| 85 | + let bStart = -1; |
| 86 | + let bTag = null; |
| 87 | + for (let i = 0; i < lines.length; i++) { |
| 88 | + const t = lines[i].trim(); |
| 89 | + if (bStart === -1) { |
| 90 | + const m = t.match(/^<(Sandpack|SandpackRSC)([\s>]|$)/); |
| 91 | + if (m) { |
| 92 | + bStart = i; |
| 93 | + bTag = m[1]; |
| 94 | + } |
| 95 | + } else if (t === `</${bTag}>`) { |
| 96 | + blocks.push({start: bStart, end: i}); |
| 97 | + bStart = -1; |
| 98 | + bTag = null; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + // 2. Process each block |
| 103 | + for (const block of blocks) { |
| 104 | + // 2a. Parse code fences |
| 105 | + const fences = []; |
| 106 | + let inFence = false; |
| 107 | + let fMeta = '', |
| 108 | + fLang = '', |
| 109 | + fMetaLine = -1, |
| 110 | + fCodeStart = -1; |
| 111 | + |
| 112 | + for (let i = block.start + 1; i < block.end; i++) { |
| 113 | + if (!inFence) { |
| 114 | + const m = lines[i].match(/^```(\w+)\s*(.*)$/); |
| 115 | + if (m) { |
| 116 | + inFence = true; |
| 117 | + fMetaLine = i; |
| 118 | + fLang = m[1]; |
| 119 | + fMeta = m[2].trim(); |
| 120 | + fCodeStart = i + 1; |
| 121 | + } |
| 122 | + } else if (lines[i].trim() === '```') { |
| 123 | + fences.push({ |
| 124 | + metaLine: fMetaLine, |
| 125 | + codeStart: fCodeStart, |
| 126 | + codeEnd: i - 1, |
| 127 | + lang: fLang, |
| 128 | + meta: fMeta, |
| 129 | + }); |
| 130 | + inFence = false; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + // 2b. Identify .js files containing JSX → build rename map |
| 135 | + const renameMap = new Map(); // basename.js → basename.jsx |
| 136 | + |
| 137 | + for (const f of fences) { |
| 138 | + if (f.lang !== 'js' && f.lang !== 'jsx') continue; |
| 139 | + |
| 140 | + const tokens = f.meta |
| 141 | + .split(/\s+/) |
| 142 | + .filter((tok) => tok && !tok.startsWith('{')); |
| 143 | + const jsName = tokens.find((tok) => tok.endsWith('.js')); |
| 144 | + if (!jsName) continue; |
| 145 | + |
| 146 | + const code = |
| 147 | + f.codeStart <= f.codeEnd |
| 148 | + ? lines.slice(f.codeStart, f.codeEnd + 1).join('\n') |
| 149 | + : ''; |
| 150 | + if (!code.trim()) continue; |
| 151 | + |
| 152 | + if (containsJSX(code)) { |
| 153 | + const base = path.basename(jsName); |
| 154 | + renameMap.set(base, base.replace(/\.js$/, '.jsx')); |
| 155 | + |
| 156 | + const newName = jsName.replace(/\.js$/, '.jsx'); |
| 157 | + lines[f.metaLine] = lines[f.metaLine].replace(jsName, newName); |
| 158 | + changed = true; |
| 159 | + renames.push(`${jsName} → ${newName}`); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + if (renameMap.size === 0) continue; |
| 164 | + |
| 165 | + // 2c. Update imports referencing renamed files |
| 166 | + for (const f of fences) { |
| 167 | + if (f.lang !== 'js' && f.lang !== 'jsx') continue; |
| 168 | + |
| 169 | + for (let i = f.codeStart; i <= f.codeEnd; i++) { |
| 170 | + let line = lines[i]; |
| 171 | + for (const [oldBase, newBase] of renameMap) { |
| 172 | + const re = new RegExp( |
| 173 | + `(?<=/)${escapeRe(oldBase)}(?=['"])`, |
| 174 | + 'g' |
| 175 | + ); |
| 176 | + const updated = line.replace(re, newBase); |
| 177 | + if (updated !== line) { |
| 178 | + line = updated; |
| 179 | + changed = true; |
| 180 | + } |
| 181 | + } |
| 182 | + lines[i] = line; |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + if (!changed) return null; |
| 188 | + |
| 189 | + if (!DRY_RUN) { |
| 190 | + fs.writeFileSync(filePath, lines.join('\n'), 'utf8'); |
| 191 | + } |
| 192 | + return renames; |
| 193 | +} |
| 194 | + |
| 195 | +// --------------------------------------------------------------------------- |
| 196 | +// Main |
| 197 | +// --------------------------------------------------------------------------- |
| 198 | + |
| 199 | +console.log( |
| 200 | + `${DRY_RUN ? '[DRY RUN] ' : ''}Scanning ${path.relative(ROOT, CONTENT_DIR)} …\n` |
| 201 | +); |
| 202 | + |
| 203 | +const files = collectMDX(CONTENT_DIR); |
| 204 | +let modCount = 0; |
| 205 | +let renameCount = 0; |
| 206 | + |
| 207 | +for (const f of files) { |
| 208 | + const result = processFile(f); |
| 209 | + if (result) { |
| 210 | + modCount++; |
| 211 | + renameCount += result.length; |
| 212 | + console.log(path.relative(ROOT, f)); |
| 213 | + for (const r of result) console.log(` ${r}`); |
| 214 | + } |
| 215 | +} |
| 216 | + |
| 217 | +console.log( |
| 218 | + `\n${DRY_RUN ? 'Would modify' : 'Modified'} ${modCount} file(s), ${renameCount} rename(s).` |
| 219 | +); |
0 commit comments