-
-
Notifications
You must be signed in to change notification settings - Fork 40
133 lines (110 loc) · 4.98 KB
/
update-readme.yml
File metadata and controls
133 lines (110 loc) · 4.98 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
name: Update README Dependencies and Structure
on:
push:
branches:
- main
paths-ignore:
- 'README.md'
permissions:
contents: write
jobs:
update-readme:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v6
- name: Setup Node.js
uses: actions/setup-node@v6
with:
node-version: 'lts/*'
- name: Update README.md
run: |
node -e "
const fs = require('fs');
const path = require('path');
const pkg = require('./package.json');
// --- 1. Generate Dependencies ---
const deps = { ...(pkg.dependencies || {}), ...(pkg.devDependencies || {}) };
const sortedDeps = Object.keys(deps).sort();
let depMarkdown = '\n';
for (const dep of sortedDeps) {
depMarkdown += \`- [\${dep}](https://www.npmjs.com/package/\${dep}): \${deps[dep]}\\n\`;
}
depMarkdown += '\n';
// --- 2. Dynamic .env Detection ---
let envFileToInject = null;
// Check if .env.example exists in the root
if (fs.existsSync('.env.example')) {
envFileToInject = '.env/.env.local';
}
// --- 3. Generate Folder Structure ---
const ignoreFolders = ['.git', '.next', 'node_modules', '.github', '.vscode'];
const ignoreFiles = ['LICENSE'];
const noExpandFolders = ['public', 'migrations', 'assets'];
function getStructure(dir, depth = 0) {
let structure = '';
// Read directory items
let items = fs.readdirSync(dir, { withFileTypes: true });
// Filter out ignored folders, files, and *.md files
items = items.filter(item => {
if (item.isDirectory() && ignoreFolders.includes(item.name)) return false;
if (item.isFile() && ignoreFiles.includes(item.name)) return false;
if (item.isFile() && item.name.endsWith('.md')) return false;
return true;
});
// Artificially inject the dynamic .env string if we are at the root
if (depth === 0 && envFileToInject) {
items.push({
name: envFileToInject,
isDirectory: () => false,
isFile: () => true
});
}
// Sort: Directories first, then files alphabetically
items.sort((a, b) => {
if (a.isDirectory() === b.isDirectory()) {
return a.name.localeCompare(b.name);
}
return a.isDirectory() ? -1 : 1;
});
// Build the string
items.forEach((item) => {
// Dynamically generate the tree prefix based on depth (e.g., ' |- ', ' |-- ', ' |--- ')
const prefix = ' '.repeat(depth + 1) + '|' + '-'.repeat(depth + 1) + ' ';
if (item.isDirectory()) {
structure += \`\${prefix}\${item.name}/\\n\`;
// Recurse if at root, OR if we are at depth 1 inside a 'src' folder
const isSrcSubfolder = depth === 1 && path.basename(dir) === 'src';
if ((depth === 0 || isSrcSubfolder) && !noExpandFolders.includes(item.name)) {
structure += getStructure(path.join(dir, item.name), depth + 1);
}
} else {
structure += \`\${prefix}\${item.name}\\n\`;
}
});
return structure;
}
const projectName = pkg.name || 'project-root';
const folderMarkdown = '\\n\`\`\`bash\\n' + projectName + '/\\n' + getStructure('.') + '\`\`\`\\n';
// --- 4. Replace in README ---
const readmePath = './README.md';
let readme = fs.readFileSync(readmePath, 'utf8');
// Replace Dependencies
const depRegex = new RegExp('<!--- DEPENDENCIES_START --->[\\\\s\\\\S]*?<!--- DEPENDENCIES_END --->');
readme = readme.replace(depRegex, '<!--- DEPENDENCIES_START --->' + depMarkdown + '<!--- DEPENDENCIES_END --->');
// Replace Folder Structure
const folderRegex = new RegExp('<!--- FOLDER_STRUCTURE_START --->[\\\\s\\\\S]*?<!--- FOLDER_STRUCTURE_END --->');
if (folderRegex.test(readme)) {
readme = readme.replace(folderRegex, '<!--- FOLDER_STRUCTURE_START --->' + folderMarkdown + '<!--- FOLDER_STRUCTURE_END --->');
} else {
console.log('Warning: FOLDER_STRUCTURE markers not found in README.md');
}
fs.writeFileSync(readmePath, readme);
"
- name: Commit and Push Changes
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add README.md
git commit -m "docs: update dependencies and folder structure in README" || echo "No changes to commit"
git push