Inspired by my last blog post on creating a Python script for organizing files, I decided to create a script that would generate an organized representation of project structure in markdown format. This new tool would not only help me visualize my folders and files but also serve as a handy resource for creating GitHub README files. With a simple run, I could generate a neat overview of my projectsβmaking documentation easy.
The Idea Takes Shape
I thought of a beautifully formatted markdown output that listed my folders and files, complete with icons to make it visually appealing. I chose the classic folder emoji (ποΈ) for directories and a document emoji (π) for files.
Hereβs a the main function of the code:
def generate_markdown_structure(root_dir, skip_folders, indent=''):
markdown_structure = ''
items = sorted(os.listdir(root_dir))
for index, item in enumerate(items):
if item in skip_folders:
continue
path = os.path.join(root_dir, item)
if os.path.isdir(path):
icon = 'ποΈ'
connector = 'βββ ' if index == len(items) - 1 else 'βββ '
markdown_structure += f"{indent}{connector}{icon} {item}\n"
sub_indent = indent + (' ' if connector == 'βββ ' else 'β ')
markdown_structure += generate_markdown_structure(path, skip_folders, sub_indent)
else:
icon = 'π'
connector = 'βββ ' if index == len(items) - 1 else 'βββ '
markdown_structure += f"{indent}{connector}{icon} {item}\n"
return markdown_structure
This piece of code recursively explores the directory and generates a structured output while skipping unwanted folders.
Sample Output
βββ π .gitignore
βββ π CHANGELOG.md
βββ π README.md
βββ π eslint.config.js
βββ π index.html
βββ π package-lock.json
βββ π package.json
βββ π postcss.config.js
βββ ποΈ public
β βββ π vite.svg
βββ ποΈ src
β βββ π App.css
β βββ π App.tsx
β βββ ποΈ assets
β β βββ π react.svg
β βββ ποΈ component
β β βββ ποΈ add-expense
β β β βββ π AddExpense.module.css
β β β βββ π AddExpense.tsx
β β βββ ποΈ expense-grid
β β β βββ π ExpenseGrid.module.css
β β β βββ π ExpenseGrid.tsx
β β βββ ποΈ home
β β β βββ π Home.tsx
β β βββ ποΈ navbar
β β β βββ π Navbar.module.css
β β β βββ π Navbar.tsx
β β βββ ποΈ sidebar
β β βββ π Sidebar.module.css
β β βββ π Sidebar.tsx
β βββ π index.css
β βββ π main.tsx
β βββ π types.ts
β βββ π vite-env.d.ts
βββ π tailwind.config.js
βββ π tsconfig.app.json
βββ π tsconfig.json
βββ π tsconfig.node.json
βββ π vite.config.ts
Next Steps
Now that we have a powerful script to organize the folder structure in markdown format, itβs time to think about how to make the most of it. One exciting possibility is to integrate this script into your release process or CI/CD pipeline. By doing so, you can ensure that your README file is always up to date with the latest project structure without any manual effort.
Automating with CI/CD
Hereβs how you can set up your CI/CD pipeline to automatically generate or update your README file:
-
Add the Script to Your Repository: Make sure the script is part of your repository. Place it in a logical location, such as a
/scripts
folder. -
Create a Workflow: If youβre using GitHub Actions, you can create a workflow that triggers on specific events, like when you push new commits or create a release. Your workflow file (e.g.,
.github/workflows/update-readme.yml
) might look something like this:
name: Update README
on:
push:
branches:
- main
release:
types: [created]
jobs:
update-readme:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.8' # Python version
- name: Install dependencies
run: |
pip install -r requirements.txt # If any dependencies
- name: Run folder structure script
run: python scripts/your_script.py # Update with script path
- name: Commit and push changes
run: |
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"
git add project_structure.md
git commit -m "Update project structure in README" || echo "No changes to commit"
git push
-
Update Your README: In your script, you can modify it to not only generate a
project_structure.md
file but also append or integrate its contents directly into your mainREADME.md
. This way, youβll always have an up-to-date overview of your project structure right on your projectβs main page. -
Test the Workflow: Push your changes to the repository and check the Actions tab on GitHub to see if the workflow runs successfully.
You can access the full code on my GitHub.