treemk
Version:
A CLI tool to generate project structures from text, JSON templates, file, with boilerplate and git integration
422 lines (395 loc) β’ 18.8 kB
Markdown
# π treemk Workflow & Architecture
## π How It Works (Visual Flow)
```
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β INPUT METHODS (Choose One) β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββΌββββββββββββββ
β β β
βΌ βΌ βΌ
ββββββββββββ ββββββββββββ ββββββββββββ
β --text β β --json β β File β
β Inline β β Inline β β Anywhere β
ββββββββββββ ββββββββββββ ββββββββββββ
β β β
β β β
βΌ βΌ βΌ
ββββββββββββ ββββββββββββ ββββββββββββ
βTemplate β β Pipe β β URL β
β Built-in β β stdin β β Fetch β
ββββββββββββ ββββββββββββ ββββββββββββ
β β β
βββββββββββββββΌββββββββββββββ
β
βΌ
ββββββββββββββββββββ
β PARSE & VALIDATE β
β (index.js) β
ββββββββββββββββββββ
β
βΌ
ββββββββββββββββββββ
β STRUCTURE GEN β
β (lib/structure) β
ββββββββββββββββββββ
β
βββββββββββββββΌββββββββββββββ
β β β
βΌ βΌ βΌ
ββββββββββββ ββββββββββββ ββββββββββββ
β Create β β Preview β β Dry Run β
β Files β β Only β β Only β
ββββββββββββ ββββββββββββ ββββββββββββ
β
βΌ
ββββββββββββββββ
β Boilerplate? β
ββββββββββββββββ
β
ββββββββ΄βββββββ
β β
βΌ βΌ
ββββββ ββββββ
βYES β β NO β
ββββββ ββββββ
β β
βΌ β
βββββββββββ β
β Smart β β
β Content β β
βββββββββββ β
β β
ββββββββ¬βββββββ
β
βΌ
ββββββββββββββββ
β Install? β
ββββββββββββββββ
β
ββββββββ΄βββββββ
β β
βΌ βΌ
ββββββββββ ββββββββββ
βnpm/pip β β Skip β
ββββββββββ ββββββββββ
β β
ββββββββ¬βββββββ
β
βΌ
ββββββββββββββββ
β Git Init? β
ββββββββββββββββ
β
ββββββββ΄βββββββ
β β
βΌ βΌ
ββββββββββ ββββββββββ
βgit initβ β Skip β
βcommit β β β
βpush β β β
ββββββββββ ββββββββββ
β β
ββββββββ¬βββββββ
β
βΌ
ββββββββββββββββ
β SUCCESS! β
β β
Done β
ββββββββββββββββ
```
## ποΈ Architecture (Module Breakdown)
```
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β index.js (430+ lines) β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β’ Argument parsing β β
β β β’ Input source detection β β
β β β’ Path resolution (any location) β β
β β β’ Error handling & user messages β β
β β β’ Config file loading β β
β β β’ Template operations routing β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββΌββββββββββββββββββββββ
β β β
βΌ βΌ βΌ
ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ
β templates.jsβ β structure- β βboilerplate.jsβ
β (150 lines) β βgenerator.js β β (220 lines) β
β β β (280 lines) β β β
β β’ Save β β β β β’ server.js β
β β’ Load β β β’ Parse tree β β β’ model.js β
β β’ List β β β’ Parse JSON β β β’ route.js β
β β’ Remove β β β’ Create dirsβ β β’ test.js β
β β’ Built-in β β β’ Create filesβ β β’ Dockerfile β
β β β β’ Preview β β β’ .env β
β β β β’ Git ops β β β’ .gitignore β
β β β β’ npm/pip β β β’ etc. β
ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ
β β β
βββββββββββββββββββββββΌββββββββββββββββββββββ
β
βΌ
ββββββββββββββββ
β config.js β
β (25 lines) β
β β
β β’ Load JSON β
β β’ Merge opts β
ββββββββββββββββ
```
## π‘ Input Method Decision Tree
```
Need to create project?
β
ββ Have structure file?
β ββ YES βββΊ treemk -i file.txt -o ./app
β ββ NO
β β
β ββ Simple structure?
β β ββ YES βββΊ treemk --text "src/index.js\nREADME.md" -o ./app
β β
β ββ Complex JSON structure?
β β ββ YES βββΊ treemk --json-input '{"src":["index.js"]}' -o ./app
β β
β ββ Standard project type?
β β ββ YES βββΊ treemk --template node -o ./app
β β
β ββ Have saved template?
β ββ YES βββΊ treemk --template-use mytemplate -o ./app
β
ββ Want to preview first?
ββ YES βββΊ treemk --preview --template node
```
## π― Feature Decision Matrix
```
ββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββ
β What You Want β Command Flag β
ββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββ€
β Add real code β --boilerplate (-b) β
β Install deps auto β --install β
β Init git repo β --git-init (-g) β
β First commit β --git-commit β
β Push to GitHub β --git-push β
β Preview only β --preview β
β Test run β --dry-run (-d) β
β Save for later β --template-save <n> β
β Use saved β --template-use <n> β
β List all templates β --template-list β
β Set defaults β Create treemk.config.json β
ββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββββββββββ
```
## π Path Resolution Flow
```
File input: treemk -i structure.txt
β
βΌ
βββββββββββββββββββββββββ
β Try as absolute path β
β /path/to/structure.txtβ
βββββββββββββββββββββββββ
β
β Not found
β
βΌ
βββββββββββββββββββββββββ
β Try current directory β
β ./structure.txt β
βββββββββββββββββββββββββ
β
β Not found
β
βΌ
βββββββββββββββββββββββββ
β Try script directory β
β __dirname/structure β
βββββββββββββββββββββββββ
β
β Not found
β
βΌ
βββββββββββββββββββββββββ
β Try Downloads β
β ~/Downloads/structure β
βββββββββββββββββββββββββ
β
β Not found
β
βΌ
βββββββββββββββββββββββββ
β Try Documents β
β ~/Documents/structure β
βββββββββββββββββββββββββ
β
β Not found
β
βΌ
βββββββββββββββββββββββββ
β Try Desktop β
β ~/Desktop/structure β
βββββββββββββββββββββββββ
β
β Not found
β
βΌ
ββββββββββββ
β ERROR β
β "Cannot β
β find" β
ββββββββββββ
```
## π¨ Boilerplate Logic Flow
```
Create file: src/server.js
β
βΌ
βββββββββββββββββββ
β Boilerplate ON? β
βββββββββββββββββββ
β
βββββ΄ββββ
β β
YES NO
β β
β ββββΊ Create empty file
β
βΌ
ββββββββββββββββββββββββ
β Check filename β
β "server.js" β
ββββββββββββββββββββββββ
β
βΌ
ββββββββββββββββββββββββ
β Context-aware match? β
ββββββββββββββββββββββββ
β
βββββ΄ββββ
β β
YES NO
β β
β ββββΊ Check extension (.js)
β β
β ββββΊ Use extension template
β
βΌ
ββββββββββββββββββββββββ
β Get Express template β
β (full working code) β
ββββββββββββββββββββββββ
β
βΌ
ββββββββββββββββββββββββ
β Write to file β
ββββββββββββββββββββββββ
```
## π Template Storage Structure
```
~/.treemk/
βββ templates/
βββ my-api.txt # Your saved template
βββ microservice.txt # Another template
βββ react-app.txt # Another template
Each .txt contains:
src/
βββ server.js
βββ routes/
β βββ api.js
βββ models/
βββ User.js
```
## π Complete Workflow Example
```
Developer wants to create API
β
βΌ
Run: treemk --template-save api
β
(Saves structure to ~/.treemk/templates/)
β
βΌ
Later: treemk --template-use api -o ./users-api -b --install -g
β
ββββΊ Parse arguments
ββββΊ Load template from ~/.treemk/templates/api.txt
ββββΊ Parse structure
ββββΊ Create directories
ββββΊ Create files with boilerplate
ββββΊ Run npm install
ββββΊ Run git init
β
βΌ
Complete API ready in <2 seconds!
β
βΌ
cd users-api && npm start
β
βΌ
π Running!
```
## π Performance Flow
```
Time breakdown for: treemk --template node -o ./app -b --install -g
0.0s ββΊ Parse args & load config
0.1s ββΊ Load template
0.1s ββΊ Parse structure
0.2s ββΊ Create directories (10-20 dirs)
0.3s ββΊ Create files (20-30 files)
0.5s ββΊ Generate boilerplate content
1.0s ββΊ Run npm install (varies)
1.5s ββΊ Run git init
1.5s ββΊ Done! β
Total: ~1.5 seconds (without npm install)
Total: ~30 seconds (with npm install)
```
## π Error Handling Chain
```
treemk -i structure.txt
β
βΌ
Parse args βββΊ OK
β
βΌ
Find file ββββΊ β Not found
β
βΌ
Try paths ββββΊ β All failed
β
βΌ
βββββββββββββββββββββββββββββββ
β Show helpful error: β
β "Cannot find: structure.txt"β
β β
β Try these paths: β
β β’ Absolute path β
β β’ Relative path β
β β’ Downloads folder β
β β
β Or use --text instead! β
βββββββββββββββββββββββββββββββ
β
βΌ
Exit with code 1
```
## π― Quick Decision Guide
```
βββββββββββββββββββββββββββββββββββββββββββββββββββ
β When to Use What? β
βββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β --text ββΊ Quick, simple structures β
β --json-input ββΊ Complex, nested structures β
β -i file ββΊ Large, reusable structures β
β --template ββΊ Standard project types β
β --template-use ββΊ Your saved templates β
β --preview ββΊ Check before creating β
β --boilerplate ββΊ Need working code β
β --install ββΊ Ready-to-run project β
β --git-init ββΊ Version control from start β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββ
```
---
**Visual guide to understanding treemk internals! π¨**