Codex CLI: Tự Động Hoá Refactor Codebase Lớn
Hướng dẫn sử dụng OpenAI Codex CLI để tự động hoá các tác vụ refactor lặp đi lặp lại trên codebase lớn. Tiết kiệm hàng ngày làm việc thủ công.
Refactor một codebase lớn với hàng trăm files là nightmare. Nhưng với Codex CLI — AI coding agent chạy trong terminal từ OpenAI — bạn có thể tự động hoá phần lớn công việc này.
Codex CLI là gì?
Codex CLI là open-source coding agent chạy trong terminal, tương tự Claude Code nhưng từ OpenAI. Nó đọc codebase, thực thi commands, và edit files trực tiếp. Đặc biệt mạnh cho các tác vụ refactor lặp lại.
Setup
# Cài đặt
npm install -g @openai/codex
# Set API key
export OPENAI_API_KEY=sk-...
# Chạy trong project
cd your-project
codex
Use Case 1: Migration API patterns
Ví dụ: migrate từ getServerSideProps sang App Router Server Components trong Next.js. Thay vì sửa từng file một:
Prompt cho Codex:
"Tìm tất cả files dùng getServerSideProps trong src/pages/.
Với mỗi file, tạo equivalent App Router page trong src/app/:
- Convert getServerSideProps thành async Server Component
- Move data fetching vào component body
- Update imports cho next/navigation thay vì next/router
- Giữ nguyên UI logic"
Codex sẽ scan codebase, tìm pattern, và apply changes across files. Bạn review diff trước khi accept.
Use Case 2: Update import paths
Sau khi restructure folders, cần update hàng trăm import paths:
// Trước refactor:
import { Button } from '../../components/ui/Button';
import { useAuth } from '../../hooks/useAuth';
import { formatDate } from '../../utils/format';
// Sau refactor - Codex tự update tất cả files:
import { Button } from '@/components/ui/Button';
import { useAuth } from '@/hooks/useAuth';
import { formatDate } from '@/lib/utils';
Use Case 3: Thêm TypeScript types
Gradually typing một JS codebase — Codex có thể infer types từ usage patterns và thêm type annotations:
Prompt: "Tìm tất cả functions trong src/utils/ chưa có
TypeScript type annotations. Thêm parameter types và return
types dựa trên cách functions được sử dụng trong codebase.
Ưu tiên strict types, tránh dùng any."
Use Case 4: Chuẩn hoá error handling
Convert tất cả try/catch blocks sang một pattern thống nhất. Codex tìm pattern cũ và replace bằng pattern mới across toàn bộ codebase.
So sánh với Claude Code
Codex CLI: Mạnh cho batch operations trên nhiều files cùng pattern. Auto-apply mode tiện cho repetitive tasks.
Claude Code: Context understanding sâu hơn. Tốt hơn cho refactors cần hiểu business logic, không chỉ pattern matching.
Combo lý tưởng: dùng Claude Code để plan refactor strategy, dùng Codex CLI để execute repetitive parts.
Tips quan trọng
Luôn review diffs: AI refactor có thể miss edge cases. Git diff trước khi commit.
Chạy tests sau mỗi batch: Refactor 10 files → run tests → fix issues → tiếp tục.
Backup branch: Tạo branch riêng cho AI refactor, không làm trực tiếp trên main.
Kết luận
Codex CLI biến công việc refactor từ "mấy ngày" thành "mấy giờ". Nhưng nhớ: AI là tool, không phải magic. Bạn vẫn cần plan refactor carefully, review changes thoroughly, và test exhaustively. AI chỉ giúp execute nhanh hơn.
Admin
Bình luận (0)
Đăng nhập để bình luận
Chưa có bình luận nào. Hãy là người đầu tiên!