| #!/usr/bin/env node |
|
|
| const fs = require("fs"); |
| const path = require("path"); |
|
|
| function calculateDepth(filePath) { |
| |
| |
| const dir = path.dirname(filePath); |
| if (dir === "." || dir === "/") { |
| return 0; |
| } |
| return dir.split(path.sep).filter((part) => part !== "").length; |
| } |
|
|
| function generateRelativePath(depth) { |
| if (depth === 0) { |
| return "./"; |
| } |
| return "../".repeat(depth); |
| } |
|
|
| function fixPathsInFile(filePath) { |
| const content = fs.readFileSync(filePath, "utf8"); |
| const depth = calculateDepth(filePath); |
| const relativePath = generateRelativePath(depth); |
|
|
| console.log(`Processing ${filePath} (depth: ${depth}, prefix: ${relativePath})`); |
|
|
| |
| |
| const updatedContent = content.replace(/"\/_next\//g, `"${relativePath}_next/`); |
|
|
| if (content !== updatedContent) { |
| fs.writeFileSync(filePath, updatedContent); |
| console.log(` ✓ Fixed paths in ${filePath}`); |
| } else { |
| console.log(` - No changes needed in ${filePath}`); |
| } |
| } |
|
|
| function findHtmlFiles(dir) { |
| const files = []; |
|
|
| function traverse(currentDir) { |
| const items = fs.readdirSync(currentDir); |
|
|
| for (const item of items) { |
| const fullPath = path.join(currentDir, item); |
| const stat = fs.statSync(fullPath); |
|
|
| if (stat.isDirectory()) { |
| traverse(fullPath); |
| } else if (item.endsWith(".html")) { |
| |
| const relativePath = path.relative("out", fullPath); |
| files.push(relativePath); |
| } |
| } |
| } |
|
|
| traverse(dir); |
| return files; |
| } |
|
|
| |
| const outDir = "out"; |
| if (!fs.existsSync(outDir)) { |
| console.error("Output directory not found. Please run next build first."); |
| process.exit(1); |
| } |
|
|
| console.log("Finding HTML files..."); |
| const htmlFiles = findHtmlFiles(outDir); |
|
|
| console.log(`Found ${htmlFiles.length} HTML files`); |
|
|
| for (const file of htmlFiles) { |
| const fullPath = path.join(outDir, file); |
|
|
| |
| if (file === "index.html" || file === "404.html") { |
| console.log(`Skipping root file: ${file}`); |
| continue; |
| } |
|
|
| fixPathsInFile(fullPath); |
| } |
|
|
| console.log("✅ Path fixing complete!"); |
|
|