-
Notifications
You must be signed in to change notification settings - Fork 0
149 lines (125 loc) · 5.73 KB
/
update-project-structure.yml
File metadata and controls
149 lines (125 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
name: Update Project Structure
on:
push:
branches: ["**"]
workflow_dispatch:
permissions:
contents: write
jobs:
update-structure:
runs-on: ubuntu-latest
env:
README_PATH: README.md
HEADING_RX: "^##\\s*📁\\s*Project Structure\\s*$"
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: 20
- name: Rebuild folder tree and update README
run: |
node - <<'NODE'
const fs = require('fs');
const path = require('path');
const README_PATH = process.env.README_PATH || 'README.md';
const HEADING_RX = new RegExp(process.env.HEADING_RX || '^##\\s*📁\\s*Project Structure\\s*$', 'm');
const repoName = (process.env.GITHUB_REPOSITORY || '').split('/').pop() || path.basename(process.cwd());
if (!fs.existsSync(README_PATH)) {
console.error(`❌ README not found at ${README_PATH}`);
process.exit(1);
}
// ---------------- Tree builder (directories first, sorted; box-drawing lines) ----------------
const IGNORE = new Set([
'.git','node_modules','.DS_Store','.idea','.vscode','.env','.venv','.python-version',
'.next','dist','build','coverage','.turbo','.cache',
'public','logs','uploads','tests','.husky','.github','scripts'
]);
function listDir(dir) {
const entries = fs.readdirSync(dir, { withFileTypes: true })
.filter(e => !IGNORE.has(e.name))
.map(e => ({ name: e.name, isDir: e.isDirectory() }))
.sort((a, b) => {
if (a.isDir !== b.isDir) return a.isDir ? -1 : 1; // dirs first
return a.name.localeCompare(b.name);
});
return entries;
}
// ----- Improved Tree Drawer (adds │ vertical connectors) -----
function renderTree(rootAbs, displayRootName) {
const lines = [`└── ${displayRootName}/`];
function walk(abs, prefix = '') {
const entries = listDir(abs);
entries.forEach((ent, idx) => {
const isLast = idx === entries.length - 1;
const connector = isLast ? '└── ' : '├── ';
const line = `${prefix}${connector}${ent.name}${ent.isDir ? '/' : ''}`;
lines.push(line);
if (ent.isDir) {
const childAbs = path.join(abs, ent.name);
const nextPrefix = prefix + (isLast ? ' ' : '│ ');
walk(childAbs, nextPrefix);
}
});
}
walk(rootAbs, '');
return lines.join('\n');
}
const treeText = renderTree(process.cwd(), repoName);
// Wrap with the exact code fence style used in the README sample
const fencedTree = "```sh\n" + treeText + "\n```";
// ---------------- Replace the block after the target heading ----------------
const readme = fs.readFileSync(README_PATH, 'utf8');
// Find heading
const headingMatch = readme.match(HEADING_RX);
if (!headingMatch) {
console.error('❌ Could not find "## 📁 Project Structure" heading in README.');
process.exit(1);
}
// Find the first ``` block after the heading and its closing ```
const headingIndex = headingMatch.index;
const afterHeading = readme.slice(headingIndex);
// Locate the opening code fence after the heading
const openFenceIdxRel = afterHeading.indexOf('```');
if (openFenceIdxRel === -1) {
console.error('❌ No code fence found after the Project Structure heading.');
process.exit(1);
}
const openFenceAbs = headingIndex + openFenceIdxRel;
// Locate the closing code fence after the opening
const afterOpen = readme.slice(openFenceAbs + 3);
const closeFenceIdxRel = afterOpen.indexOf('```');
if (closeFenceIdxRel === -1) {
console.error('❌ Closing code fence not found for the Project Structure section.');
process.exit(1);
}
const closeFenceAbs = openFenceAbs + 3 + closeFenceIdxRel + 3; // include closing ```
// New README with replaced fenced block
const before = readme.slice(0, openFenceAbs);
const after = readme.slice(closeFenceAbs);
const updated = before + fencedTree + after;
if (updated === readme) {
console.log('No changes to Project Structure.');
process.exit(0);
}
fs.writeFileSync(README_PATH, updated, 'utf8');
console.log('✅ Project Structure updated in README.');
NODE
- name: Commit & push if changed
env:
PAT_OR_TOKEN: ${{ secrets.PERSONAL_TOKEN || secrets.GITHUB_TOKEN }}
README_PATH: README.md
run: |
if git diff --quiet -- "${README_PATH}"; then
echo "No changes."
exit 0
fi
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add "${README_PATH}"
git commit -m "docs: auto-update Project Structure tree"
git push https://x-access-token:${PAT_OR_TOKEN}@github.com/${GITHUB_REPOSITORY}.git HEAD:${GITHUB_REF_NAME:-main} || \
git push https://x-access-token:${PAT_OR_TOKEN}@github.com/${GITHUB_REPOSITORY}.git HEAD:main || \
git push https://x-access-token:${PAT_OR_TOKEN}@github.com/${GITHUB_REPOSITORY}.git HEAD:master