|
| 1 | +name: Update Interactive README |
| 2 | +on: |
| 3 | + push: |
| 4 | + branches: [main] |
| 5 | +permissions: |
| 6 | + contents: write |
| 7 | +jobs: |
| 8 | + update-readme: |
| 9 | + runs-on: ubuntu-latest |
| 10 | + steps: |
| 11 | + - name: Checkout code |
| 12 | + uses: actions/checkout@v4 |
| 13 | + with: |
| 14 | + fetch-depth: 0 |
| 15 | + |
| 16 | + - name: Setup Node.js |
| 17 | + uses: actions/setup-node@v4 |
| 18 | + with: |
| 19 | + node-version: '20' |
| 20 | + |
| 21 | + - name: Generate README |
| 22 | + run: | |
| 23 | + cat > README.md << 'EOL' |
| 24 | + <div align="center"> |
| 25 | + |
| 26 | + # 🚀 DSA-CP-Codes |
| 27 | + |
| 28 | +  |
| 29 | +  |
| 30 | + [](https://github.com/Ayush-Vish/DSA-CP-Codes/stargazers) |
| 31 | + |
| 32 | + </div> |
| 33 | + |
| 34 | + <p align="center">A collection of Data Structures, Algorithms, and Competitive Programming solutions.</p> |
| 35 | + |
| 36 | + <hr> |
| 37 | + |
| 38 | + ## 📁 Project Structure |
| 39 | + |
| 40 | + <details open> |
| 41 | + <summary><b>🗂️ Directory Explorer</b></summary> |
| 42 | + <div id="directory-explorer"> |
| 43 | + |
| 44 | + EOL |
| 45 | + |
| 46 | + node -e ' |
| 47 | + const fs = require("fs"); |
| 48 | + const path = require("path"); |
| 49 | + |
| 50 | + const repoUrl = "https://github.com/Ayush-Vish/DSA-CP-Codes/blob/main"; |
| 51 | + const ignoreList = [".git", ".github", "node_modules", "README.md", ".gitignore"]; |
| 52 | + |
| 53 | + function getLanguage(filename) { |
| 54 | + const ext = path.extname(filename).toLowerCase(); |
| 55 | + const langMap = { ".js": "javascript", ".py": "python", ".java": "java", ".cpp": "cpp", ".c": "c" }; |
| 56 | + return langMap[ext] || ""; |
| 57 | + } |
| 58 | + |
| 59 | + function getFileIcon(filename) { |
| 60 | + const ext = path.extname(filename).toLowerCase(); |
| 61 | + const icons = { ".js": "⚡", ".py": "🐍", ".java": "☕", ".cpp": "🔥", ".c": "🔍" }; |
| 62 | + return icons[ext] || "📄"; |
| 63 | + } |
| 64 | + |
| 65 | + function getCodePreview(filePath, maxLines = 5) { |
| 66 | + try { |
| 67 | + if (fs.statSync(filePath).size > 10000) return ""; |
| 68 | + const content = fs.readFileSync(filePath, "utf8"); |
| 69 | + return content.split("\n").slice(0, maxLines).join("\n"); |
| 70 | + } catch (err) { |
| 71 | + return ""; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + function createTreeMarkdown(dir, prefix = "", level = 0) { |
| 76 | + let result = ""; |
| 77 | + const items = fs.readdirSync(dir) |
| 78 | + .filter(item => !ignoreList.includes(item)) |
| 79 | + .sort((a, b) => { |
| 80 | + const aIsDir = fs.statSync(path.join(dir, a)).isDirectory(); |
| 81 | + const bIsDir = fs.statSync(path.join(dir, b)).isDirectory(); |
| 82 | + return aIsDir && !bIsDir ? -1 : !aIsDir && bIsDir ? 1 : a.localeCompare(b); |
| 83 | + }); |
| 84 | + |
| 85 | + for (const item of items) { |
| 86 | + const itemPath = path.join(dir, item); |
| 87 | + const relativePath = path.relative(".", itemPath).replace(/\\/g, "/"); |
| 88 | + |
| 89 | + if (fs.statSync(itemPath).isDirectory()) { |
| 90 | + const dirCount = fs.readdirSync(itemPath).filter(i => !ignoreList.includes(i)).length; |
| 91 | + result += `${prefix}<details${level === 0 ? " open" : ""} class="dir-details">\n`; |
| 92 | + result += `${prefix} <summary><span class="dir-icon">📂</span> ${item} (${dirCount} items)</summary>\n`; |
| 93 | + result += `${prefix} <div class="dir-container">\n`; |
| 94 | + result += createTreeMarkdown(itemPath, prefix + " ", level + 1); |
| 95 | + result += `${prefix} </div>\n`; |
| 96 | + result += `${prefix}</details>\n\n`; |
| 97 | + } else { |
| 98 | + const fileIcon = getFileIcon(item); |
| 99 | + const fileSize = (fs.statSync(itemPath).size / 1024).toFixed(1); |
| 100 | + const gitHubLink = `${repoUrl}/${relativePath}`; |
| 101 | + const language = getLanguage(item); |
| 102 | + const preview = getCodePreview(itemPath); |
| 103 | + |
| 104 | + result += `${prefix}<div class="file-item">\n`; |
| 105 | + result += `${prefix} <span class="file-icon">${fileIcon}</span> `; |
| 106 | + result += `<a href="${gitHubLink}" target="_blank">${item}</a> (${fileSize} KB)\n`; |
| 107 | + if (preview && language) { |
| 108 | + result += `${prefix} <details class="preview-details">\n`; |
| 109 | + result += `${prefix} <summary>Preview</summary>\n`; |
| 110 | + result += `${prefix} \`\`\`${language}\n${preview}\n...\n\`\`\`\n`; |
| 111 | + result += `${prefix} </details>\n`; |
| 112 | + } |
| 113 | + result += `${prefix}</div>\n\n`; |
| 114 | + } |
| 115 | + } |
| 116 | + return result || `${prefix}<div class="empty-dir">Empty directory</div>\n\n`; |
| 117 | + } |
| 118 | + |
| 119 | + fs.appendFileSync("README.md", createTreeMarkdown(".")); |
| 120 | + ' |
| 121 | + |
| 122 | + cat >> README.md << 'EOL' |
| 123 | + </div> |
| 124 | + </details> |
| 125 | + |
| 126 | + <hr> |
| 127 | + |
| 128 | + ## 🔍 How to Navigate |
| 129 | + |
| 130 | + - Click 📂 to expand/collapse directories |
| 131 | + - Click file names to view code on GitHub |
| 132 | + - Expand previews for code snippets |
| 133 | + |
| 134 | + <hr> |
| 135 | + |
| 136 | + <div align="center"> |
| 137 | + <p>⭐ Star this repo if it's helpful! ⭐</p> |
| 138 | + <p>Generated on $(date '+%B %d, %Y')</p> |
| 139 | + </div> |
| 140 | + |
| 141 | + <style> |
| 142 | + #directory-explorer { |
| 143 | + background: #f6f8fa; |
| 144 | + border: 1px solid #e1e4e8; |
| 145 | + border-radius: 6px; |
| 146 | + padding: 10px; |
| 147 | + max-height: 80vh; |
| 148 | + overflow-y: auto; |
| 149 | + } |
| 150 | + .dir-details { margin: 5px 0; } |
| 151 | + summary { cursor: pointer; display: flex; align-items: center; padding: 5px; } |
| 152 | + .dir-icon { margin-right: 8px; } |
| 153 | + .dir-container { margin-left: 20px; border-left: 1px solid #e1e4e8; } |
| 154 | + .file-item { margin: 5px 0 5px 20px; } |
| 155 | + .file-icon { margin-right: 8px; } |
| 156 | + a { color: #0366d6; text-decoration: none; } |
| 157 | + a:hover { text-decoration: underline; } |
| 158 | + .preview-details { margin-left: 28px; } |
| 159 | + .empty-dir { color: #586069; font-style: italic; } |
| 160 | + </style> |
| 161 | + EOL |
| 162 | +
|
| 163 | + - name: Commit and push |
| 164 | + env: |
| 165 | + TOKEN: ${{ secrets.GH_PAT }} |
| 166 | + run: | |
| 167 | + git config user.name "GitHub Actions Bot" |
| 168 | + git config user.email "actions@github.com" |
| 169 | + git remote set-url origin https://x-access-token:${TOKEN}@github.com/${{ github.repository }} |
| 170 | + if ! git diff --quiet README.md; then |
| 171 | + git add README.md |
| 172 | + git commit -m "Update interactive README" |
| 173 | + git push origin main |
| 174 | + else |
| 175 | + echo "No changes to commit" |
| 176 | + fi |
0 commit comments