|
| 1 | +import fs from "fs"; |
| 2 | +import path from "path"; |
| 3 | +import { fileURLToPath } from "url"; |
| 4 | + |
| 5 | +const __filename = fileURLToPath(import.meta.url); |
| 6 | +const __dirname = path.dirname(__filename); |
| 7 | + |
| 8 | +const PATTERNS_DIR = path.join(path.dirname(__dirname), "content/en/patterns"); |
| 9 | +const README_PATH = path.join(path.dirname(__dirname), "README.md"); |
| 10 | +const PROD_URL = "https://uxpatterns.dev/patterns"; |
| 11 | + |
| 12 | +// Markers for the patterns section in README |
| 13 | +const START_MARKER = |
| 14 | + "<!-- PATTERNS-LIST:START - Do not remove or modify this section -->"; |
| 15 | +const END_MARKER = "<!-- PATTERNS-LIST:END -->"; |
| 16 | + |
| 17 | +// Helper to check if a pattern is complete by checking its content |
| 18 | +function isPatternComplete(filePath) { |
| 19 | + try { |
| 20 | + const content = fs.readFileSync(filePath, "utf8"); |
| 21 | + // A pattern is considered complete if it has more than 50 lines |
| 22 | + // You might want to adjust this threshold or use a different criteria |
| 23 | + return content.split("\n").length > 50; |
| 24 | + } catch (error) { |
| 25 | + return false; |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +// Convert string to title case |
| 30 | +function toTitleCase(str) { |
| 31 | + return str |
| 32 | + .split("-") |
| 33 | + .map((word) => word.charAt(0).toUpperCase() + word.slice(1)) |
| 34 | + .join(" "); |
| 35 | +} |
| 36 | + |
| 37 | +function generatePatternsList() { |
| 38 | + // Get all pattern categories (directories) |
| 39 | + const categories = fs |
| 40 | + .readdirSync(PATTERNS_DIR) |
| 41 | + .filter( |
| 42 | + (item) => |
| 43 | + fs.statSync(path.join(PATTERNS_DIR, item)).isDirectory() && |
| 44 | + !item.startsWith("_") |
| 45 | + ); |
| 46 | + |
| 47 | + // Generate patterns list |
| 48 | + let patternsContent = |
| 49 | + "\nThis is an updated list of available and incoming patterns.\n"; |
| 50 | + |
| 51 | + for (const category of categories) { |
| 52 | + const categoryPath = path.join(PATTERNS_DIR, category); |
| 53 | + const patterns = fs |
| 54 | + .readdirSync(categoryPath) |
| 55 | + .filter((file) => file.endsWith(".mdx") && !file.startsWith("_")) |
| 56 | + .map((file) => file.replace(".mdx", "")); |
| 57 | + |
| 58 | + if (patterns.length > 0) { |
| 59 | + const categoryTitle = toTitleCase(category); |
| 60 | + patternsContent += `\n### ${categoryTitle}\n\n`; |
| 61 | + |
| 62 | + for (const pattern of patterns) { |
| 63 | + const patternPath = path.join(categoryPath, `${pattern}.mdx`); |
| 64 | + const isComplete = isPatternComplete(patternPath); |
| 65 | + const patternTitle = toTitleCase(pattern); |
| 66 | + |
| 67 | + if (isComplete) { |
| 68 | + patternsContent += `- [${patternTitle}](${PROD_URL}/${category}/${pattern})\n`; |
| 69 | + } else { |
| 70 | + patternsContent += `- ${patternTitle} (coming soon)\n`; |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return patternsContent; |
| 77 | +} |
| 78 | + |
| 79 | +function updatePatternsList() { |
| 80 | + try { |
| 81 | + // Read current README |
| 82 | + let readme = fs.readFileSync(README_PATH, "utf8"); |
| 83 | + |
| 84 | + // Check if markers exist |
| 85 | + if (!readme.includes(START_MARKER) || !readme.includes(END_MARKER)) { |
| 86 | + throw new Error( |
| 87 | + `Could not find ${START_MARKER} and ${END_MARKER} markers in README.md. Please add them around the patterns section.` |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + // Generate new patterns list |
| 92 | + const patternsContent = generatePatternsList(); |
| 93 | + |
| 94 | + // Replace content between markers |
| 95 | + const newReadme = readme.replace( |
| 96 | + new RegExp(`${START_MARKER}[\\s\\S]*?${END_MARKER}`), |
| 97 | + `${START_MARKER}${patternsContent}${END_MARKER}` |
| 98 | + ); |
| 99 | + |
| 100 | + // Write updated README |
| 101 | + fs.writeFileSync(README_PATH, newReadme); |
| 102 | + console.log("✅ README.md has been updated successfully!"); |
| 103 | + } catch (error) { |
| 104 | + console.error("��� Error updating patterns list:", error); |
| 105 | + process.exit(1); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +// Run the update if this file is being executed directly |
| 110 | +if (process.argv[1] === fileURLToPath(import.meta.url)) { |
| 111 | + updatePatternsList(); |
| 112 | +} |
| 113 | + |
| 114 | +export default updatePatternsList; |
0 commit comments