|
| 1 | +/* |
| 2 | + * The script logs locations of affected URLs due to following reasons: |
| 3 | + * - file deletion |
| 4 | + * - Markdown header updates |
| 5 | + */ |
| 6 | + |
| 7 | +import fs from "node:fs/promises"; |
| 8 | +import path from "node:path"; |
| 9 | +import { |
| 10 | + execGit, |
| 11 | + getRootDir, |
| 12 | + walkSync, |
| 13 | + isImagePath, |
| 14 | + getLocations, |
| 15 | + IMG_RX, |
| 16 | + stringToFragment, |
| 17 | +} from "./utils.js"; |
| 18 | + |
| 19 | +const rootDir = getRootDir(); |
| 20 | +const argLength = process.argv.length; |
| 21 | +const deletedSlugs = []; |
| 22 | +const fragmentDetails = []; |
| 23 | +let isAllOk = true; |
| 24 | + |
| 25 | +function getDeletedSlugs() { |
| 26 | + // git status --short --porcelain |
| 27 | + let result = execGit(["status", "--short", "--porcelain"], { cwd: "." }); |
| 28 | + |
| 29 | + if (result.trim()) { |
| 30 | + deletedSlugs.push( |
| 31 | + ...result |
| 32 | + .split("\n") |
| 33 | + .filter( |
| 34 | + (line) => |
| 35 | + /^\s*D\s+/gi.test(line) && |
| 36 | + line.includes("files/en-us") && |
| 37 | + (IMG_RX.test(line) || line.includes("index.md")), |
| 38 | + ) |
| 39 | + .map((line) => line.replaceAll(/^\s*|files\/en-us\/|\/index.md/gm, "")) |
| 40 | + .map((line) => line.split(/\s+/)[1]), |
| 41 | + ); |
| 42 | + } |
| 43 | + console.log("deletedSlugs", deletedSlugs); |
| 44 | +} |
| 45 | + |
| 46 | +function getFragmentDetails(fromStaging = true) { |
| 47 | + let result = ""; |
| 48 | + |
| 49 | + if (fromStaging) { |
| 50 | + // get staged and unstaged changes |
| 51 | + result = execGit(["diff", "HEAD"], { cwd: "." }); |
| 52 | + } else { |
| 53 | + // get diff between branch base and HEAD |
| 54 | + result = execGit(["diff", "origin/main...HEAD"], { cwd: "." }); |
| 55 | + } |
| 56 | + |
| 57 | + if (result.trim()) { |
| 58 | + const segments = [ |
| 59 | + ...result.split("diff --git a/").filter((segment) => segment !== ""), |
| 60 | + ]; |
| 61 | + for (const segment of segments) { |
| 62 | + const path = segment |
| 63 | + .substring(0, segment.indexOf(" ")) |
| 64 | + .replaceAll(/files\/en-us\/|\/index.md/gm, ""); |
| 65 | + |
| 66 | + const headerRx = /^-#+ .*$/gm; |
| 67 | + const fragments = [...segment.matchAll(headerRx)] |
| 68 | + .map((match) => match[0].toLowerCase()) |
| 69 | + .map((header) => header.replace(/-#+ /g, "")) |
| 70 | + .map((header) => stringToFragment(header)); |
| 71 | + |
| 72 | + for (const fragment of fragments) { |
| 73 | + fragmentDetails.push(`${path}#${fragment}`); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + console.log("fragmentDetails", fragmentDetails); |
| 78 | +} |
| 79 | + |
| 80 | +if (process.argv[2] !== "--workflow") { |
| 81 | + getDeletedSlugs(); |
| 82 | + getFragmentDetails(); |
| 83 | +} else { |
| 84 | + getFragmentDetails(false); |
| 85 | +} |
| 86 | + |
| 87 | +if (deletedSlugs.length < 1 && fragmentDetails.length < 1) { |
| 88 | + console.log("Nothing to check. 🎉"); |
| 89 | + process.exit(0); |
| 90 | +} |
| 91 | + |
| 92 | +for await (const filePath of walkSync(getRootDir())) { |
| 93 | + if (filePath.endsWith("index.md")) { |
| 94 | + try { |
| 95 | + const content = await fs.readFile(filePath, "utf-8"); |
| 96 | + const relativePath = filePath.substring(filePath.indexOf("files/en-us")); |
| 97 | + |
| 98 | + // check deleted links |
| 99 | + for (const slug of deletedSlugs) { |
| 100 | + isAllOk = false; |
| 101 | + const locations = getLocations( |
| 102 | + content, |
| 103 | + new RegExp(`/${slug}[)># \"']`, "mig"), |
| 104 | + ); |
| 105 | + if (locations.length) { |
| 106 | + for (const location of locations) { |
| 107 | + console.error( |
| 108 | + `ERROR:${relativePath}:${location.line}:${location.column}:Slug '${slug}' has been deleted`, |
| 109 | + ); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + // check broken URL fragment |
| 115 | + for (const fragment of fragmentDetails) { |
| 116 | + isAllOk = false; |
| 117 | + const locations = getLocations(content, fragment); |
| 118 | + // check fragments in the same file |
| 119 | + const urlParts = fragment.split("#"); |
| 120 | + if (filePath.includes(urlParts[0])) { |
| 121 | + locations.push(...getLocations(content, urlParts[1])); |
| 122 | + } |
| 123 | + if (locations.length) { |
| 124 | + for (const location of locations) { |
| 125 | + console.error( |
| 126 | + `ERROR:${relativePath}:${location.line}:${location.column}:URL fragment in URL '${fragment}' is broken`, |
| 127 | + ); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } catch (e) { |
| 132 | + console.error(`Error processing ${filePath}: ${e.message}`); |
| 133 | + throw e; |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +if (!isAllOk) { |
| 139 | + process.exit(1); |
| 140 | +} |
0 commit comments