Skip to content

Commit 24f29bb

Browse files
feat: implement auto-linting and commit agent with husky and chokidar support
1 parent 0ec86bc commit 24f29bb

6 files changed

Lines changed: 198 additions & 13 deletions

File tree

.husky/pre-push

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
npm run lint
Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,38 @@
1-
# React + Vite
1+
# React Hooks: useCallback
22

3-
This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
3+
## 🎯 Objective
4+
Master the `useCallback` hook to stabilize function references and optimize child component performance using `React.memo`.
45

5-
Currently, two official plugins are available:
6+
## 🛠 Tech Stack
7+
- **Languages:** JavaScript (ES6+)
8+
- **Frameworks/Libraries:** React.js
9+
- **Tools:** Vite, ESLint
610

7-
- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh
8-
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh
11+
## 🧠 Key Learnings
12+
- Preventing unnecessary child re-renders by memoizing callback functions.
13+
- Identifying when a function reference change causes performance bottlenecks.
14+
- Correctly identifying dependencies for memoized callbacks.
15+
16+
## 🚀 Run Instructions
17+
18+
### Prerequisites
19+
- Node.js v18+
20+
21+
### Setup
22+
1. Navigate to this directory:
23+
```bash
24+
cd frontend/react-hooks/use-callback
25+
```
26+
2. Install dependencies:
27+
```bash
28+
npm install
29+
```
30+
31+
### Execution
32+
- Run the application:
33+
```bash
34+
npm run dev
35+
```
36+
37+
---
38+
*Generated using the SDE2 Learning-chunk Template.*
Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,38 @@
1-
# React + Vite
1+
# React Hooks: useMemo
22

3-
This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
3+
## 🎯 Objective
4+
Learn how to optimize React applications by memoizing expensive calculations and preventing unnecessary re-renders.
45

5-
Currently, two official plugins are available:
6+
## 🛠 Tech Stack
7+
- **Languages:** JavaScript (ES6+)
8+
- **Frameworks/Libraries:** React.js
9+
- **Tools:** Vite, ESLint
610

7-
- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh
8-
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh
11+
## 🧠 Key Learnings
12+
- Understanding the difference between memoization and caching.
13+
- When to use `useMemo` for performance optimization.
14+
- Managing the dependency array to ensure data consistency.
15+
16+
## 🚀 Run Instructions
17+
18+
### Prerequisites
19+
- Node.js v18+
20+
21+
### Setup
22+
1. Navigate to this directory:
23+
```bash
24+
cd frontend/react-hooks/use-memo
25+
```
26+
2. Install dependencies:
27+
```bash
28+
npm install
29+
```
30+
31+
### Execution
32+
- Run the application:
33+
```bash
34+
npm run dev
35+
```
36+
37+
---
38+
*Generated using the SDE2 Learning-chunk Template.*

package-lock.json

Lines changed: 49 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44
"description": "Professional SDE Learning Log",
55
"main": "index.js",
66
"scripts": {
7-
"lint": "eslint . --ext .js"
7+
"lint": "eslint . --ext .js",
8+
"git-watch": "node scripts/git-watch-agent.js",
9+
"prepare": "husky"
810
},
911
"devDependencies": {
10-
"eslint": "^8.0.0"
12+
"chokidar": "^5.0.0",
13+
"eslint": "^8.0.0",
14+
"husky": "^9.1.7"
1115
},
1216
"author": "Abdullah Sadik",
1317
"license": "MIT"

scripts/git-watch-agent.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
const chokidar = require('chokidar');
2+
const { execSync } = require('child_process');
3+
const path = require('path');
4+
5+
/**
6+
* SDE Mastery: Git Watch Agent
7+
* Automatically runs linting, auto-fixes errors, and commits on save.
8+
*/
9+
10+
console.log('🚀 Git Watch Agent active. Monitoring for saves...');
11+
console.log('Standards: ESLint Auto-fix | Git Auto-commit');
12+
13+
// Initialize watcher on domain directories
14+
const watcher = chokidar.watch(['frontend', 'backend', 'basics', 'database'], {
15+
ignored: [
16+
/(^|[\/\\])\../, // ignore dotfiles
17+
'**/node_modules/**',
18+
'**/dist/**',
19+
'**/build/**'
20+
],
21+
persistent: true,
22+
ignoreInitial: true,
23+
awaitWriteFinish: {
24+
stabilityThreshold: 500,
25+
pollInterval: 100
26+
}
27+
});
28+
29+
let isProcessing = false;
30+
31+
watcher.on('change', (filePath) => {
32+
if (isProcessing) return;
33+
34+
// Only process JS/JSX files for linting/fixing
35+
const ext = path.extname(filePath);
36+
if (!['.js', '.jsx'].includes(ext)) return;
37+
38+
isProcessing = true;
39+
const fileName = path.basename(filePath);
40+
41+
console.log(`\n[AGENT] Save detected: ${fileName}`);
42+
43+
try {
44+
// 1. Run Lint and Auto-fix
45+
console.log(`[LINT] Fixing ${fileName}...`);
46+
try {
47+
execSync(`npx eslint "${filePath}" --fix`, { stdio: 'inherit' });
48+
} catch (lintError) {
49+
console.warn(`[LINT] Warning: Some errors could not be auto-fixed.`);
50+
}
51+
52+
// 2. Stage the file
53+
execSync(`git add "${filePath}"`);
54+
55+
// 3. Check for changes (avoid empty commits)
56+
const hasChanges = execSync('git diff --cached --name-only').toString().trim();
57+
58+
if (hasChanges) {
59+
const commitMsg = `style: auto-fix and track changes in ${fileName}`;
60+
console.log(`[GIT] Committing: ${commitMsg}`);
61+
execSync(`git commit -m "${commitMsg}"`, { stdio: 'inherit' });
62+
console.log(`[DONE] ${fileName} synchronized.`);
63+
} else {
64+
console.log(`[SKIP] No changes detected after linting.`);
65+
}
66+
67+
} catch (error) {
68+
console.error(`[ERROR] Agent failed for ${fileName}:`, error.message);
69+
} finally {
70+
isProcessing = false;
71+
}
72+
});

0 commit comments

Comments
 (0)